Game Development Community

Soccer Ball physics

by Gustavo Boni · in Torque Game Builder · 01/25/2007 (1:45 pm) · 5 replies

Hi guys,

I'm creating a soccer game on TGB and i'm trying to figure out how to do the player "shoot" and how it can control the ball as it run. In addition, i was trying to create the soccer Ball physics. I've read the mini Tutorials for Ball Physics at TDN but i can't figure out a solution for a soccer ball.

Any help would be appreciated.

Thanks in Advance

#1
01/26/2007 (6:00 am)
I'm going to assume this is top-down.

You would probably want to have "kicks" that impart forces on the ball. A 'dribble' kick (or whatever, I don't know soccer at all) would be smaller and just push the ball a little bit. A 'shoot' kick would impart a larger force. The tricky part is figuring out how hard to push the ball and what the forces should look like. While there is no one solution, there are a few things that can make finding the proper forces easier.

Dribble kicks should put the ball somewhat in front of the player. Every so often when a player is in possession of the ball, the player should kick the ball a little bit. This should happen when the player catches up with the ball. Otherwise, the ball should be slowly losing speed and eventually stop on it's own. If a player is in possession and the ball is close enough, the player would be allowed to shoot the ball, which would be the same as dribbling, but a larger force would be applied. You'll also probably want to set the ball to BOUNCE response with a medium restitution.

If you assume that you want the ball to aim towards some number of units in front of where the player is facing and be kicked with a certain amount of force, you could get a vector that represents that direction with the following formula:

// assume that:
// %player is the player (scene object)
// %ball is the ball (scene object)
// %offset is the number of units from the player of the aiming position (float)
// %forceMagnitude is the strength of the kick (float)


// a vector that represents the direction that the player is facing
%rotation = %player.getRotation();
%directionX = mSin( mDegToRad( %rotation ) );
%directionY = -mCos( mDegToRad( %rotation ) );

// scale the vector to get the offset several units away
%offsetVector = t2dVectorMultiply(%directionX SPC %directionY, %offset);

// get the target vector based on the player's position
%target = t2dVectorSub( %offsetVector , %player.getPosition() );

// get a normalized vector from the ball to the target position
%forceDirection = t2dVectorNormalise( t2dVectorSub(%target, %ball.getPosition()) );

// scale the vector for the appropriate force to apply
%ballForce = t2dVectorMultiply( %forceDirection, %forceMagnitude );

I haven't tested this code (or worked with TorqueScript in several months), so the above code probably has some errors. Some of the function names might be wrong and the order of the vector subtraction might be reversed.. I can't remember if T2D switches them for you or not. Either way, that's the basic idea.

I hope this was helpful.
#2
01/26/2007 (8:18 am)
Thank you very much Thomas, it will help me a lot!
#3
01/27/2007 (8:51 am)
Hi Thomas, how can i apply this values to the ball? I'm a bit confused.
#4
01/28/2007 (1:26 am)
The snippet above can be used in some sort of kick function (for shots and for dribbling). You can alter the magnitude of the kick by changing %forceMagnitude and you can apply the force to the ball by calling setVelocity(%ballForce) on the ball.
#5
01/28/2007 (12:26 pm)
Thanks again Thomas! Now i got the idea.

I ceated a function for the "kick" and i call that in the ball onCollision callback. it not works very well because it is called a lot of times when the player collides with the ball. Do you have any suggestion where to call it?

Edit: Ops, i fixed that, just scaled down the collision polygon.

Regards

Gustavo Boni