Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What's the formula for firing a projectile to a certain point?

Asked by 10 years ago

So, I want to fire off a bullet from a gun and make it travel towards where the mouse is pointing at a certain rate of studs per second using the BodyForce object. Through this formula that I figured out through tons of testing and research:

((target - start).unit*studspersecond + gravity)*bulletmass

And with that, I created this:

((player.Character.Humanoid.TargetPoint - bullet.CFrame.p).unit*300 + Vector3.new(0,196.2,0))*bullet:GetMass()

Is this the correct formula? *Would subtracting gravity out of the equation make it more realistic? *I'm not very sure if I pulled this off correctly.

2 answers

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

BodyForces provide a constant force or acceleration. That means they correspond to a linear change in velocity.

Gravity is also a linear change in velocity. You can cancel out the gravity using a body force of Vector3.new(0, 196.2, 0) * bullet:GetMass().

That way, the bullet will float mid-air. Or, if you push it, it will move only in the direction that you push (and not also downward).

The direction you ultimately want it to move toward is the target point. That direction (or displacement) is finish - start, the TargetPoint - bullet.CFrame.p.

Using .unit will make it ignore the current distance to that target (which is reasonable - bullets don't go faster when pointed at things further away).

Forces are vectors, so you can add them and get the result of both forces being applied. Adding the gravity to this negates gravity while accelerating it in the same direction.


This all said, BodyForces don't provide a constant rate of motion (in terms of a constant speed). You can use a BodyVelocity for that; in that, you would ignore the gravity calculation since it would attempt to maintain that velocity even with gravity.


To be completely realistic, though -- remember that bullets aren't propelled while midair. They only have an initial velocity.

You could just use the (finish - start).unit * largenumber to set the Velocity of the bullet and not use any Body-Objects. It will arc downward (the same way that bullets do) because of gravity.

To adjust for that, you can adjust the direction slightly so that it would land at the right place, but that is somewhat non-trivial.

0
Thanks! I'm learning some good stuff from your post. I'll be sure to look up some of the stuff you mentioned (like initial velocity, and how guns ACTUALLY fire off bullets). evolvedpikachu 40 — 10y
0
^Physics class also helps :P Redbullusa 1580 — 10y
Ad
Log in to vote
-3
Answered by 10 years ago

I guess I'll actually put the formula here...

bullet.Velocity = (mouse.hit.p - Player.Character.Head.Position).unit*100 + (Vector3.new(math.random(-5, 5),math.random(-5, 5),math.random(-5, 5)))
local float = Instance.new("BodyForce")
float.force = Vector3.new(0, missile:getMass() * 196, 0)
float.Parent = bullet

If this isn't what you mean then I'm sorry I misunderstood.

Answer this question