Game Development Community

Calculate an offset to player position in world

by Marc-Andre Vachon · in Torque 3D Public · 11/21/2009 (1:34 am) · 2 replies

Hi!
I'm working on a basic squad formation system and I'm wondering if there's something in T3D that would allow me to easily calculate a world position using an offset.

I want the new position to take the player direction in consideration (for ex: 1 want the unit move on the left of squad leader).

A friend helped me to get start but maybe there's something more efficient built in T3D.

Here's my current quick attempt, it's script side but my goal is to bring that in code.

%position = %obj.squadLeader.getPosition();
   %rotation = %obj.squadLeader.getEulerRotation();
   %rotation = getWord(%rotation,2);
   %xOffset = 2;
   
   echo(" Squad Leader Rotation = " @ %rotation);
   
   %posX = getWord(%position, 0);
   %posY = getWord(%position, 1);
   %posZ = getWord(%position, 2);
   
   //Calculate offset
   %newX =  %posX + (mCos(%rotation) * xOffset);
   %newY =  %posY + (mSin(%rotation) * xOffset);
   %newZ = %posZ;
   
   %finalPos = %newX SPC %newY SPC %newZ;
   
   echo(" Squad Leader Pos = " @ %position SPC "Formation Unit pos = " @ %finalPos);
   
   %obj.setMoveDestination(%finalPos);

It's not really working, it's only to show what I'm looking for

Any scripting or coding help to acheive that would be appreciate!

Thanks for reading!

#1
11/21/2009 (7:06 am)
This would be easier in source as you can just grab the x-axis vector from the Player's rotation matrix to use as an offset vector. Quick example:
// The Distance from the Player.
const F32 offsetLength = 2.f;
// Fetch Offset Vector.
const VectorF offsetVector;
player->getTransform().getColumn( 0, &offsetVector );

// Offset the Position.
const Point3F finalPosition = player->getPosition() + offsetLength * offsetVector;
Do you understand that? The first column of the Player's rotation matrix is its x-axis orientation. So if you take this vector and scale it and add it to the Player's current position, you *should* get a point 2 units to the right of the Player.

If you wanted the point to the left, just use the negative (-offsetVector).

Edit: Sorry, this code is untested and I could be wrong, but give it a whirl and let us know how it goes.
#2
11/21/2009 (4:53 pm)
Generally this is how you convert from object to world space.
Point3F offset(x,x,x); // in object space eg. relative to obj
obj->getTransform().mulP( offset ); // Offset is now a worldspace position.