How Do I Find A Proper Midpoint Between Velocities?

Alright, so, I have a system in a game I’m working on that makes a little space ship go to different waypoints, for a fancy “animation” kinda. The animation is of it taking off and landing, so the speed fluctuates between each point. I use TweenService to tween the BodyVelocity and the BodyGyro orientation and stuff.

Here’s the problem. I need to determine how long to wait between each keypoint jump, and my current system divides the distance by the mean speed (directly between the lowest and highest), but because of mathematics and acceleration and stuff, it often overshoots the waypoints. It usually looks fine, because I calibrated it, but it sometimes has issues and clips through walls.

How can I find the optimal speed to calculate for, so that it reaches each waypoint when it should?

Video of the landing shown below. Each of the floating blue parts is a waypoint.

Code to land the spacecraft:

function go(main,pos,speed,nxt)
	--main.BodyGyro.CFrame = CFrame.new(main.Position,pos)
	local avgSpeed = (getMid(main.Velocity.Magnitude,speed))
	local duration = (main.Position-pos).Magnitude/avgSpeed
	local velocity = CFrame.new(main.Position,pos).LookVector*speed
	TS:Create(main.BodyGyro,TweenInfo.new(duration,Enum.EasingStyle.Linear),{CFrame=nxt.CFrame}):Play()
	TS:Create(main.BodyVelocity,TweenInfo.new(duration,Enum.EasingStyle.Linear),{Velocity=velocity}):Play()
	wait(duration)
end

local SHIP = nil

function getDropship()
	local ship = game.ReplicatedStorage.DroidGunship:Clone()
	ship.Parent = game.Workspace
	local main = ship.Main
	main.EngineSound:Play()
	SHIP = ship
	local points = workspace:WaitForChild("DroidGunshipPoints")
	go(main,points[1].Position,200,points[1])
	go(main,points[2].Position,185,points[2])
	go(main,points[3].Position,170,points[3])
	go(main,points[4].Position,150,points[4])
	go(main,points[5].Position,100,points[5])
	main.BodyPosition.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	main.BodyVelocity.MaxForce = Vector3.new(0,0,0)
	main.BodyVelocity.Velocity = Vector3.new(0,0,0)
	main.BodyGyro.CFrame = CFrame.new(main.Position,main.Position+Vector3.new(100,0,0))
end
1 Like

If your acceleration is constant, you can simply calculate the midpoint by (a+b)/2. However, keep in mind that a BodyVelocity is simply a controller which controls a Force on the object. This can result in your ship overshooting, resulting in it arriving at the next waypoint at a different time than you would expect.

1 Like