What math do I need to make physics based projectiles?

Something like this: Home - Roblox
It must be raycast based.

Just a link in the right direction would be ace. I can handle all the CFrames for rotations and positions, but I’m interested in speeds and angles.

Thanks!

2 Likes

It’s a pretty basic approach but in theory you could handle the vertical angle somewhat like the following code. The change in angle would be constant, however you could use a fancy equation to improve that.

local DROP_SPEED = 100 -- degrees per second
local END_ANGLE = 180 -- straight down

local StartAngle -- determined by where you shoot

local AngleDifference = END_ANGLE- StartAngle
if AngleDifference < 0 then
    -- make end negative so it lerps straight to the end angle (anticlockwise) instead of going the long way
    AngleDifference = (-END_ANGLE) - StartAngle
end

local TotalTime = AngleDifference / DROP_SPEED -- total time to get from start angle to end angle
local StartTime = tick()

-- every frame (or 0.3 seconds or whatever you like)
    local CurrentAlpha = (tick() - StartTime) / TotalTime -- apply your fancy equation here
    local CurrentAngle = lerp(StartAngle, EndAngle, CurrentAlpha)
    -- actually change the angle of the part

I don’t do much of this stuff so I’m not 100% sure it’ll work good but it seems to work out in my head :wink:

3 Likes

The easiest equation you can use to simulate projectiles is p = p0 + vt + 0.5 * a * t * t

where
p = new position
p0 = starting position
v = velocity
a = acceleration (due to gravity, so negative Y axis)
t = time elapsed (starting at 0)

8 Likes

The most commonly used equations for this type of application are called SUVAT equations Equations of motion - Wikipedia or perhaps a more friendly article https://www.quora.com/What-are-the-SUVAT-equations​. The equation Polymorphic posted is one of these.

5 Likes

Making a combat game with ranged weapons? FastCast may be the module for you! Taking a look at that source could be of help.

2 Likes

Okay so I got it moving nicely using @Polymorphic’s and @magnalite’s SUVAT suggestions (can’t believe I forgot about those xd).

Issue I’m having now is getting the direction it should come out at. Like if I click, I want it to come out at the angle I clicked at, if that makes sense?

Edit: Good chance I was just dumb and can simply raycast and then get the unit vector between where I clicked and the humanoidrootpart.

1 Like

You’d want to do it from the muzzle of the gun (or wherever your projectile actually comes from). But yeah, that’s a fine method.