December 22, 2024, 06:30:48

Author Topic: [Answered] Help with the ship's position and rotation variables  (Read 7129 times)

Offline Mularac

  • Lieutenant
  • ***
  • Posts: 531
  • Karma: 11
    • View Profile
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.
« Last Edit: April 05, 2011, 04:31:39 by Mularac »

Offline The Old Dragon

  • Ensign
  • ***
  • Posts: 362
  • Karma: 6
    • View Profile
    • http://
(No subject)
« Reply #1 on: June 06, 2010, 11:34:19 »
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;
Better to look the fool by asking, then prove them right with ignorance.

Offline Mularac

  • Lieutenant
  • ***
  • Posts: 531
  • Karma: 11
    • View Profile
(No subject)
« Reply #2 on: June 06, 2010, 17:12:42 »
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<0<180, which is, effectibly, a circunference
the 2°, the pitch, is a value that works in a similar way, an angle between the ship's nose and an arbitrary 0 in the plane x=0, but the values go from -90<0<90 but in this way:
(well... asumming that's an actual circle, that is....)


The 3° I'm yet to figure it out, but it must be the angles of a circunference in the y=0 plane

Offline Arparso

  • Administrator
  • Lieutenant
  • *****
  • Posts: 558
  • Karma: 14
  • I can see you...
    • View Profile
    • http://arparso.de/nexus
(No subject)
« Reply #3 on: June 07, 2010, 00:18:09 »
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: [Select]
// 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);

Offline Mularac

  • Lieutenant
  • ***
  • Posts: 531
  • Karma: 11
    • View Profile
(No subject)
« Reply #4 on: June 07, 2010, 00:31:20 »
Yep, that's it. You're damn good, Arparso. Btw, how's work coming on the Freespace mod?