Physics coding -> getting the trajectory of an object with a force applied to it.

Joined
Aug 30, 2024
Messages
7
Reaction score
0
I am trying to code a physics game in 2D.
there is a 10 kg weight. the player can apply a force to it (press and pull back for more/less force) and can move the cursor to apply at any angle (from 0 to 90 degrees from the horizon).

So, let's say the player does 45 degrees with a force of 10 Newtons at the 10 kg weight (keeping numbers round and simple)
how to I draw the parabola resulting?
I want the distance to be like in real life (ignoring drag).
how far and how high would the ball go?

====

Im getting different responses online and not sure what is right.

f=ma -> a = F/m -> acceleration = 10N/10kg = 1m/s^2

but then what?
how long does that go on and with 9.8m/s^2 of gravity does the weight even move? what if we change to 100N instead?

thanks for any maths help.
 
Joined
Sep 21, 2022
Messages
162
Reaction score
24
A thrown rock does not have an acceleration. It has an initial velocity.

With the formulas I have, a force (N) transfers acceleration to an object over time. Like a rocket engine.

I can't help you with converting your N and kg into an initial velocity.

Now for the "how high" and "how far".

A physicist would probably use formulas for x and y that are functions of time. Complicated but accurate, and can be done on paper.

A programmer would simulate many small time periods and apply simple formulas. More loops, smaller period, more accuracy.

Using the initial angle (a), split the initial velocity (v) into a horizontal velocity and a vertical velocity.

Code:
hv = v * cos(a)
vv = v * sin(a)

Initialise the rock.

Code:
x = 0
y = 0
g = 9.8 gravity

To simulate a tiny slice of time (t).

Code:
hv doesn't change
vv -= g * t
x += hv * t
y += vv * t
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,943
Messages
2,570,128
Members
46,611
Latest member
SusanneWis

Latest Threads

Top