Jump to content
  • Advertisement
Sign in to follow this  
lorddanny

C++ 3D physics on ball.

This topic is 2535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does anyone on here know how to code a ball with physics with it? Trying to make a 3D cannon game that shoots balls out. Got the ball shooting, but can't do this physics part.

currently I've got the following variables:

Vector3 velocity;
Vector3 position;
float xAngel;
float yAngel;
float time;

but can't get my head around how to code it so that the ball will fall to the ground when it is shoot from a position.

Any help would be great.

Thanks

Share this post


Link to post
Share on other sites
Advertisement
Generally, say you have a gravity vector G, representing its change to an object's velocity in one time unit, and an object with velocity V[sub]0[/sub] at time zero, then in one time unit the velocity will be V[sub]1[/sub] = V[sub]0[/sub] + G, in two time units the velocity will be V[sub]2[/sub] = V[sub]0[/sub] + 2G, ... thus at time t the velocity will be V[sub]t[/sub] = V[sub]0[/sub] + tG.

e.g. Say that a projectile is launched with velocity (1, 2, 3) and gravity pulls along the vector (0, 0, -1) at 1 unit per time step. After one time unit, the projectile's velocity will be (1, 2, 2). After two time units, the projectile's velocity will be (1, 2, 1), etc. The position of the projectile at each time unit would be its position at the last time unit added to its velocity at the last time unit. This is assuming no wind and the like.

Share this post


Link to post
Share on other sites
The starting values of your variables are:
Vector3 position - equals to the position of the ball in the cannon (thus directly related to the world position of the cannon)
Vector3 velocity - direction of this vector is given by the direction of the cannon barrel (probably your float angle variables, but it depends on how do you define those angles) and magnitude of the vector is given by the initial velocity of the ball just when it's leaving the cannon (you can change this to simulate the amount of powder in the cannon - the power of the shot).

Then you also have to define a gravity vector. If your up coordinate is +Y, the gravity would be for example:
Vector3 gravity = Vector3(0, -9.81, 0)

Notice that in this a little bit simplified case (more on this later) you don't need the mass (weight) of the ball.

And then, after the ball has been fired, you make this incredibly simple calculation every game step:

velocity = velocity + gravity * dt
position = position + velocity * dt

Where both lines are using vector operations (the variables velocity, gravity and position are vectors).
dt is a float variable and is equal to the actual time between two executions of the calculation - usually it will be equal to 1 / fps, so if you have fixed FPS of 60 frames per second, dt would be 0.01667 (seconds).

This alone will give you a nice arch trajectory of the ball. You'll just have to detect collisions somehow. In the simpliest case when your ground is flat, you'll just watch the Y coordinate of position and when it's <= 0, you'll know the ball hit the ground.


If you want a little bit more relastic trajectory, you would have to add air resistance. That's a force so you'd need to add mass of the ball. But you definitely should start without it, you always should advance step by step when coding something.

Share this post


Link to post
Share on other sites
hi guys,

Thanks for the replys. Just to make sure, so do I not need something like a "acceleration" variable aswell? I read a book (which I still don't understand) saying that to make this, acceleration will need to be included aswell... (i'm so bad at physics)

Share this post


Link to post
Share on other sites
Well the unit of your velocity vector would be "units per second" where "units" would be the unit of your coordinate system. You may also have a scale that says "one unit = one metre" for example. If we go with that scale then a (1, 2, 3) velocity vector would mean "one metre per second along x, one metre per second along y, and one metre per second along z".

Since the gravity vector is affecting velocity, then its unit would be "units per second per second" (units/second[sup]2[/sup]). So a gravity vector of (0, 0, -1) would mean "affect velocity by zero metres per second along x, by zero metres per second along y, and by -1 metres per second along z". The fact that the gravity vector is affecting the velocity vector of an object means it is applying an acceleration to the object. Applying the same gravity vector to an object's velocity vector at each time step would be giving it a constant acceleration which is how objects behave with gravity in real life.

To simulate Earth's gravity you could have your gravity vector as (0, 0, -9.8) since the constant acceleration due to gravity on Earth is 9.8 m/s[sup]2[/sup], again going with the "one unit = one metre" scale.

Also, since every frame, a second may or may not have passed you figure out the actual time between frames and adjust your calculation accordingly (this is the "dt" you see in the post above).

Share this post


Link to post
Share on other sites

hi guys,

Thanks for the replys. Just to make sure, so do I not need something like a "acceleration" variable aswell? I read a book (which I still don't understand) saying that to make this, acceleration will need to be included aswell... (i'm so bad at physics)


There was acceleration variable in my previous post. It was called gravity.

To explain it a bit more: Acceleration is caused by forces acting on your ball. If you omit air resistance (which you easily can, it's not that important after all), the only force you have is gravity. And gravity is simply represented by a constant gravitation acceleration vector pointing down.
You can call the vector acceleration instead of gravity if you want because it IS acceleration.

If you had other forces acting on your ball (including for example wind), you would somehow calculate the acceleration caused by that forces and add them all together with the gravity to get the final composed acceleration vector (which would not be pointing directly down anymore, because wind never blows down).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!