How hard to kick a ball

Started by
6 comments, last by BlackEye 20 years, 1 month ago
Guys, I may be been dumb here, i may not, but suspect I am. I need to know how hard a player should kick a ball to travel a set distance. I have availible the distance, say 1000 pixels. I have a negative force of friction at say 1. Which is applied to whatever my velocity is set to from the kick power each frame. Then each cycle, The usual Velocity -= friction. PlayerX = Player X + Velocity. There is acceleration, but as this object is not been pushed it recieves a set velocity(ie the kick), acceleration is not applicable here. I am using time based movement , so my elapsed time is multipled to my Velocity each frame, will this also need to be accounted for? Thanks for any answers, and please no name calling!
Advertisement
Use the good ol'' equation:

v2 = u2 + 2as

You have to remember that you''re unique, just like everybody else.
If at first you don't succeed, redefine success.
I advise that to start with you set up a basic phisical framework:
For your object, you need to know the forces acting on it, its velocity and its mass.

There are two ways of "kicking" a ball: 1 the Proper physicsl way, 2 the simulation way
If you are writing a game without a deteicated physics engine I would advise method 2, method 1 Involved impulses etc.

For method 1, you need to know the force that the ball experiences and how long that force lasts for.
Newton said that a force is equal to a change in momentum over time, basicaly you use this to get your ball moving, by a appliting a force for a fixed time you give your ball a set momemtum and thefore a velocity.

For method 2, just asign your ball a velocity which is what you did...

Okay so you have now got your ball going,
as you say it slows down by friction, i.e. it decellerates,

Now here is your tricky bit.

Yet again there are two ways to do it:
1) If you have a set frame rate its easy
2) No fixed frame rate you need to know the time since the ball started.

1) If you have a fixed frame rate you do what you do and just subtract the acceleration due to your frictional force each frame.

2) If you do not have a fixed frame rate:
If you know its starting velocity: the distance the ball has traveled will be:
 s = ut + 0.5at^2  where s is your distance, u is original velocity, a is your decellertaion (as a negative number) and t is your time. 


(As you do not have a fixed frame rate, you can never know easily exactly what speed that ball is traveling, so I advise you base the ball position on distance after a cirtain time)

From this frame work you can now work out your range:
To get the velocity for a set distance, use:
 v = sqrt(2as) where a is accelation and s is distance  


this will give you the velocity to kick a ball to move distance s


Remember: all these equations only work in standard units, unit distance per time for velocity etc, you might have to tweak your friction code...

Hope that helps, I expect most of it is irrelevent, but it should come in useful for designing a nice physics engine.

SonOfNed

---------

Does it matter? Even if it does matter, does it matter that it matters?
---------Does it matter? Even if it does matter, does it matter that it matters?
It is important to note that friction force is always in the opposite direction to the velocity of the object. This presents problems when adding a constant force such as gravity, but in your case it probably doesn''t make much difference.
My stuff.Shameless promotion: FreePop: The GPL god-sim.
well you can use two formulas

t=2*vertical velocity/10
and than use
horizntal Velocity*vertical velocity=distance

play around with these formulas and you could do anything

(the great thing about computers are that they are like real time integraters).

ps: if you wanna use air friction just enter some number for b.
thus the force against the ball (vertical + horizantal) is v-vb
Great Thanks for the replies guys, I will have a blast when i finish work tonight.

Son of Ned I dont know what looks more mind blowing, your physics bit or your signature!

Thanks again.
pete.
Still having a littel trouble with this guys.

I have no accelereration as i am applying a set velocity and minusing teh friction each frame. Also I know the distance, sho i was thinking its some thing like..

while (not at distance)
required power += friction;

but do i then need to take note of my elapsed time as this is used in moving objects by

m_X += m_VelocityX* m_ElapsedTime;
m_Y += m_VelocityY* m_ElapsedTime;


hmm little stuck on this simple problem



Here is an Idea:

Work out ur initial speed by using v = sqrt(2as) where a is your acceleration (i.e. your friction), and s is your distance
You will also need to save the time your created the ball (i.e. time after your created the ball is time elapsed)
If you are working in 2d, do the equation twice, once on each component.

Okay, so you now have your ball and it travels is traveling at a set speed.
Now, for every frame, you will want to move your ball. Do this as follows:
For every frame, The distance past the point where the ball was created can be calculated by s = vt + 0.5at^2 where v is te velocity that you created the ball with, t is the time elapsed since your ball was created and a is yoru decelleration due to friction.

Here is a handy example (in C coz its too l8 to think in C++ )

//WARNING - UNTESTED CODE - WARNING - UNTESTED CODE - WARNING - UNTESTED CODE - WARNING - UNTESTED CODE -#define FRICTION	2typedef struct{	float xV,yV;			//x,y Velocity you make ball at	unsigned long StartTime;	//Time ball made	float ixPos,iyPos;		//Initial X,y position	float x,y;			//Current x,Y position (realtive to ixPos etc.)}BALL;/* CreateBall * Pass Pntr to Ball, x&y Distance your want ball to go, and initial starting positions... */void CreateBall(BALL *Ball,float xDist,float yDist,float xPos,float yPos){	Ball->xV=sqrt(2*FRICTION*xDist);	// Work on both components, working out	Ball->yV=sqrt(2*FRICTION*yDist);	// initial velocity	Ball->StartTime=GetTimeSomehow();	// Set start time	Ball->x=Ball->x=0;	Ball->ixPos=xPos;			//Set initial staring pos	Ball->iyPos=yPos;}/* Call every frame */void OnFrame(BALL *Ball){	float Time = GetTimeSomehow()-Ball->StartTime;	/* Use the formula s = ut + 0.5at^2, but for each component as you are in 2D	 * This gives you te position from the origin, therefore add your initila positions on,	 */	Ball->x=Ball->ixPos + (Ball->xV*Time + 0.5*FRICTION*Time*Time);	Ball->y=Ball->iyPos + (Ball->yV*Time + 0.5*FRICTION*Time*Time);} 


Right, This code is how I think it should be done. Short sweet and compact.

You will notice that the position of the ball is only based on time, this measn that tere is no need to cnage velocities etc in code and worry about for loops. This just gets called every frame and what is more...
...The code is automagicaly independedn of frame rate, it will run the same on all machines!!!!!


If you want to learn more about the equations they are known as te "equations of motion" and come in very handy for doing physics simulations (like kicking a ball...)

Enjoy!

PS would it be too nosy to ask what this code is for?

SonOfNed

---------

Does it matter? Even if it does matter, does it matter that it matters?
---------Does it matter? Even if it does matter, does it matter that it matters?

This topic is closed to new replies.

Advertisement