• Create Account

### #Actualmklynge

Posted 17 June 2013 - 12:37 PM

Ok, found something strange. Using this code:

a.vel.x += (acf - aci) * n.x;

a.vel.y += (acf - aci) * -n.y;

b.vel.x += (bcf - bci) * n.x;

b.vel.y += (bcf - bci) * -n.y;

The balls going up and down is now responding as expected (like the balls going left/right). But if a ball going down is colliding with a ball going left, all hell breaks lose. They add an insane amount of speed to each other for some reason.

I also simplifed the method that handles the collision:

public void Collision(PhysicsObject a, PhysicsObject b) {

Vector3 n = new Vector3(a.pos.x - b.pos.x, a.pos.y - b.pos.y, 0);

double d = n.length();

n.x = n.x / d;

n.y = n.y / d;

double aci = a.vel.dot(n);

double bci = b.vel.dot(n);

double acf = bci;

double bcf = aci;

a.vel.x += (acf - aci) * n.x;

a.vel.y += (acf - aci) * -n.y;

b.vel.x += (bcf - bci) * n.x;

b.vel.y += (bcf - bci) * -n.y;

}

Edit: And the update loop pr. PhysicsObject: (in case anyone was wondering if something is being added)

public void updatePhysics(long frameTime) {

oldpos.x = pos.x;

oldpos.y = pos.y;

pos.x = pos.x + (vel.x * 0.5 * frameTime);

pos.y = pos.y + (vel.y * 0.5 * frameTime);

}

### #3mklynge

Posted 17 June 2013 - 12:35 PM

Ok, found something strange. Using this code:

a.vel.x += (acf - aci) * n.x;

a.vel.y += (acf - aci) * -n.y;

b.vel.x += (bcf - bci) * n.x;

b.vel.y += (bcf - bci) * -n.y;

The balls going up and down is now responding as expected (like the balls going left/right). But if a ball going down is colliding with a ball going left, all hell breaks lose. They add an insane amount of speed to each other for some reason.

I also simplifed the method that handles the collision:

public void Collision(PhysicsObject a, PhysicsObject b) {

Vector3 n = new Vector3(a.pos.x - b.pos.x, a.pos.y - b.pos.y, 0);

double d = n.length();

n.x = n.x / d;

n.y = n.y / d;

double aci = a.vel.dot(n);

double bci = b.vel.dot(n);

double acf = bci;

double bcf = aci;

a.vel.x += (acf - aci) * n.x;

a.vel.y += (acf - aci) * -n.y;

b.vel.x += (bcf - bci) * n.x;

b.vel.y += (bcf - bci) * -n.y;

}

### #2mklynge

Posted 17 June 2013 - 12:35 PM

Ok, found something strange. Using this code:

a.vel.x += (acf - aci) * n.x;

a.vel.y += (acf - aci) * -n.y;

b.vel.x += (bcf - bci) * n.x;

b.vel.y += (bcf - bci) * -n.y;

The balls going up and down is now responding as expected (like the balls going left/right). But if a ball going down is colliding with a ball going left, all hell breaks lose. They add an insane amount of speed to each other for some reason.

I also simplifed the method that handles the collision:

public void Collision(PhysicsObject a, PhysicsObject b) {

Vector3 n = new Vector3(a.pos.x - b.pos.x, a.pos.y - b.pos.y, 0);

double d = n.length();

n.x = n.x / d;

n.y = n.y / d;

double aci = a.vel.dot(n);

double bci = b.vel.dot(n);

double acf = bci;

double bcf = aci;

a.vel.x += (acf - aci) * n.x;

a.vel.y += (acf - aci) * -n.y;

b.vel.x += (bcf - bci) * n.x;

b.vel.y += (bcf - bci) * -n.y;

}

### #1mklynge

Posted 17 June 2013 - 12:33 PM

Ok, found something strange. Using this code:

a.vel.x += (acf - aci) * n.x;

a.vel.y += (acf - aci) * -n.y;

b.vel.x += (bcf - bci) * n.x;

b.vel.y += (bcf - bci) * -n.y;

The balls going up and down is now responding as expected (like the balls going left/right). But if a ball going down is colliding with a ball going left, all hell breaks lose. They add an insane amount of speed to each other for some reason.

PARTNERS