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

Joined
Aug 30, 2024
Messages
10
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
186
Reaction score
26
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
 
Joined
Sep 21, 2022
Messages
186
Reaction score
26
Personally, if I was writing a game, I'd have the player specify the velocity, rather than the mass and force.

But if I had to, I guess I'd need a ramp, at least.

Pull the mass up, using the specified force. Gravity would slow it down, depending on the ramp angle.

I'd be guessing that I could add the pull and gravity vectors, then cancel out any force perpendicular to the ramp.

From the ramp length, calculate when the mass exits, then calculate the velocity at that time.

I'm thinking about the rule of YAGNI.

By the way, Microsoft gave away a free program called Gorillas with its QBasic interpreter. Gorillas on buildings, throwing bananas at each other. The math was sound.
 

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

Forum statistics

Threads
474,039
Messages
2,570,376
Members
47,032
Latest member
OdellBerg4

Latest Threads

Top