SDK Question Adding velocity through DefSetStateEx()

dumbo2007

Crazy about real time sims
Joined
Nov 29, 2009
Messages
675
Reaction score
0
Points
0
Location
India
I thought I was getting quite good with co-ordinate transformation....but nopes, stuck again!

This time I am trying to set a velocity. So first lets define the frames.

I am using a vessel as a base positioned at Brighton Beach, which I call the base vessel.

The DG is attached to it or detached as needed depending on whether collision is needed. When the DG is attached to the base, I control its position based on what Bullet is giving me. This includes when the DG takes off from the pad at Brighton.

After the DG rises to a certain height above the surface, I detach it from the base vessel and let Orbiter take control. However I want to transfer the last reported linear velocity by Bullet to Orbiter. Otherwise immediately after detachment, the DG will have 0 velocity till the Orbiter Engine kicks in and accelerates it.

Now for the code :

Code:
btVector3 linearVel = rbCollider->getLinearVelocity();   //velocity from Bullet in base frame

VECTOR3 rdirBaseLinearVel = _V(linearVel.x(), linearVel.y(), linearVel.z());

VESSELSTATUS2 vs;
memset(&vs, 0, sizeof(VESSELSTATUS2));
vs.version = 2;
vAttached->GetStatusEx(&vs);

vs.rvel = rdirBaseLinearVel ;

vAttached->DefSetStateEx(&vs);

First problem, I do get a nearly smooth transition after I set the velocity using above. But there is a slight sideways velocity added. I dont understand why.
I can probably live with it, and maybe somehow compensate for it, but it should not happen. Its very noticeable and the deflection increases with the amount of linear velocity that is set. If I do not set anything then its fine. Which makes me think that somehow I am not converting the linear velocity with respect to the base vessel to the correct vector to set/add to vs.rvel.

Second problem, I printed out the reference body in vs.rbody . Its the Moon, which is correct as the Moon is the nearest gravity reference. Yet I can set vs.rvel to a velocity which is with respect to the base vessel. It should accept relative velocities only with respect to the Moon ! (meaning I should be required to convert the velocity vector). But if I do that using :

Code:
baseVessel->Local2Rel(rdirBaseLinearVel, rdirMoonLinearVel);
vs.rvel = rdirMoonLinearVel;

I end up somewhere in orbit, high above the Moon !
 
Last edited:
I thought I was getting quite good with co-ordinate transformation....but nopes, stuck again!

This time I am trying to set a velocity. So first lets define the frames.

I am using a vessel as a base positioned at Brighton Beach, which I call the base vessel.

The DG is attached to it or detached as needed depending on whether collision is needed. When the DG is attached to the base, I control its position based on what Bullet is giving me. This includes when the DG takes off from the pad at Brighton.

After the DG rises to a certain height above the surface, I detach it from the base vessel and let Orbiter take control. However I want to transfer the last reported linear velocity by Bullet to Orbiter. Otherwise immediately after detachment, the DG will have 0 velocity till the Orbiter Engine kicks in and accelerates it.

Now for the code :

Code:
btVector3 linearVel = rbCollider->getLinearVelocity();   //velocity from Bullet in base frame

VECTOR3 rdirBaseLinearVel = _V(linearVel.x(), linearVel.y(), linearVel.z());

VESSELSTATUS2 vs;
memset(&vs, 0, sizeof(VESSELSTATUS2));
vs.version = 2;
vAttached->GetStatusEx(&vs);

vs.rvel = rdirBaseLinearVel ;

vAttached->DefSetStateEx(&vs);

First problem, I do get a nearly smooth transition after I set the velocity using above. But there is a slight sideways velocity added. I dont understand why.
I can probably live with it, and maybe somehow compensate for it, but it should not happen. Its very noticeable and the deflection increases with the amount of linear velocity that is set. If I do not set anything then its fine. Which makes me think that somehow I am not converting the linear velocity with respect to the base vessel to the correct vector to set/add to vs.rvel.

Second problem, I printed out the reference body in vs.rbody . Its the Moon, which is correct as the Moon is the nearest gravity reference. Yet I can set vs.rvel to a velocity which is with respect to the base vessel. It should accept relative velocities only with respect to the Moon ! (meaning I should be required to convert the velocity vector). But if I do that using :

Code:
baseVessel->Local2Rel(rdirBaseLinearVel, rdirMoonLinearVel);
vs.rvel = rdirMoonLinearVel;

I end up somewhere in orbit, high above the Moon !

