Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
Posted 15 July 2012 - 12:20 PM
Posted 15 July 2012 - 06:04 PM
Posted 16 July 2012 - 01:17 AM
Posted 16 July 2012 - 03:49 AM
Edited by Inferiarum, 16 July 2012 - 07:20 AM.
Posted 16 July 2012 - 06:39 AM
private void collisionResponse(Ball b) { // TODO: Work more on ball-to-ball collision response. double theta = Math.atan2(this.position[1] - b.position[1], this.position[0] - b.position[0]); double sin = Math.sin(theta); double cos = Math.cos(theta); double p1 = b.speed[0] * cos + b.speed[1] * sin; double p2 = -b.speed[0] * sin + b.speed[1] * cos; double P1 = this.speed[0] * cos + this.speed[1] * sin; double P2 = -this.speed[0] * sin + this.speed[1] * cos; double pAfter = p1 + 2 * P1; double PAfter = 2 * p1 + P1; sin = Math.sin(-theta); cos = Math.cos(-theta); b.speed[0] = (float) (pAfter * cos + p2 * sin); b.speed[1] = (float) (-pAfter * sin + p2 * cos); this.speed[0] = (float) (PAfter * cos + P2 * sin); this.speed[1] = (float) (-PAfter * sin + P2 * cos); } private void collisionResponse2(Ball b) { double xVelocity = this.speed[0] - b.speed[0]; double yVelocity = this.speed[1] - b.speed[1]; double xDist = this.position[0] - b.position[0]; double yDist = this.position[1] - b.position[1]; double distSquared = xDist * xDist + yDist * yDist; double dotProduct = xDist * xVelocity + yDist * yVelocity; if (dotProduct > 0) { double collisionScale = dotProduct / distSquared; double xCollision = xDist * collisionScale; double yCollision = yDist * collisionScale; b.speed[0] += xCollision * 10; b.speed[1] += yCollision * 10; this.speed[0] -= xCollision * 10; this.speed[1] -= yCollision * 10; } }
Posted 16 July 2012 - 06:47 AM
o3o
Posted 16 July 2012 - 07:03 AM
void resolveCollision(Ball &a, Ball &b) { vec2 diff = a.position-b.position; double dist = length(diff); double penetration = max(0, a.radius + b.radius - dist); a.position += 0.5*penetration*diff/length; b.position -= 0.5*penetration*diff/length; }Then you just apply that iteratively to all colliding pairs of balls and either have "multi collision" being sorted out over multiple time steps, or iterate this until there is no collisions anymore.
Edited by japro, 16 July 2012 - 07:14 AM.
Posted 16 July 2012 - 07:51 AM
Posted 16 July 2012 - 09:56 AM
Posted 17 July 2012 - 07:18 AM
Not sure why everyone is so fascinated by working with angles. It usually makes things more complicated. Anyway a simple collision resolution for this case is to simply offset each ball by half the penetration.
void resolveCollision(Ball &a, Ball &b) { vec2 diff = a.position-b.position; double dist = length(diff); double penetration = max(0, a.radius + b.radius - dist); a.position += 0.5*penetration*diff/length; b.position -= 0.5*penetration*diff/length; }Then you just apply that iteratively to all colliding pairs of balls and either have "multi collision" being sorted out over multiple time steps, or iterate this until there is no collisions anymore. This doesn't handle velocity yet but I didn't really get how you want that resolved anyway? just cancel out relative movement? elastic collision
Is there a reason you don't want to use a physics library for this?
I'm not really experienced programmer but heres' what I would do: 1. I would find the center of the ball and the radius 2. When a ball move I would loop through all balls and check if the ball 1's radius + ball 2's radius < magnitute(ball 2's pos - ball 1's pos) 3. If the balls are moving with speed higher than ball 1's diameter + ball 2's diameter, I would make the same checks like in 2 but this time with half the current speed (creating temp location and then checking for collisions... also this is bad idea to be hard coded. I mean divide ur speed by the diameter to find out how many checks u need and use loop to check all needed positions so u are sure u won't miss the ball) 4. if collision occurs ( 2. returns true ), get the reverse vector and find out the best position so they don't overlap. Lame try to show u whats in my mind..
Posted 01 August 2012 - 09:51 AM
public void handleCollisions(){ double xDist, yDist; for(int i = 0; i < balls.size(); i++){ Ball A = balls.get(i); for(int j = i+1; j < balls.size(); j++){ Ball B = balls.get(j); xDist = A.getCenterX() - B.getCenterX(); yDist = A.getCenterY() - B.getCenterY(); double distSquared = xDist*xDist + yDist*yDist; //Check the squared distances instead of the the distances, same result, but avoids a square root. if(distSquared <= (A.radius + B.radius)*(A.radius + B.radius)){ double xVelocity = B.xVel - A.xVel; double yVelocity = B.yVel - A.yVel; double dotProduct = xDist*xVelocity + yDist*yVelocity; //Neat vector maths, used for checking if the objects moves towards one another. if(dotProduct > 0){ double collisionScale = dotProduct / distSquared; double xCollision = xDist * collisionScale; double yCollision = yDist * collisionScale; //The Collision vector is the speed difference projected on the Dist vector, //thus it is the component of the speed difference needed for the collision. double combinedMass = A.mass + B.mass; double collisionWeightA = 2 * B.mass / combinedMass; double collisionWeightB = 2 * A.mass / combinedMass; A.xVel += collisionWeightA * xCollision; A.yVel += collisionWeightA * yCollision; B.xVel -= collisionWeightB * xCollision; B.yVel -= collisionWeightB * yCollision; } } } } }
Edited by tom_mai78101, 01 August 2012 - 09:52 AM.
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
GameDev.net™, the GameDev.net logo, and GDNet™ are trademarks of GameDev.net, LLC.