Jump to content

calculate rotation for object to hit a moving object


Lash Carver
 Share

You are about to reply to a thread that has been inactive for 3500 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

I have another annoying issue I can't solve so I thought I'd ask here again... I have two potential ways but both seem to arrive to the target location late. 

So basically I have a stationary object, when I tell it GoForIt() I want it to make an object "dart" and send it at a steady speed towards the detected (and moving) object in the hopes that they arrive at the same position and collide. The 1st solution has <0,0,30> which is the speed of the object once it starts moving. Right now the stationary object always arrives late and I cannot figure out why. I want the other object to have verying speeds for each pass, but once the stationary object always goes the same speed once moving.

 

vector Interception( vector cPos, vector tPos, vector cVel, vector tVel)
{
    vector rPos = tPos - cPos;
    float tVxtV = tVel * tVel; 
    float b = rPos * tVel;
    float a = cVel * cVel - tVxtV;
    return tPos + ( b + llSqrt( b*b + a * (rPos*rPos) ) ) / a * tVel;
}

rotation grtpa(vector axis, vector pos, vector target) 
{
target.z = pos.z; return llRotBetween(axis, target - pos);
}
GoForIt() {
//under another part of the script, estimate=llDetectedVel(0);
llDetectedVel(i) vector MyPos=llGetPos();
m = llGetObjectDetails(TARGET,[OBJECT_POS]);
LastPos=llList2Vector(d,1);

//1st way I tried
rotation m=grtpa(<0,0,1>, MyPos, Interception(MyPos, LastPos, <0,0,30>, estimate));

llRezObject("dart",llGetPos()+<-0.1,0,0.6>*llGetRot(),<0,0,velocity>*m,m,0);

//----------------------------------

//2nd way I tried
rotation m=grtpa(<0,0,1>, llGetPos(), LastPos+estimate);

llRezObject("Bullet",llGetPos()+<-0.1,0,0.6>*llGetRot(),<0,0,velocity>*m,m,0); }

 

 

Any ideas of what to fix or options to replace the whole thing would be great.

 

 

----------so in sort...----------

3 objects, objA and objB, and dart

objA is sitting still and gets the location and velocity of objB

objA rezzes "dart" and sends it at the correct rotation to hit objB where it WILL be. predicting it's location.

Link to comment
Share on other sites

Target prediction is not easy unless your target is either stationary or moving on an invariant path.  It is a lot easier to simply move your dart to the target's current position and then keep updating the "current" position while the dart is in flight, using new sensor data.  I'd suggest using a basic follower script to start and then messing with sensor speed and response time to get the effect you want.  There's a basic script in the wiki at http://wiki.secondlife.com/wiki/LlMoveToTarget

Link to comment
Share on other sites

>I have another annoying issue I can't solve so I thought I'd ask here again... I have two potential ways but both seem to arrive to the target location late. 

You are calculating where they will be at launch, not where they will be when the projectile reaches them.

 

 

Calculating intercepts is a function of position and time.  time is NOT lsl's strong suit.  in adition to time dialation and other sorts of sim lag. it's not real time and your function may be interupted at any point. 

That being said You can still do some fun things. I have a script I was playing with  I will post here

 

float speed = 100;float travelTime;float lastTime;//deals with latency so long as it remains consistentfloat cooldown;rotation intercept(vector vTarget,vector vVel) {//GUESTIMATE INTERCEPT	llResetTime();    vector vPos = llGetPos();     float travelTimeTemp = llVecDist(vTarget,vPos)/speed+lastTime;    vTarget+=vVel*travelTimeTemp;//estimate intercept point    float travelTime = llVecDist(vTarget,vPos)/speed+lastTime;    vTarget+=vVel*(travelTime-travelTimeTemp);//estimate intercept point with more accurate time    float fDistance = llVecDist(<vTarget.x, vTarget.y, 0>, <vPos.x, vPos.y, 0>);    return llRotBetween(<1, 0, 0>, llVecNorm( < fDistance, 0, vTarget.z - vPos.z > )) * llRotBetween(<1, 0, 0>, llVecNorm( < vTarget.x - vPos.x, vTarget.y - vPos.y, 0 > ));}default{    state_entry(){        llSensorRepeat("",llGetOwner(),AGENT,25,TWO_PI,1);    }    sensor(integer num){        rotation r = intercept(llDetectedPos(0),llDetectedVel(0));        llSetRot®;        if(cooldown < llGetTime()){            llRezObject("targeting round",llGetPos(),<speed,0,0>*r,r,(integer)(travelTime+2));			lastTime = llGetTime();        }    }    no_sensor(){        llSetRot(ZERO_ROTATION);    }}

 You can compensate for the inevitable accuracy with the method that was previously mentioned, a self correcting projectile,  or using a proximity fuse.

You can also use a BIG projectile. including an invisible "hitbox" prim if you are concerned with the visual effects.

Or something I am going to try right now... perhaps an array of soft linked projectiles...

Anyway that's part of what makes SL so fantastic. Figuring out ways to achieve the effect you want, sometimes in ways you hadn't even considered when you began. 

 

 

Link to comment
Share on other sites

Thanks to everybody who has tried to help, especially Aspire Rang. I am not wanting the dart to curve or follow the object, I need to be able to have it go straight and meet the object when it gets there.

Aspire brought up a very good point. I seem to have calulated where it will be when I throw the dart, hence why it's always late. I can see that now.

I will take a look at your script and try it out as well. Thank you again!

 

(oh and I'm not looking to hit it 100% of the time. I understand lag and such. I just want best-case scenario)

 

EDIT: I tried your script, but for my dart and overall setup, it shoots upward for some reason. I made some small changes and now it shoots right. It's very close, even hitting on the moving target once in a while which is great. But I noticed when the target sits still, it misses! Any ideas?

Link to comment
Share on other sites

The shooting up thing would just be an issue with the objects using different axis. I'm not sure what could make it miss while the target is motionless as the Velocity would be ZERO_VECTOR  I sent you a copy of the object  I was playing with in world. If that is still giving you trouble hit me up inworld.

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 3500 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...