Game Development Community

Imparting momentum from player movement

by John Doppler Schiff · in Technical Issues · 07/12/2006 (7:52 pm) · 3 replies

Hi guys,
I'm monkeying around with the rigid shape class, and I've run into a snag when using applyImpulse().

My test application has a bunch of "soccer balls" lying around. When the player collides with them, I'd like the balls to bounce away from the player.

Most of the solutions I've been looking at rely on the player's getEyeVector() or getForwardVector(), but that only moves the balls in the direction the player is facing. If the player backs into a ball, the ball attempts to bury itself in the player's buttocks, which isn't the effect I'm going for.

I've tried using the player's getVelocity(), but the ball flies off in strange directions. Do I need to convert from the players' axes to world axes, or am I completely off-track? (I'm severely math-impaired, so I'm guessing the latter.) =)

In the player's onCollision function, modifying code from rigid shape thread mentioned above:
if (%col.getDataBlock().category $= "PhysicsTest") {
	
		messageAll(NULL, "Hit object with velocity " @ %obj.getVelocity());
	
		// Apply an impulse to the object we collided with      
		%dir = %obj.getVelocity();      
		%vec = vectorScale(%dir, 2);
	
		// Add a vertical component to give the item a better arc      
		%dot = vectorDot("0 0 1",%dir);      
		if (%dot < 0)         
			%dot = -%dot;      
		%vec = vectorAdd(%vec,vectorScale("0 0 2",1 - %dot));      
	
		// Set the objects position and initial velocity      
		%trans = %col.getTransform();      
		
		// Heres the position and rotation.      
		%pos = getWords(%trans, 0, 2);      
		%col.applyImpulse(%pos,%vec);

Any help you can provide would be greatly appreciated!

-- JohnDopp

#1
07/12/2006 (8:44 pm)
Hey John -

for starters maybe delete the "add a vertical component" stuff until you've got the rest working.

then, i'd try using "0 0 0" for the %pos in the final applyImpulse. - i'm not sure what the %pos is supposed to be, but i think it might be relative %col's position, in which case "0 0 0" would be a good approximation.

if it still behaves strangely,
try hard-coding %vec to things like "1 0 0" and "0 1 0" to see if it behaves as expected.

may the impulse be with you!
#2
07/12/2006 (10:15 pm)
@Orion: Ah! I didn't realize the first parameter to applyImpulse was the position relative to the object... I thought it was world position.

I set %pos to "0 0 0", which is theoretically the object's center of mass, and dropped the vertical component. In my test room, I've set up "rocks" and "balloons" with various masses, friction coefficients, etc.

The rocks behave pretty well, but the balloons spiral crazily out of control -- however, both initially fly in the correct direction, so I think it's just down to tweaking mass and drag and such to get more realistic results.

Thanks for the assist! I'll post the adjusted code when I get the kinks worked out.

-- JohnDopp
#3
07/12/2006 (10:35 pm)
Awesome John! glad i could help. and also i wasn't sure about the %pos thing, but now i am!