You're close, but not quite there. You are correct that it needs to be relative to the moon, but you need to account for Local2Rel converting a position vector into another position vector, not a velocity vector into another velocity vector. In order to use it for this, you need to subtract the base's position relative to the moon from the velocity vector before calling Local2Rel. This allows you to cancel out the position translation performed by Local2Rel and just make use of it for the rotation.

Make sense? I've explained it elsewhere on the forums--search for posts by me containing words like "vector" and "velocity" or the function names, I might've explained it better before when I wasn't typing on my phone.
 
Thanks !

I remember you explaining many times before :), but I cant seem to be able to get it to work for this case :P . Here is what I do now for subtracting the base position with respect to the Moon from the Linear Velocity first :

Code:
btVector3 linearVel = rbCollider->getLinearVelocity();   //base frame


    VECTOR3 rdirRBodyLinearVel, rdirMoonBase, rdirBaseLinearVel;
    VECTOR3 gposBase, gposMoon;
    
    rdirBaseLinearVel = _V(linearVel.x(), linearVel.y(), linearVel.z());

    bumpBase->bb->GetGlobalPos(gposBase);
    oapiGetGlobalPos(bumpBase->hObjRefBody, &gposMoon);

    rdirMoonBase = gposBase - gposMoon;
    rdirBaseLinearVel -= rdirMoonBase;


    bumpBase->bb->Local2Rel(rdirBaseLinearVel, rdirRBodyLinearVel);

    VESSELSTATUS2 vs;
    memset(&vs, 0, sizeof(VESSELSTATUS2));
    vs.version = 2;
    vAttached->GetStatusEx(&vs);

    //vs.rpos = rposVesselPos;
    vs.rvel = rdirBaseLinearVel;

    char strRBodyName[MAX_STRLEN];
    oapiGetObjectName(vs.rbody, strRBodyName, MAX_STRLEN);



    sprintf(oapiDebugString(), "transferCount: %d", transferCount);

    oapiWriteLogV("Bullet linearVel : %f, %f, %f >> %f, %f, %f, pos: %f, %f,%f >> %f, %f, %f, %s",
            BT3ARGS(linearVel), V3ARGS(vs.rvel),
            V3ARGS(vs.rpos), V3ARGS(rposVesselPos), strRBodyName);



    vAttached->DefSetStateEx(&vs);
The vessel translates severely to its right when I do this.

---------- Post added at 08:15 PM ---------- Previous post was at 08:10 PM ----------

I also tried this :

Code:
btVector3 linearVel = rbCollider->getLinearVelocity();   //base frame


    VECTOR3 rdirRBodyLinearVel, rdirMoonBase, rdirBaseLinearVel;
    VECTOR3 gposBase, gposMoon;

    rdirBaseLinearVel = _V(linearVel.x(), linearVel.y(), linearVel.z());

    bumpBase->bb->Local2Rel(_V(0, 0, 0), rdirMoonBase);
    rdirBaseLinearVel -= rdirMoonBase;


    bumpBase->bb->Local2Rel(rdirBaseLinearVel, rdirRBodyLinearVel);

     VESSELSTATUS2 vs;
    memset(&vs, 0, sizeof(VESSELSTATUS2));
    vs.version = 2;
    vAttached->GetStatusEx(&vs);

    //vs.rpos = rposVesselPos;
    vs.rvel = rdirBaseLinearVel;

    char strRBodyName[MAX_STRLEN];
    oapiGetObjectName(vs.rbody, strRBodyName, MAX_STRLEN);



    sprintf(oapiDebugString(), "transferCount: %d", transferCount);

    oapiWriteLogV("Bullet linearVel : %f, %f, %f >> %f, %f, %f, pos: %f, %f,%f >> %f, %f, %f, %s",
            BT3ARGS(linearVel), V3ARGS(vs.rvel),
            V3ARGS(vs.rpos), V3ARGS(rposVesselPos), strRBodyName);



    vAttached->DefSetStateEx(&vs);

