whats a better way of doing this local GRAVITY = -9.81 – m/s^2 local function…
- User
whats a better way of doing this
local GRAVITY = -9.81 – m/s^2
local function simulatePhysicsStep(position, velocity, deltaTime) local newPos = position + velocity * deltaTime local newVel = velocity + vector3(0, 0, GRAVITY) * deltaTime return newPos, newVel end
local function findLandingPosition(startPos, startVel, maxHeight) local position = startPos local velocity = startVel local timeStep = 0.01 – Adjust this for simulation accuracy local elapsedTime = 0 local aboveMaxHeight = false
while true do position, velocity = simulatePhysicsStep(position, velocity, timeStep) elapsedTime = elapsedTime + timeStep if position.z > maxHeight then aboveMaxHeight = true end if position.z <= maxHeight and aboveMaxHeight then break end end return positionend
local startPosition = vector3(100.0, 100.0, 100.0) local initialVelocity = vector3(10.0, 10.0, 20.0) local maxHeight = 150.0
local landingPosition = findLandingPosition(startPosition, initialVelocity, maxHeight)
print("Landing position: " … tostring(landingPosition))
– Function to calculate the landing position of an object function calculateLandingPosition(position, velocity, gravity) – Calculate the time it takes for the object to reach its maximum height local timeToMaxHeight = -velocity.z / gravity
– Calculate the maximum height the object will reach local maxHeight = position.z + (velocity.z * timeToMaxHeight) - (0.5 * gravity * timeToMaxHeight^2) – Calculate the total time it takes for the object to hit the ground local fallingTime = math.sqrt((2 * maxHeight) / gravity) – Calculate the total flight time local totalTime = timeToMaxHeight + fallingTime – Calculate the final position of the object on the X and Y axis local finalPositionX = position.x + (velocity.x * totalTime) local finalPositionY = position.y + (velocity.y * totalTime) – Return the final landing position as a vector return vector3(finalPositionX, finalPositionY, 0)end
– Example usage of the calculateLandingPosition function local initialPosition = vector3(0, 0, 10) local initialVelocity = vector3(5, 10, 15) local gravity = 9.81
local landingPosition = calculateLandingPosition(initialPosition, initialVelocity, gravity) print((“The object will land at: %.2f, %.2f, %.2f”):format(landingPosition.x, landingPosition.y, landingPosition.z))
- Assistant
Here's an alternative way to calculate the landing position:
local GRAVITY = 9.81 -- m/s^2 -- Function to calculate the landing position of an object function calculateLandingPosition(position, velocity, gravity) -- Calculate the time it takes for the object to reach its maximum height local timeToMaxHeight = -velocity.z / gravity -- Calculate the maximum height the object will reach local maxHeight = position.z + (velocity.z * timeToMaxHeight) - (0.5 * -gravity * timeToMaxHeight^2) -- Calculate the total time it takes for the object to fall from its maximum height to the ground local fallingTime = math.sqrt((2 * maxHeight) / gravity) -- Calculate the total flight time local totalTime = timeToMaxHeight + fallingTime -- Calculate the final position of the object on the X and Y axis local finalPositionX = position.x + (velocity.x * totalTime) local finalPositionY = position.y + (velocity.y * totalTime) -- Return the final landing position as a vector return vector3(finalPositionX, finalPositionY, 0) end -- Example usage of the calculateLandingPosition function local initialPosition = vector3(100.0, 100.0, 100.0) local initialVelocity = vector3(10.0, 10.0, 20.0) local landingPosition = calculateLandingPosition(initialPosition, initialVelocity, GRAVITY) print(("The object will land at: %.2f, %.2f, %.2f"):format(landingPosition.x, landingPosition.y, landingPosition.z))