XNA Creators Club Online
Page 1 of 1 (8 items)
Sort Posts: Previous Next

2D Ballz and Linez physicz

Last post 07/11/2009 10:41 by Teh1337Bix. 7 replies.
  • 04/11/2009 10:34

    2D Ballz and Linez physicz

    I am just about to smash my keyboard into the wall with this one. Basically I am making a 2D game where you play as a ball with lots of lines around you that you can bounce off. So far, you can bounce off the lines if you hit them completely on one of the flat sides. To do this it uses a matrix representing the transformation of the line (lines are just axis aligned 1 x 1 squares transformed through matrices) to transform the ball's speed and position into the local space of the line's axis aligned 1 x 1 square, where I can just reverse the Y component of the local speed and then transform back into world speed. Now I want to simulate the ball hitting the edge of a line, which would just be ball on line physics. My try at this kinda works with when the ball falls from top down onto flat lines with no rotation, but fails otherwise. Hopefully you can work out what I am trying to do from my code and point out where I went wrong.

    Here is the code:
                    //bounce off right edge
                    else if (transPos.X > 1)
                    {
                        Vector3 lineTest = Vector3.Transform(new Vector3(1, 0.5f, 0), lines[i2].FullTransform);
                        Vector3 endToBall = ballPosition - lineTest;
                        if (endToBall.Length() < ballRadius)
                        {
                            endToBall.Normalize();
                            ballPosition = lineTest + endToBall * ballRadius;
                            double angle = Math.Atan2(lineTest.Y, lineTest.X);
                            Matrix angleTransform = Matrix.CreateRotationZ((float)angle);
                            Vector3 transSpeed = Vector3.Transform(ballSpeed, Matrix.Invert(angleTransform));
                            transSpeed.Y *= bounce;
                            ballSpeed = Vector3.Transform(transSpeed, angleTransform);
                        }
                    }

    So basically I am asking for ball on point physics.

  • 04/11/2009 19:15 In reply to

    Re: 2D Ballz and Linez physicz

    You can't really reflect off of a point; it doesn't have a normal. My best recommendation would be to negate the ball's speed when you hit the tip of the line segment, rather than reflecting it from a surface as you've been doing.

    I'm not sure why you decided to use matrices for 2D refection collisions, but it seems to be working for you. Since you seem to be asking about the edge case of hitting the line segment's endpoints, I would recommend checking that the distance between the ball (let's call it C) and the endpoint (P) is less than the ball's radius (R). If so, simply negate the ball's velocity (V), rather than trying to reflect it using your matrices. So, something like:
    if (Vector2.Distance(C, P) < R) 
        V = -V 

  • 04/11/2009 19:24 In reply to

    Re: 2D Ballz and Linez physicz

    My suggestion is to take into account the dot product of the ball's velocity and the line's normal. If the ball is heading straight at the line, the dot product will give you a -1. As you come more from the side of the line (or rather in a path parallel to the line), the dot product will approach 0. You can use that value in your equation such that the closer to a perpendicular collision you have, the more of the line's normal that is taken into account. If the ball is traveling alone the line's direction, the dot product is 0 and the ball's velocity should simply be negated.
  • 05/11/2009 4:46 In reply to

    Re: 2D Ballz and Linez physicz

    Let me clarify, I am basically trying to bounce a circle, off of another circle where the second circle doesn't move at all and has a radius of 0.
  • 05/11/2009 7:24 In reply to

    Re: 2D Ballz and Linez physicz

    Teh1337Bix:
    Let me clarify, I am basically trying to bounce a circle, off of another circle where the second circle doesn't move at all and has a radius of 0.

    What you're trying to do doesn't really make sense to me.  You're trying to do physics on an infinitely small object.  The question is, do you just want the circle to reverse direction when it hits the point, or do you want the point's velocity to factor in to?  Regardless, I think doing what was suggested and using the dot product to the line's normal would give you the best results.
  • 05/11/2009 11:14 In reply to

    Re: 2D Ballz and Linez physicz

    Okay, its basically circle collision and reflection, except one circle doesn't move. Forget the infinitely small object thing, all a radius of zero does is make the collision system not take into account the radius of the other circle, for example:

    // if circle a is the ball and circle b is the point.
    if((circleB - circleA).Length < circleAradius ) // as opposed to if((circleB - circleA).Length < circleAradius + circleBradius )
    {
    blah blah blah blarg
    }

    Don't worry, I think I have found a good resource at http://freespace.virgin.net/hugo.elias/models/m_snokr.htm
    I'll find out when I go to make the program tomorrow, to late now.

    EDIT: Yes worry!
    I need some one to explain circle physics to me! I don't get how this (found on the webpage):

    "Now, the impulse is multiplied by the impact speed and the masses:

        impulse = impulse * square_root(ImpactSpeed * mass1 * mass2)"

    Can help find the reflection of a circle.
  • 05/11/2009 21:01 In reply to

    Re: 2D Ballz and Linez physicz

    Whats wrong with the dot product solution that was suggested earlier?

    I'd probably just calculate the tangent line of the collision (vector between ball's center and corner point of the line, rotated by pi/2)  and then reflect the ball from that.
  • 07/11/2009 10:41 In reply to

    Re: 2D Ballz and Linez physicz

    "Whats wrong with the dot product solution that was suggested earlier?"

    I don't get it.
Page 1 of 1 (8 items) Previous Next