Nexus Modding > Scripting
[Answered] Help with the ship's position and rotation variables
(1/1)
Mularac:
A thing that's been bugging me for weeks (or pehraps even months) is how could we make a function that using a ship's position and rotation coordinates we could create a direction vector of that ship, for example, or something like that.
What I'm trying to achieve is something that would return the coordinates of point in space directly behind the ship, let's say at x number of meters behind. but if we understand how that all works we can do a lot more.
The Old Dragon:
Well, we can start by using the following to get any positional data...
obj: PositionX;
obj: PositionY;
obj: PositionZ;
and orientational data...
obj: heading;
obj: bank;
obj: pitch;
And to use these (at least the positional ones) to place effects...
PosX:=Obj: PositionX;
Mularac:
Well, yes, that part is pretty clear to me :P
And also, I wish to emphazise that this is not enterily an scripting problem but a mathematical one (that when it's solved it needs to be ported to a informatic structure... but that's an other problem :P)
So I think we should basically start be understanding what those variables mean.
Well, so far we know this: m.ship:position is the coordinates of where the ship is in space, related to an arbitrary point of origin in the center of the map and m.ship:orientation vector of some sort, where the values are not numbers but degrees, where the first one is the "heading", the 2° one the "pitch", and the 3° the "bank" of the ship.
In my experimentation, I gathered that the 1° value (heading) is the value of the angle that the ship's nose makes with an arbitrary line defined as the 0° in a circunference in the plane z=0, ranging from -180
Arparso:
Heading, pitch and bank are more or less the same as yaw, pitch and roll: they rotate the craft around a certain axis by the given amount of degrees.
Heading or yaw rotates the ship around its Y-axis. E.g. a heading of 180 degrees and its facing backwards, at 90 degrees it's facing left and at -90 degrees its facing right.
Pitch rotates the craft around its X-axis. E.g. pitch of 90 degrees makes it face upwards and -90 degrees downwards.
Bank or roll rotate the craft around its Z-axis, which runs through the ship from the back to the front. For example, if roll is 180 degrees, it's flying upside down.
You can read more about it on http://en.wikipedia.org/wiki/Yaw,_pitch,_and_roll.
Now to get the direction vector out of that, just use Nexus' inbuilt R2Vec() function. It takes the orientation of the ship and calculates a vector pointing in that direction. You can then normalize it with VNorm() (make it have a length of 1) and multiply it with whatever distance you see fit, using VMul(). For example, to get a position 2000m in front of a ship, you can do something like that:
--- Code: ---// get the normalized direction vector
E.directionVector := VNorm(R2Vec(m.ship:orientation));
// multiply it with 2000m
E.directionVector := VMul(2000, E.directionVector);
// add the direction to the current ship position
// to get the absolute coordinates of the new position
E.newPosition := VAdd(m.ship:position, E.directionVector);
--- End code ---
Mularac:
Yep, that's it. You're damn good, Arparso. Btw, how's work coming on the Freespace mod?
Navigation
[0] Message Index
Go to full version