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

How can I equate the curve of a projectile?

Asked by
Edenojack 171
8 years ago

I'm trying to use raycasting to create a "Thrown" object move, however I can't think of the equation to get it to reach its target.

So far, I have the follow: the value it "DROPS" at the length each ray will be the distance between the two points. the start position the target position

I can't think of an equation that will use these to create the necessary curve at the start. I've been trying to add the distance to the target's Y, but that overshoots. and multiplying it by its Drop value seems to just throw it off entirely :/ Essentially, the thrown part is moved, rotated, moved, rotated, moved rotated, to create the curve. But I need to work out how to find the initial angle.

1
Try the Quadratic equation. Dolphinous 36 — 8y

1 answer

Log in to vote
1
Answered by
EgoMoose 802 Moderation Voter
8 years ago

I know this is a late response and you and I actually talked about this specifically on twitter, but it seems worth it to answer this for anyone else who may stumble upon this question in the future.

There are two ways you can do that both of which do require a quadratic equation of some sort. You can use either matrices like I do in this video to solve for system of equations. Or you can use physics to solve for a function of time and position, which is also a quadratic equation.

The problem is both those equations don't work with vectors. So what you need to do is get two axis that you can use to multiply your equations values in to get the vector form. The x-axis is really easy to get, simply subtract do a vector subtraction and get the unit vector in the horizontal direction the projectile should be going. Recall though, you only want x and z components! The y-axis is also easy because you want only y components so you know your unit vector is going to be <0, 1, 0>.

With all that in mind:

local mouse = game.Players.LocalPlayer:GetMouse();
local character = game.Players.LocalPlayer.CharacterAdded:wait();
local hrp = character:WaitForChild("HumanoidRootPart");

local grav = -196.2;

function fire(v)
    local part = Instance.new("Part", game.Workspace);
    part.CanCollide = false;
    part.Anchored = true;

    local xaxis = (v - hrp.CFrame.p); -- example
    local yaxis = Vector3.new(0, 1, 0); 

    local sv = hrp.CFrame.p + hrp.CFrame.lookVector * 3;
    local v0 = 100;
    local x0 = 0;

    local function curve(t)
        return (grav/2) * t^2 + t * v0 + x0;
    end;

    local tfinish = (-v0 - math.sqrt(v0^2 - 4*(grav/2)*x0))/(2*(grav/2)); -- quadratic formula

    local t = tick();
    while (tick() - t) <= tfinish do
        local dt = tick() - t;
        part.CFrame = CFrame.new((sv + xaxis * dt/tfinish) + yaxis * curve(dt));
        game:GetService("RunService").RenderStepped:wait();
    end;
end;

mouse.Button1Down:connect(function()
    fire(mouse.Hit.p);
end);

It could use some smoothing over, but you get the idea hopefully

Ad

Answer this question