Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Distance, force, and SL physics.

Alphonsus Peck
Registered User
Join date: 4 Apr 2006
Posts: 10
06-06-2007 11:13
I would like to use llApplyImpulse to toss an object a more or less precise distance, and I was hoping that someone could give me a formula. Even a rough one would be appreciated.

Here is a code segment:

float fForce=20;
float fMass=llGetMass();

llApplyImpulse(<fForce, 0, fForce> * fMass, FALSE);

(I'm not worried about wind at this time);

Is there any way to calculate the distance an object will be thrown along the X axis in the above formula where the only variable that changes is the force? I'm assuming that the projectile will not be crossing a Sim boundary, will not hit a wall, will not bounce off ban lines, etc.

Any help would be appreciated.
Ged Larsen
thwarted by quaternions
Join date: 4 Dec 2006
Posts: 294
06-06-2007 11:53
From: Alphonsus Peck
I would like to use llApplyImpulse to toss an object a more or less precise distance, and I was hoping that someone could give me a formula. Even a rough one would be appreciated.

Here is a code segment:

float fForce=20;
float fMass=llGetMass();

llApplyImpulse(<fForce, 0, fForce> * fMass, FALSE);

(I'm not worried about wind at this time);

Is there any way to calculate the distance an object will be thrown along the X axis in the above formula where the only variable that changes is the force? I'm assuming that the projectile will not be crossing a Sim boundary, will not hit a wall, will not bounce off ban lines, etc.

Any help would be appreciated.


Alphonsus, I went through these calculations before, under the assumption that real-world physics, with respect to applied force, impulse, inertia, momentum, gravity, and kinetic / potential energy was accurate.

After doing all the calculations, I discovered that SL imposes "caps" maximum velocities. Furthermore, I believe the physics engine doesn't appear to be "continuous" -- you'll have variable results, on repeated tests, depending on the lag the Sim is currently experiencing.

Perhaps you will have better luck.

BTW, your question is not _quite_ precise. If an impulse is applied to an object, the distance the object travels will depend on the vector the impulse is applied. Maximum horizontal distance will be generated if the impulse is applied at 45 degrees from the horizon. Straight up, and it'll return to your "zero" height exactly where it left; straight horizontally, and it impacts the "zero" height immediately. So, your distance calculation will depend on both the magnitude and direction of your impulse. (I presume you meant impulse, and not force, because you need to apply a force over a specified period of time.)

Edited to add: Oops, just looked -- you are applying it at 45 degrees, in your vector.

Edited again: here's my old post, with calculations -- upward impulse only: /54/d3/167753/1.html
_____________________
- LoopRez, flexi prim skirt generating tool
- LinkRez, a necklace chain generator
Ged Larsen
thwarted by quaternions
Join date: 4 Dec 2006
Posts: 294
06-06-2007 12:05
Quick Google search revealed this good treatment of your problem: http://id.mind.net/~zona/mstm/physics/mechanics/curvedMotion/projectileMotion/generalSolution/generalSolution.html

Just use your impulse and mass to get your starting velocity vector, and plug into their equations.

I still don't think SL will give you an accurate answer, but if you go ahead and try it, please report back on your results, "Alphonsus Newton" ;)
_____________________
- LoopRez, flexi prim skirt generating tool
- LinkRez, a necklace chain generator
Alphonsus Peck
Registered User
Join date: 4 Apr 2006
Posts: 10
Distance, force, and SL physics.
06-06-2007 13:51
I should have mentioned that I was only worrying about a 45 degree vector. As to the difference between force and impulse, well, there are reasons why I am not currently a physicist. :-)

Thank you very much for both links to both your previous thread and to the projectile motion page. The Projectile motion page has me itching to try to enter the formulas into my shell. And learning about the 50m/s cap is very useful; I may be able to find a work around. (Perhaps using an "exploding shell" which supplies a second impulse in the same direction when the shell reaches the top of it's trajectory -- kind of like the second stage of a rocket. Ahhh...what fun!) Thank god the formulas are spelled out for me. I'd hate to try to figure out the whys and wherefores myself.

Alphonsus Newton? I think not. It ain't apples I'm shooting all over the Sim. (/me grins evilly)
Ged Larsen
thwarted by quaternions
Join date: 4 Dec 2006
Posts: 294
06-06-2007 15:16
From: Alphonsus Peck
Alphonsus Newton? I think not. It ain't apples I'm shooting all over the Sim. (/me grins evilly)


Alphonsus Nobel, of the Nobel prize then? After all, he did invent a nice safe way to package trinitrotoluene...

It might be fun to allow adjustable angles to fire your projectiles -- I'm imagining CONSTANT impulses as an option, and artillery aiming adjustments, lol.
_____________________
- LoopRez, flexi prim skirt generating tool
- LinkRez, a necklace chain generator
Alphonsus Peck
Registered User
Join date: 4 Apr 2006
Posts: 10
06-06-2007 17:37
Constant impulses aren't what I want for my purposes. It would be very easy to send an object off into infinity. That feels like cheating.

And I do plan to handle different angles (although I'm sticking to the 45 degree firing angle for simplicities sake right now). The following code is the nuts and bones of what I've come up with. Various subroutines would have to actually be placed in different objects, and this code is COMPLETELY UNTESTED. Still, the velocity calculator is correct, given real world physics, anyway. This code is designed to fire a projectile from the projectile's starting location to any other X, Y coordinate in the Sim. (NOT Z. That's a bridge way too far for me right now). The only thing the code needs is the vector vDestPos (destination position), and the final subroutine (fnFire_Projectile) is all that needs to be called to get the whole thing running.

float fDist;
float fVelocity;
vector vAngle;
vector vUnitCirc;
vector vVelocity;


//*************//
fnCalculate_Angle(vector vDestPos){
vector vStartPos;
vector vDelta;

vStartPos = llGetPos();
vDelta.x = vDestPos.x - vStartPos.x;
vDelta.y = vDestPos.y - vStartPos.y;
vDelta.z = 0;

fDist = llSqrt( vDelta.x * vDelta.x + vDelta.y * vDelta.y );

vUnitCirc.x = vDelta.x / fDist;
vUnitCirc.y = vDelta.y / fDist;
vUnitCirc.z = 1.0;
vAngle.x = llAcos( vUnitCirc.x );
vAngle.y = llAsin( vUnitCirc.y );
vAngle.z = 0;
}

//*************//
fnRotate_Platform(vector vDestPos){
rotation rRotation;

fnCalculate_Angle( vDestPos );
rRotation = llEuler2Rot( vAngle );
llSetRotation( rRotation );
}

//*************//
fnCalculate_Velocity(float fDist){
fVelocity = llSqrt( fDist * 9.8 / 2 );
vVelocity = vUnitCirc * fVelocity;
}

//*************//
fnFire_Projectile(vector vDestPos){
fnRotate_Platform( vDestPos );
fnCalculate_Velocity( fDist );

llApplyImpulse( vVelocity * llGetMass(), FALSE );
}