# C++ Physics on a ball

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

## Recommended Posts

Hi guys,

I'm trying to make a 3D cannon game which shoots balls out and hits targets. I have got the cannon to shoot balls out, but it only goes in a straight line. I wanted to add some physics to it so that the ball will fall to the ground and make a nice curve. How do I do that?

I have the following varibles:

Vector3 position
Vector3 velocity
Vector3 force
float mass

##### Share on other sites
Every frame:

 #define GRAVITY -0.01f velocity.y += GRAVITY 

The same for every ball, mass does not affect it

##### Share on other sites
hmm... sorry, but I don't see how that would make the curve... For example, if the ball if launched or thrown from position 0, 0, 0, the y axis value should be positive at some point (or atleast from the start), but all I can work out from that (given gravity is -0.01) is all negative values.... so confused lol

##### Share on other sites
You have to set your velocity to. for example:

pos(0,0,0)
velocity(0,1,0)

and in every update loop
velocity.y -= (0.01*time)

with this example you would throw your ball straight up and it will slow down until velocity.y < 0 then it will fall down again(don´t forget stopping it on the ground( = when pos.y == 0 again)

I hope you get it know ;)

##### Share on other sites

You have to set your velocity to. for example:

pos(0,0,0)
velocity(0,1,0)

and in every update loop
velocity.y -= (0.01*time)

with this example you would throw your ball straight up and it will slow down until velocity.y < 0 then it will fall down again(don´t forget stopping it on the ground( = when pos.y == 0 again)

I hope you get it know ;)

Thanks

Makes more sense now. Thanks again

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 11
• 9
• 24
• 45