Inverse trajectory calculation help

I have a trajectory function that takes the launch origin and velocity and simulates a trajectory.
But I want to implement this for an NPC launching a projectile and the variables available is only the origin and targetPoint.

Does anyone know how I can get the Vector3 velocity with only the Vector3 origin and Vector3 targetPoint?

Here’s a sample code of how my code looks like, I just need help with the math.

local delta = 1/15;
local acceleration = Vector3.new(0, -196.2, 0);

local function trajectory(origin, velocity)
    --Simulate trajectory
	repeat
		velocity = velocity + acceleration * delta;
		
		local rayHit, rayPoint = raycast(origin, velocity * delta);
		
		if rayPoint == nil then
			rayPoint = origin + (velocity * delta);
		end
		
		origin = rayPoint;
	until rayHit;
end


-- Reverse trajectory
local function getVelocity(origin, targetPoint)
	
	
	return velocity;
end

Thanks in advance,
Khronos

Integrating the acceleration due to gravity with respect to time, we can find the velocity, which is:

v(t) = ∫gdt = g * t + v0

Where:

  • v = instantaneous velocity (velocity at time “t”)
  • g = acceleration due to gravity
  • t = time
  • v0 = initial velocity (velocity at time = 0)

Integrating the equation we have for velocity with respect to time, we can find position, which is:

p(t) = ∫ g * t + v0dt = 0.5 * g * t^2 + v0 * t + p0

Where:

  • p = instantaneous position (position at time “t”)
  • p0 = initial position (position at time = 0)

Now, we can think of p being the target; we can transpose the equation to make v0 the subject of the formula, so that we can find the initial velocity needed so that it reaches the target:

v0(t) = (p - p0 - 0.5gt^2) / t

Putting it into code:

local t = 0.5 -- it takes 0.5 seconds to reach the "targetPoint"

local function getVelocity(origin, targetPoint)
    return (targetPoint - origin - 0.5 * acceleration * t^2) / t
end

This is amazing, the t variable this the equation is fascinatingly handy for my purpose. I’ve incorporated distance/speed to determine the travel time. How would one get the velocity if a bounce variable was introduced to the trajectory calculation?

As such

local bounce = 0.5;
repeat
	velocity = velocity + acceleration * delta;
	
	local rayHit, rayPoint, rayNormal = raycast(origin, velocity * delta);
	
	if rayPoint == nil then
		rayPoint = origin + (velocity * delta);
	end
	
	
	if rayHit then
		local unitVel = velocity.Unit;
		
		velocity = (unitVel - 2 * unitVel:Dot(rayNormal) * rayNormal) * velocity.Magnitude * math.clamp(bounce, 0, 1);
		origin = origin + rayNormal * 0.001;
	end
	
	origin = rayPoint;
until rayHit; 

This is just some extra velocity calculation that may be useful to me, but I am glad for the help you provided above. Thanks again.