Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Need Help with simple bouncing collison/math  (Read 1458 times)

0 Members and 1 Guest are viewing this topic.

billboard_baggins

  • Newbie
  • *
  • Posts: 9
    • View Profile
Need Help with simple bouncing collison/math
« on: February 02, 2012, 10:28:35 pm »
I feel like I'm going to make this highly convoluted so please bear with me. I'm in the process of teaching myself C++ and SFML(2.0) and am making a relatively simple game. I have multiple rocks bouncing around the screen that I want to bounce off of each other.

Right now I have them bouncing off the edges of the screen fine, and off each other when colliding along the x axis. This is really an issue of math I think (and my main problem is my math skills are very lacking). I have, in the collision detection code, the rock's angle changing using the formula: angle = Pi - angle (which works when bouncing off the left and right sides of the screen). However, my code for bouncing off the top and bottom of the screen simply inverts the angle (angle = -angle).

I know using "angle = -angle" will work for y axis rock-to-rock collisions, so I guess my question is, how do I check (once they have collided) whether to use "angle = -angle" or "angle = Pi - angle"?

julen26

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
    • http://julen26.blogspot.com
Need Help with simple bouncing collison/math
« Reply #1 on: February 03, 2012, 03:51:32 pm »
(Supposing that you are working with "balls")

Velocity is calculated with:




m = mass
u = last velocity
v = new velocity

Note that these last operations are with vectors. (Velocity is a vector, speed is scalar)

EDIT: Sorry that's for one-dimension, check this on wiki pedia for 2D and an example:



http://en.wikipedia.org/wiki/Elastic_collision

Cheers.