Hello Guest

Author Topic: Collisions with small box colliders  (Read 6902 times)

JonDavis23

  • Newbie
  • *
  • Posts: 3
    • View Profile
Collisions with small box colliders
« on: June 01, 2012, 06:48:09 pm »
I'm having issues with my rigidbodies falling through each other slightly.

Im creating a boggle clone, with a 5x6 grid of letters. Each letter has a rigid body that is non kinematic, interpolation is Interpolate, and Collision Detection is Continuous Dynmaic. When the user forms a word, the letters are deleted and new ones fall from above. I'm using triggers to determine the row / col position of the letters, this is why I'd like to use rigidbodies.

I'm using rigidbody.MovePosition() to handle movement. Everything worked fine until I tried moving this to the iPhone. On the computer, a Fixed Timestamp of 0.001 works fine, but it crawls on the phone. Updating the Fixed Timestamp between 0.04 - 0.067 greatly increases the speed, but the collisions are no longer accurate.

The box collider size on my tk2dsprite is (.25f, .25f, .25f).  I know this is really small and most likely the cause of my issue, but that size encompasses my letters and anything larger would have too much space between my letters.

Is there a method to check for overlapping sprites in 2D Toolkit?

Any advice on increasing collision accuracy and mainting a good fps would be greatly appreciated.

Thanks

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Collisions with small box colliders
« Reply #1 on: June 02, 2012, 07:38:25 pm »
Hi,
A collider size of 0.25 sounds pretty small, is that intentional? Edit > Project Settings > Physics - is gravity in the correct scale for your object sizes?

Emre

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Collisions with small box colliders
« Reply #2 on: June 07, 2012, 12:43:16 am »
I'm just learning Unity but I had an issue with sprites going through each other and found that if I set the rigidbody's Z scale to an arbitrarily large number (in my case, 100 "pixels") that the Pong ball wouldn't go through the paddles or walls which used a simple box collider (no rigidbody). Perhaps that will help.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Collisions with small box colliders
« Reply #3 on: June 07, 2012, 11:59:13 am »
Instead of changing the scale, you can also change the collider depth in the sprite collection editor "settings" panel. This will do the same without the need for any scaling.

JonDavis23

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Collisions with small box colliders
« Reply #4 on: June 07, 2012, 06:51:04 pm »
I changed the scale from .25 to (1.075, 1.075, 1.2). I was afraid the sprites would be too large, but then I remembered I could change the Size property on the camera.
I'm trying to get a consistent distance between letters once the gravity stops.  I'm guessing its the Raycast that isn't in sync?

FallSpeed is set to 5

Code: [Select]
    void FixedUpdate()
    {
        var moved = FallSpeed * Time.deltaTime;
        var offsetVector = new Vector3(0, myTransform.collider.bounds.size.y / 2, 0);
        var offsetPosition = myTransform.position - offsetVector;

        if (Physics.Raycast(offsetPosition, Vector3.down, out hit, moved))
        {
            if (hit.transform.tag == "Coin" || hit.transform.tag == "Lift")
            {
                if (coinComponent.IsFalling)
                    coinComponent.IsFalling = false;
            }
        }
        else
        {
            // Hit nothing, falling
            coinComponent.IsFalling = true;
            coinComponent.Triggered = false;

            myTransform.Translate(Vector3.down * FallSpeed * Time.deltaTime);
        }
    }
« Last Edit: June 07, 2012, 08:22:51 pm by JonDavis23 »

JonDavis23

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Collisions with small box colliders
« Reply #5 on: June 10, 2012, 08:18:12 pm »
I was able to work out my gravity issues and it runs well on the iPhone.  If anyone is interested here are the changes I've made.
At first I was lerping to the final position but it was giving me an undesired slowing down effect, so I switched to Sinerp.

Code: [Select]
void FixedUpdate()
    {
        if (Physics.Raycast(myTransform.position, Vector3.down, out hit, 1))
        {
if (coinComponent.IsFalling)
{
tempLayer = hit.transform.gameObject.layer;
// 9 = Coin
// 8 = Lift
            if (tempLayer == 8 || tempLayer == 9)
            {
                    // It's stoping here.
                    // Determine if this is the right row, if so. sinerp the y pos if necessary
                    SetPrecisePosition(coinComponent.Row);

                    if (myTransform.position.y != preciseYPos)
{
// Lerp eases to the end. Lerp appears to add jerkiness near the end of its run.
// Switched to Sinerp
yPos2 = Mathfx.Sinerp(myTransform.position.y, preciseYPos, (FallSpeed * 3.5f) * Time.deltaTime);

tempVector.x = myTransform.position.x;
tempVector.y = yPos2;

myTransform.position = tempVector;
}
                    else
                        coinComponent.IsFalling = false;
                }
            }
        }
        else
        {
            // Hit nothing, falling
            coinComponent.IsFalling = true;
            coinComponent.Triggered = false;

            myTransform.Translate(Vector3.down * FallSpeed * Time.deltaTime);
        }
    }

public static float Sinerp(float start, float end, float value)
{
        return Mathf.Lerp(start, end, Mathf.Sin(value * Mathf.PI * 0.5f));
}