Jump to content
  • Advertisement
Sign in to follow this  
Kaze

attack of the atari remakes

This topic is 4962 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

say i have two circles that move in 2d with realistic physics and i have public X as double public Y as double public Xv as double public Yv as double Im using that pythagoras to detect collions between two spheres but when they collide i want them to bounce realisticly , as opose to just reversing direction even on a grazing blow.

Share this post


Link to post
Share on other sites
Advertisement
You need to reflect their velocity through the collision plane, which will be perpendicular to the vector connecting the centers of the balls.

Share this post


Link to post
Share on other sites
...at the moment of impact.

float tcoll; // time of collision that you've found

// move at moment of impact
A.Pos += A.Vel * tcoll;
B.Pos += B.Vel * tcoll;

// normal of collision plane = vector separating ball centres
Vector N = B.Pos - A.Pos;

// distance between ball centres
float n = N.Length();

// normalise collision plane normal
N /= n;

// relative velocity
Vector V = B.Vel - A.Vel;

// impact velocity on the collision plane
float vn = V.Dot(N);

// balls are moving away already
if (vn > 0.0f) return;

// the collision impulse.
float Cor = 0.4f // coefficient of restitutio, or amount of 'bounce'.
V = N * (-(1 + CoR) * vn);

// apply response to balls
float ima = 1.0f / A.mass; // inverse mass of ball A
float imb = 1.0f / B.mass; // inverse mass of ball B

A.Vel -= V * (ima / (ima + imb));
B.Vel += V * (imb / (ima + imb));

Share this post


Link to post
Share on other sites
OP - could you edit the title of this post? It's title really doesn't give away that you are actually talking about collision detection! Glad I stumbled upon this though, going to track and see what people post [smile]

Share this post


Link to post
Share on other sites
kaze: as olii has done, you will probably find things are easier if you look at the vast number of things you have that have an X and a Y, and create a struct/class/whatever that contains an X and a Y, and use those instead.


class Vec2f
{
public:
float operator[](int i) { return x; }
private:
float x[2];
};

class Thingy
{
...
Vec2f mPosition;
Vec2f mVelocity;
...
};


When you get deeper into game physics, this will make things much easier, and save you quite a bit of typing. That's all a vector is, for now. Adding operators to it is nice aswell, then oliis code stops being pseudo and starts working just as it is.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!