How do you make a ball not lose speed when colliding

Official forum for the Chipmunk2D Physics Library.
Post Reply
hoan
Posts: 5
Joined: Tue Jul 01, 2008 9:37 pm
Contact:

How do you make a ball not lose speed when colliding

Post by hoan »

Hey there,

How can you make a ball not lose speed when colliding, and only change direction? Here is my snippet of code that is in the collision callback function...

Code: Select all

		cpBody *body1 = a->body;
		cpBody *body2 = b->body;
		cpVect v1 = body1->p;
		cpVect v2 = body2->p;
		cpFloat dy = v1.y - v2.y;
		cpFloat dx = v1.x - v2.x;
		cpFloat angle = atan2(-dy, dx);

		cpBodySetAngle(body1, angle*57.29582);
		return 0;
From the logs, I can tell the mathematical angle is correct, however the balls go right through each other. My guess is that cpBodySetAngle isn't the right thing to use. Any help would be nice.

Thanks
Hoan
Michael Buckley
Posts: 46
Joined: Tue Aug 21, 2007 10:30 am
Contact:

Re: How do you make a ball not lose speed when colliding

Post by Michael Buckley »

cpBodySetAngle will rotate the ball around the center. It will not rotate the direction of the ball. It looks like the collision function is returning 0, which means "do not collide." Chipmunk will automatically calculate the angle at which the balls collide and take care of setting the proper velocities and accelerations for both balls if the function returns 1. You don't have to calculate the angle yourself.
hoan
Posts: 5
Joined: Tue Jul 01, 2008 9:37 pm
Contact:

Re: How do you make a ball not lose speed when colliding

Post by hoan »

Okay, so cpBodySetAngle does the wrong thing. I just want ball 1 to change direction, but keep the same velocity. I don't want ball 2 to be affected... is there a function that can do this? I'm returning 0 because I want to process the collision myself, however is it better to return 1, and then change the velocity after cpSpaceStep finishes?

Thanks for your time
Hoan
hoan
Posts: 5
Joined: Tue Jul 01, 2008 9:37 pm
Contact:

Re: How do you make a ball not lose speed when colliding

Post by hoan »

Okay,

I now have this which kinda works...

Code: Select all

		cpBody *body1 = a->body;
		cpBody *body2 = b->body;
		cpVect v1 = body1->p;
		cpVect v2 = body2->p;
		cpFloat dy = v1.y - v2.y;
		cpFloat dx = v1.x - v2.x;
		cpFloat angle = atan2(-dy, dx);

		body1->v = cpvrotate(cpvforangle(angle*57.29582), body1->v);
The problem is sometimes ball1 will get "stuck inside" ball 2 for a few seconds or more before moving onwards.
hoan
Posts: 5
Joined: Tue Jul 01, 2008 9:37 pm
Contact:

Re: How do you make a ball not lose speed when colliding

Post by hoan »

I've now come up with a better solution, outside the collision function. After every cpSpaceStep I rescale the length of each vector to the default length.

Code: Select all

		cpBody *body = shape->body;
		cpFloat defaultLength = cpvlength(cpv(5.0, 4.0));
		cpFloat currentLength = cpvlength(body->v);
		cpFloat scaleBy = defaultLength/currentLength;
		body->v = cpvmult(body->v, scaleBy);
		cpBodyUpdatePosition(body, kChipmunkSpaceStep);
However these balls with a constant velocity still push other objects around. How can I make these balls with constant velocity not affect any other objects and just change direction?

Thanks
Hoan
Michael Buckley
Posts: 46
Joined: Tue Aug 21, 2007 10:30 am
Contact:

Re: How do you make a ball not lose speed when colliding

Post by Michael Buckley »

If you don't want the balls to affect non-ball objects, you can either filter the collisions using the collision pair function or put the balls on their own collision layer.
hoan
Posts: 5
Joined: Tue Jul 01, 2008 9:37 pm
Contact:

Re: How do you make a ball not lose speed when colliding

Post by hoan »

I don't know if I made myself clear. There are 2 types of balls in the game, a and b. When ball a hits ball b, I want ball a's velocity to remain the same, but its direction to change. I also want ball b's angle and velocity not to change.

So I've also tried setting the mass of ball B to INFINITY and ball A to 0, but nothing changes. Any ideas?
Post Reply

Who is online

Users browsing this forum: No registered users and 29 guests