Corona has a great article showcasing how to predict a trajectory of a physics object:
http://www.coronalabs.com/blog/2013/04/09/physics-radial-gravity-and-predicting-trajectory/
I need to expand on that code to predict applying a linear impulse each frame.
I am wondering if anyone can give me some direction on how I can simulate applying a linear impulse on the object as it travels in the air? I have an event listener on the actual physics object that applies the linear impulse each frame. I want to be able to predict that trajectory.
Here is my basic code: (Note this code also allows linear damping)
local function getTrajectoryPoint (startingPosition, startingVelocity, n, linearDampingVar) function clamp( val, low, high ) if (val \< low) then return low end if (val \> high) then return high end return val end local dt = 1/30 stepVelocity = { x=dt\*startingVelocity.x, y=dt\*startingVelocity.y } local stepGravity = { x=dt\*xGravity, y=dt\*yGravity } d = clamp(1.0 - dt \* linearDampingVar, 0.0, 1.0); vd = 0; ad = 0; for i = 0,n do p = math.pow(d,i+1); vd = vd + p; ad = ad + ((n-i) \* p); end return { x = startingPosition.x + vd \* stepVelocity.x + ad \* stepGravity.x, y = startingPosition.y + vd \* stepVelocity.y + ad \* stepGravity.y } end local function updatePrediction( event ) Runtime:removeEventListener("enterFrame",moving); display.remove( prediction ) prediction = display.newGroup() ; prediction.alpha = 0.5 local startingVelocity = { x=xWeight, y=yWeight } for i = 1,180 do local s = { x=160, y=480 } local trajectoryPosition = getTrajectoryPoint( s, startingVelocity, i,.3) local circ = display.newCircle( prediction, trajectoryPosition.x, trajectoryPosition.y,5 ) end end
Any suggestions on where to start? I’ve racked my brains on this one.