Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

arcticBear

a good equation for a ball bouncing off a wall (straight or angled)?

This topic is 5732 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

im trying to make a simple program where i can draw lines on a screen, where im basically making a bunch of walls..and once im done, a ball is released and basically bounces all over the place, between the walls, wherever (it never loses its speed though, it will move at constant speed).. but i dont want the ball to bounce off a wall the old-fashioned simple way, such as just making the X and Y values negative.. like i want the ball to bounce off the walls based on the angle its currently moving at and also the angle of the wall.. each line segment or wall is really just two points..the line itself is just made just from drawing a line between the two points(duh) anyone know a good equation or algorithm to help me out? btw u dont have to take into consideration the curved nature of the ball, its really just going to be a pixel with an X/Y coordinate.. [edited by - arcticbear on March 30, 2003 7:14:59 PM]

Share this post


Link to post
Share on other sites
Advertisement
Well, the wall exerts a force on the ball along the normal vector of the collision. So, figure out what coefficient of restitution you want, then try:

impulse = -(1+restitution) * relative normal velocity/(1/m1 + 1/m2)
Where m1 and m2 are your object masses, and relative normal velocity is the dot of the collision normal and relative velocity of the objects(which is v1 -v2 w/o rotation, gets more complicated w/ rotation).

Then apply this impulse as delta v1 = impulse * collisionNormal / m1 and delta v2 = -impule * collisionNormal /m2

Since one of your objects is immobile, don''t update it, and 1/m for is it 0, while m for it is infinity. Restitution should be 1 for perfectly elastic collisions, and 0 for inelastic collisions.

If you want to know more, check out physics for game developers, by david eberly(but for god''s sake, don''t use his code!)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
>If you want to know more, check out physics for game developers, by david eberly(but for god''s sake, don''t use his code!)

Why not? It works fine.

Share this post


Link to post
Share on other sites
Actually, it doesn''t. Have you run his later examples, like the one where the car hits a box? The whole system blows up!

Share this post


Link to post
Share on other sites

2D vector ops.


  
struct Vector2
{
float x, y;

Vector2(float _x, float _y): x(_x), y(_y) {}

Vector2& operator +=(const Vector2& V)
{ x += V.x; y += V.y; return *this; }

Vector2& operator -=(const Vector2& V)
{ x -= V.x; y -= V.y; return *this; }

Vector2& operator *=(float k)
{ x *= k; y *= k return *this; }

float operator * (const Vector2& V) const
{ return x * V.x + y * V.y; }

float Length(void) const
{ return sqrt(x*x + y*y); }

Vector2 UnitVector(void) const
{ Vector2 Temp = *this;
float l = Length();
if (Length < 0.000001f) return Temp;
return Temp * (1.0f / l);
}

Vector2 operator + (const Vector2& V) const
{ Vector2 Temp = *this;
return Temp += V;
}

Vector2 operator - (const Vector2& V) const
{ Vector2 Temp = *this;
return Temp -= V;
}

Vector2 operator * (float k) const
{ Vector2 Temp = *this;
return Temp *= k;
}

Vector2 friend operator*(float k, const Vector& V)
{ return V * k; }
};



  
struct cSphere2
{
Vector2 P;
Vector2 V
float rad;

// bounce off a plane with normal D and param d

// Ax + By + D = 0, where (A = N.x, B = N.y, D = d).

void BounceOnPlane(const Vector2& N, float d)
{
// distance of ball to the plane

float colldepth = ((P * N) - rad) - d;

// ball too far away from plane

if (colldepth < 0.0f)
return;

// ball separating from plane

if (V * N > 0.0f)
return;

// move ball away from plane

const float damping = 0.3f;
P -= (colldepth * damping) * N;

// veloctiy of ball along plane normal

Vector2 Vn = (V * N) * N;

// velocity of ball in plane

Vector2 Vp = V - Vn;

// new vel with friction and restitution

const float CoR = 1.0f, CoF = 0.0f;
V = -Vn * Cor + Vp * Cof;
}
};


you''ll need to find the collision intersections first before doing the bounce, as your edges don''t extend to infinity, just testing against infinite planes won''t do.

you can also use the velocity to detect collision, but that''s a little more complicated, and there are some floating point accuracy problems.

Share this post


Link to post
Share on other sites
You need to mirror the speed vector of your ball. Use the search function for (mirror vector).

Cédric

Oh, search function, how we missed thee.

Share this post


Link to post
Share on other sites
For reflection collision:

Given velocity vector V and line normal N (unit vector pointing away from line), reflection velocity = vectorV - 2 * (vectorV dot vectorN) * vectorN

Don''t forget that you can collide with the endpoints, too. The normal for that collision is drawn from the endpoint to the center of the ball (if you are using a radius), otherwise it''s the same calculation as above. The endpoint collisions are important if you are bouncing around off from convex polygons and could collide with a sharp point.

Share this post


Link to post
Share on other sites
quote:
Original post by sjelkjd
Actually, it doesn''t. Have you run his later examples, like the one where the car hits a box? The whole system blows up!



I think you''re talking about David Bourg''s "Physics for Game Developers" book, not Dave Eberly''s "Game Physics" book, which is not yet published. Dave Eberly''s code is much better.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!