Jump to content
• Advertisement

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.

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

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

but I don't understand the maths behind it. Please help.

Share this post

Share on other sites
Advertisement
Every frame:

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

The same for every ball, mass does not affect it

Share this post

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 this post

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 this post

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

Share this post

Share on other sites

• Advertisement
• 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!

• Advertisement

• Popular Now

• 16
• 11
• 9
• 24
• 45
• 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!