Its the same effect of course with the same consequences :(

---------- Post added at 08:38 PM ---------- Previous post was at 08:15 PM ----------

Here is a video of the issue :


-----------------------

Here is what happens if I directly pass the base linear velocity :

Code:
        btVector3 linearVel = rbCollider->getLinearVelocity();   //base frame


	VECTOR3 rdirRBodyLinearVel, rdirMoonBase, rdirBaseLinearVel;
	VECTOR3 gposBase, gposMoon;
	
	rdirBaseLinearVel = _V(linearVel.x(), linearVel.y(), linearVel.z());	

	
	VESSELSTATUS2 vs;
	memset(&vs, 0, sizeof(VESSELSTATUS2));
	vs.version = 2;
	vAttached->GetStatusEx(&vs);

	//vs.rpos = rposVesselPos;
	vs.rvel = rdirBaseLinearVel;

	char strRBodyName[MAX_STRLEN];
	oapiGetObjectName(vs.rbody, strRBodyName, MAX_STRLEN);


	sprintf(oapiDebugString(), "transferCount: %d", transferCount);

	oapiWriteLogV("Bullet linearVel : %f, %f, %f >> %f, %f, %f, pos: %f, %f,%f >> %f, %f, %f, %s",
			BT3ARGS(linearVel), V3ARGS(vs.rvel),
			V3ARGS(vs.rpos), V3ARGS(rposVesselPos), strRBodyName);

	vAttached->DefSetStateEx(&vs);


Note at the transition point, the DG has just been detached from the base vessel, so its position is correct. Its only the linear velocity which seems to be just off by a bit :P

Whats kind of un-nerving is that this case seems to be more accurate even though it shouldnt be :)
 
Last edited:
Congratulations, I think you've discovered orbital effects...

You also need to add the velocity vector of the base relative to the moon in order to account for the moon's rotation, i think...
 
You also need to add the velocity vector of the base relative to the moon in order to account for the moon's rotation

But thats zero, as the base vessel is landed on the Moon.

Say its the speed at which the base is rotating with the surface of the moon, i.e. the linear speed of the surface point at the base, then how would I determine it ?

angular speed * radius of Moon ?

---------

I tried this :

Code:
	bumpBase->bb->GetRelativeVel(bumpBase->hObjRefBody, rdirMoonBaseVel);

	rdirBaseLinearVel += rdirMoonBaseVel;

No effect. Its probably returning 0,0,0 in rdirMoonBaseVel
 
Last edited:
But thats zero, as the base vessel is landed on the Moon.

Say its the speed at which the base is rotating with the surface of the moon, i.e. the linear speed of the surface point at the base, then how would I determine it ?

angular speed * radius of Moon ?
I have a post for this too :) http://www.orbiter-forum.com/showthread.php?p=57499&postcount=10

But the velocity of the base relative to (center of) the moon isn't zero, and I would think that GetRelVel would give you the relative velocity that you could use without calculating it yourself...
 
WOHOOOOOO!!!!


As usual, you are spot on Heilor!

I was adding the velocities before calling Local2Rel(). They should be added later.

Seems we ll have smooth collision detection after all :) :)

---------- Post added at 03:42 AM ---------- Previous post was at 03:35 AM ----------

Here is the code for posterity :

Code:
    VECTOR3 rvelVesselWRTBase, rvelBaseWRTRefBody, rposBaseWRTRefBody, rvelVesselWRTRefBody;
    VECTOR3 ravelVesselWRTBase;

    btVector3 rvelBtVessel = rbCollider->getLinearVelocity();   //base frame
    rvelVesselWRTBase = _V(BT3ARGS(rvelBtVessel));

    btVector3 ravelBtVessel = rbCollider->getAngularVelocity();   //base frame
    ravelVesselWRTBase = _V(-ravelBtVessel.x(), -ravelBtVessel.y(), -ravelBtVessel.z());

    bumpBase->bb->Local2Rel(_V(0., 0., 0.), rposBaseWRTRefBody);
    bumpBase->bb->GetRelativeVel(bumpBase->hObjRefBody, rvelBaseWRTRefBody); //this is not 0

    //Convert linear velocity
    bumpBase->bb->Local2Rel(rvelVesselWRTBase, rvelVesselWRTRefBody);
    rvelVesselWRTRefBody -= rposBaseWRTRefBody;  //to compensate for Local2Rel() converting a direction vector
    rvelVesselWRTRefBody += rvelBaseWRTRefBody;  //to account for orbital speed of base(rotating with the Moon)
    
    
    //Vessel status stuff
    VESSELSTATUS2 vs;
    memset(&vs, 0, sizeof(VESSELSTATUS2));
    vs.version = 2;
    vAttached->GetStatusEx(&vs);

    vs.rvel = rvelVesselWRTRefBody;
    vs.vrot = ravelVesselWRTBase ;

    vAttached->DefSetStateEx(&vs);
Darn, the transition is so smooth now, a pilot would barely know it :)

---------- Post added at 04:25 AM ---------- Previous post was at 03:42 AM ----------



For angular velocity, just changing the sign(left handed system to right handed system) and passing the anguar velocity values directly
seems to work !
 
Last edited:
For angular velocity, just changing the sign(left handed system to right handed system) and passing the anguar velocity values directly
seems to work !
Orbiter convention is that linear coordinates are left-handed and rotations are right-handed.
 
Back
Top