Predicting Trajectory - Zero Gravity with linearDamping on Physics Body

Well, at the basic level, it’s a force in the opposite direction based on the damping coefficient, in meters per second. But reverse-translating that from the Box2D world could be tricky…

Did you search around on Stack Overflow? There are usually some interesting tidbits and suggestions on there, even if they’re aimed at another language or development platform… and if you find one written in Javascript or Obj-C, you might be able to to convert it to Lua (depending on your familiarity with those languages).

I think I found a basic solution… it’s not perfect, but close enough for my purposes.

proj.linearDamping=.3

I simply multiplied  n * stepVelocity.x by .5 

return { x = (startingPosition.x) + (n \* stepVelocity.x \* .5) + (0.5 \* (n\*n+n) \* stepGravity.x), y = (startingPosition.y) + (n \* stepVelocity.y \* .5) + (0.5 \* (n\*n+n) \* stepGravity.y) }

If anyone know the formula for calculating linearDamping, please post it here…

Thanks in advance…

B

Here is a version that I am looking at from this site:

http://www.cocos2d-iphone.org/forum/topic/85982

//trajectory 'point at timestep n' formula taking linear damping into account b2Vec2 getTrajectoryPoint( b2Vec2 startingPosition, b2Vec2 startingVelocity, float n /\*time steps\*/, float linearDamping, float dt ) { b2Vec2 stepVelocity = dt \* startingVelocity; // m/s b2Vec2 stepGravity = dt \* dt \* m\_world-\>GetGravity(); // m/s/s //h4 = h + v( d + d^2 + d^3 + d^4) // + a(4d + 3d^2 + 2d^3 + d^4) float d = b2Clamp(1.0f - dt \* linearDamping, 0.0f, 1.0f); float vd = 0; float ad = 0; for (int i = 0; i \< n; i++) { float p = pow(d,i+1); vd += p; ad += (n-i) \* p; } return startingPosition + vd \* stepVelocity + ad \* stepGravity; }

Here is the solution…  figured it out late last night

Predicted Trajectory with linearDamping

local function getTrajectoryPoint (startingPosition, startingVelocity, n, linearDamping, dt) function clamp( val, low, high ) if (val \< low) then return low end if (val \> high) then return high end return val end stepVelocity = { x=dt\*startingVelocity.x, y=dt\*startingVelocity.y } local stepGravity = { x=dt\*0, y=dt\*0 } d = clamp(1.0 - dt \* linearDamping, 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 print(vd) return { x = startingPosition.x + vd \* stepVelocity.x + ad \* stepGravity.x, y = startingPosition.y + vd \* stepVelocity.y + ad \* stepGravity.y } end

Awesome, great work! I’m going to test it out later today. This is a useful addition to the demo.

Brent

Thanks Brent!

Now I have to figure out how to predict the applyLinearImpulse that provides the “curl” effect.

I am sure there is a simple solution, but I’ve racked my brain too long on this already. :slight_smile:

-Byron Fillmore