xna - C# Collision Programming - Momentum -
i'm programming 3d xna game , i'm struggling understand how implement momentum-like collision between 2 players. concept either player hit other , attempt knock opponent off level (like smash bros games) score point.
//declared variables primitiveobject player1body; primitiveobject player2body; vector3 player1vel = vector3.zero; vector3 player2vel = vector3.zero; //created in loadcontent() primitivesphere player1 = new primitivesphere(tgd, 70, 32); this.player1vel = new vector3(0, 135, -120); this.player1body = new primitiveobject(player1); this.player1body.translation = player1vel; this.player1body.colour = color.blue; primitivesphere player2 = new primitivesphere(tgd, 70, 32); this.player2vel = new vector3(0, 135, 120); this.player2body = new primitiveobject(player2); this.player2body.translation = player2vel; this.player2body.colour = color.red; //update method code collision this.player1vel.x = 0; this.player1vel.y = 0; this.player1vel.z = 0; this.player2vel.x = 0; this.player2vel.y = 0; this.player2vel.z = 0; if (player1body.boundingsphere.intersects(player2body.boundingsphere)) { this.player2vel.z += 10; }
as can see i'm doing checking player1's bounding sphere , when intersects player 2's player 2 pushed on z axis, wouldn't work , isn't intuitive , problem lies i'm buggered trying work out how come solution, want happening when either player collides other swap vectors giving bounce effect i'm looking , not affecting 1 axis both x & z.
thanks taking time read , i'll grateful solutions can think of.
notes: primitivesphere if you're wondering using (graphics device, diameter of sphere, tesselation).
basically, in collision 1 trying do, want elastic collision, in both kinetic energy , momentum conserved. momentum (p) is mass * velocity, , kinetic energy (k) 1/2*mass*velocity^2. in situtation, i'm assuming has same mass. if so, k = 1/2 * v^2
. so, kf = ki , pf = pi. using kinetic energy, velocity's magnitudes of players swapped. , long collisions head on (which based on code assume you're fine with), players swap directions.
so simple this:
if (player1body.boundingsphere.intersects(player2body.boundingsphere)) { vector3 p1v = this.player1vel; this.player1vel = this.player2vel; this.player2vel = p1v; }
this should create realistic collision. reason included info p , k if want non-head on collisions or different masses, should able incorporate that. if masses aren't same, velocity's won't change magnitudes , directions. there lot more math involved. hope helps.
Comments
Post a Comment