Vectors and Quaternions and Headaches, Oh, my...
Moderators: winston, another_commander
Vectors and Quaternions and Headaches, Oh, my...
Trying to make sense out of vectors and quaternions is like trying to grab a fistful of smoke...
it's actually harder (I think) than pointers in C/C++.
MY PROBLEM
I want to be able to direct the movements of eight ships (specifically, one Behemoth, three
Frigates and four Transports) arranged like this:
B = Behemoth_position: [0, 0, 0]
Fa = Frigate1_post: [0, 0, 10000]† (12 o' clock, range: 10.0 km)
Fb = Frigate2_post: [-8660.254, 0, -5000]† (8 o' clock, range: 10.0 km)
Fc = Frigate3_post: [8660.254, 0, -5000]† (4 o' clock, range: 10.0 km)
Ta = Transport1_post: [-5303.3, 5303.3, 2500]† (9:30 high, range: 7.905694 km)
Tb = Transport2_post: [5303.3, 5303.3, 2500]† (2:30 high, range: 7.905694 km)
Tc = Transport3_post: [-5303.3, -5303.3, 2500]† (9:30 low, range: 7.905694 km)
Td = Transport4_post: [5303.3, -5303.3, 2500]† (2:30 low, range: 7.905694 km)
†(relative to the Behemoth, and moving with it.)
added with the code:
system.addShips("1701_behemoth-groucho", 1, [0, 0, 6000], 0);
system.addShips("1701_TGAF-01", 1, [0, 0, 16000], 0);
system.addShips("1701_TGAF-02", 1, [-8660.254, 0, 1000], 0);
system.addShips("1701_TGAF-03", 1, [8660.254, 0, 1000], 0);
system.addShips("1701_TGAT-01", 1, [-5303.3, 5303.3, 8500], 0);
system.addShips("1701_TGAT-02", 1, [5303.3, 5303.3, 8500], 0);
system.addShips("1701_TGAT-03", 1, [-5303.3, -5303.3, 8500], 0);
system.addShips("1701_TGAT-04", 1, [5303.3, -5303.3, 8500], 0);
What I need is JS code that will maintain the formation, keeping the Frigates and Transports
on their stations, no matter how the Behemoth moves.
MY VAGUE, SKETCHY NOTION OF A SOLUTION
I think the way to go is to identify the 7 points [Fa, Fb, Fc, Ta, Tb, Tc, and Td] located
relative to the Behemoth and then use a timer in the ship scripts to call (every 5 to 30 seconds
or so) a function that keeps each ship aimed at its assigned point (except during combat, of
course).
The trouble is, I have no idea how to:
1.) specify these 7 points,
2.) update them as the Behemoth moves,
3.) translate them into system ("abs") co-ordinates or
4.) make a ship move to a set of co-ordinates using only JavaScript (that is,
without resorting to the AI...)
So, if there's anybody out there who really understands this quaternion stuff (paging Dr.
Ahruman... paging Dr. Walch...) and how to use the Vector3D methods like angleTo() and
rotateTo(), I'd appreciate any help I can get.
A detailed explanation (as detailed as your patience with a slow student will permit...) would be
extremely welcome. Don't worry about drowning me in details... I really want to understand this
vector/quaternion business.
it's actually harder (I think) than pointers in C/C++.
MY PROBLEM
I want to be able to direct the movements of eight ships (specifically, one Behemoth, three
Frigates and four Transports) arranged like this:
B = Behemoth_position: [0, 0, 0]
Fa = Frigate1_post: [0, 0, 10000]† (12 o' clock, range: 10.0 km)
Fb = Frigate2_post: [-8660.254, 0, -5000]† (8 o' clock, range: 10.0 km)
Fc = Frigate3_post: [8660.254, 0, -5000]† (4 o' clock, range: 10.0 km)
Ta = Transport1_post: [-5303.3, 5303.3, 2500]† (9:30 high, range: 7.905694 km)
Tb = Transport2_post: [5303.3, 5303.3, 2500]† (2:30 high, range: 7.905694 km)
Tc = Transport3_post: [-5303.3, -5303.3, 2500]† (9:30 low, range: 7.905694 km)
Td = Transport4_post: [5303.3, -5303.3, 2500]† (2:30 low, range: 7.905694 km)
†(relative to the Behemoth, and moving with it.)
added with the code:
system.addShips("1701_behemoth-groucho", 1, [0, 0, 6000], 0);
system.addShips("1701_TGAF-01", 1, [0, 0, 16000], 0);
system.addShips("1701_TGAF-02", 1, [-8660.254, 0, 1000], 0);
system.addShips("1701_TGAF-03", 1, [8660.254, 0, 1000], 0);
system.addShips("1701_TGAT-01", 1, [-5303.3, 5303.3, 8500], 0);
system.addShips("1701_TGAT-02", 1, [5303.3, 5303.3, 8500], 0);
system.addShips("1701_TGAT-03", 1, [-5303.3, -5303.3, 8500], 0);
system.addShips("1701_TGAT-04", 1, [5303.3, -5303.3, 8500], 0);
What I need is JS code that will maintain the formation, keeping the Frigates and Transports
on their stations, no matter how the Behemoth moves.
MY VAGUE, SKETCHY NOTION OF A SOLUTION
I think the way to go is to identify the 7 points [Fa, Fb, Fc, Ta, Tb, Tc, and Td] located
relative to the Behemoth and then use a timer in the ship scripts to call (every 5 to 30 seconds
or so) a function that keeps each ship aimed at its assigned point (except during combat, of
course).
The trouble is, I have no idea how to:
1.) specify these 7 points,
2.) update them as the Behemoth moves,
3.) translate them into system ("abs") co-ordinates or
4.) make a ship move to a set of co-ordinates using only JavaScript (that is,
without resorting to the AI...)
So, if there's anybody out there who really understands this quaternion stuff (paging Dr.
Ahruman... paging Dr. Walch...) and how to use the Vector3D methods like angleTo() and
rotateTo(), I'd appreciate any help I can get.
A detailed explanation (as detailed as your patience with a slow student will permit...) would be
extremely welcome. Don't worry about drowning me in details... I really want to understand this
vector/quaternion business.
- Eric Walch
- Slightly Grand Rear Admiral
- Posts: 5536
- Joined: Sat Jun 16, 2007 3:48 pm
- Location: Netherlands
Re: Vectors and Quaternions and Headaches, Oh, my...
It should be possible to program, but not quite easy I think.
To translate local positions, relative to the Behemoth, in system coordinates, you can use
I used it in the buoyRepairControl ship from buoyRepair.oxp. This global position you must than add the the position of the behemoth.
This code will always work. Probably faster code is the internal library function of Oolite that executes in C and should have done the same. But that has a sign error that is fixed in trunk. That bug adds extra complexity when you want it to also work with trunk:
Again must this returned position be added to the behemoth position.
That calculated target position must than be stored in the
No need to add timers yourself. The
One problem you will encounter is that ships slow there speed when approaching their destination. So, I have now Idea how that would look in game and if formation would still be there. playing a bit with the
To translate local positions, relative to the Behemoth, in system coordinates, you can use
Code: Select all
this.rotateBy = function(position, orientation)
{
var t2, t3, t4, t5, t6, t7, t8, t9, t10;
var result = new Vector3D();
t2 = -orientation.w * orientation.x;
t3 = -orientation.w * orientation.y;
t4 = -orientation.w * orientation.z;
t5 = -orientation.x * orientation.x;
t6 = orientation.x * orientation.y;
t7 = orientation.x * orientation.z;
t8 = -orientation.y * orientation.y;
t9 = orientation.y * orientation.z;
t10 = -orientation.z * orientation.z;
result.x = 2*((t8 + t10)*position.x + (t6 - t4)*position.y + (t3 + t7)*position.z) + position.x;
result.y = 2*((t4 + t6)*position.x + (t5 + t10)*position.y + (t9 - t2)*position.z) + position.y;
result.z = 2*((t7 - t3)*position.x + (t2 + t9)*position.y + (t5 + t8)*position.z) + position.z;
return result;
}
Code: Select all
behemoth.position.add(this.rotateBy(position, behemoth.orientation))
Code: Select all
this.rotateBy = function (position, orientation)
{
if (0 < oolite.compareVersion("1.75")) orientation.w = -orientation.w;
return position.rotateBy(orientation);
}
That calculated target position must than be stored in the
savedCoordinates
property of the escorts. Than the escort ship can use a simple AI to fly to that position.Code: Select all
UPDATE = ("sendScriptMessage: nextPosition", setDestinationFromCoordinates, "setSpeedTo: 200", "setDesiredRangeTo: 1000", performFlyToRangeFromDestination, "pauseAI: 10");
sendScriptMessage:
does the position calculation. Speed should probably be set higher than the mothers speed. And the pause makes that the ships only update the position at low intervals. (pause is good for overall performance and it would look wrong when escorts would react to fast on their mothers direction changes)One problem you will encounter is that ships slow there speed when approaching their destination. So, I have now Idea how that would look in game and if formation would still be there. playing a bit with the
setDesiredRangeTo:
and and setSpeed:
should solve this.
Last edited by Eric Walch on Thu Jan 20, 2011 5:51 pm, edited 1 time in total.
UPS-Courier & DeepSpacePirates & others at the box and some older versions
Re: Vectors and Quaternions and Headaches, Oh, my...
I think I've tried this before in a test by that method, and also by having the "mother" ship explicitly set the position of the fleet ships by JS (at shorter intervals of course) when formation flying, and iirc both ways look very odd.
The problem is when the fleet ships try and do any kind of solo maneuvering or avoidance, it gets in the way of the commands issued by the mother ship and you get all sorts of weirdness. Also depending on the ships relative speeds you can have problems with fleet ships either getting left behind or reaching their designated points and just sitting their waiting.
Unfortunately it was one I also couldn't really think of a better method of than the one Eric alludes to above.
The problem is when the fleet ships try and do any kind of solo maneuvering or avoidance, it gets in the way of the commands issued by the mother ship and you get all sorts of weirdness. Also depending on the ships relative speeds you can have problems with fleet ships either getting left behind or reaching their designated points and just sitting their waiting.
Unfortunately it was one I also couldn't really think of a better method of than the one Eric alludes to above.
My OXPs via Boxspace or from my Wiki pages .
Thargoid TV
Dropbox Referral Link
Thargoid TV
Dropbox Referral Link
- JensAyton
- Grand Admiral Emeritus
- Posts: 6657
- Joined: Sat Apr 02, 2005 2:43 pm
- Location: Sweden
- Contact:
Re: Vectors and Quaternions and Headaches, Oh, my...
Your unarticulated wish is my command. (Not guaranteed. Usually not true.)
E-mail: [email protected]
Re: Vectors and Quaternions and Headaches, Oh, my...
Many thanks for the prompt replies...
Thanks, Ahruman, for your suggestion in the other thread. I neglected to mention that each of the ships accompanying the Behemoth will have Viper Interceptor escorts of its own, so -- due to the memory leak problem -- they can't actually be escorting the Behemoth. Still, it's good to know that escort formations are now controllable by something other than variations in "setDesiredRangeTo: ".
E. W.: Ouch! Much of your post has gone straight over my head... with any luck, it should start to penetrate after I've re-read it a few dozen times...
Thanks again for the help, everybody. It looks like I've still got a lot of work to do...
Thanks, Ahruman, for your suggestion in the other thread. I neglected to mention that each of the ships accompanying the Behemoth will have Viper Interceptor escorts of its own, so -- due to the memory leak problem -- they can't actually be escorting the Behemoth. Still, it's good to know that escort formations are now controllable by something other than variations in "setDesiredRangeTo: ".
E. W.: Ouch! Much of your post has gone straight over my head... with any luck, it should start to penetrate after I've re-read it a few dozen times...
Thanks again for the help, everybody. It looks like I've still got a lot of work to do...
- Eric Walch
- Slightly Grand Rear Admiral
- Posts: 5536
- Joined: Sat Jun 16, 2007 3:48 pm
- Location: Netherlands
Re: Vectors and Quaternions and Headaches, Oh, my...
Those quaternions take a while to digest. The main reason they are used it to make calculations faster. And you can ignore the first complex formula in above example. It is just an example how quaternions are handled internally. And I used it because I could not make the internal functionPhasted wrote:E. W.: Ouch! Much of your post has gone straight over my head... with any luck, it should start to penetrate after I've re-read it a few dozen times...
rotateBy()
working. At that time I thought I didn't understand it and looked for some other examples. After understanding things better I realised that the problem was in the function rotateBy()
and not in me using it wrong. rotateBy()
is actually very useful, specially when working with subentities. It allows to convert a local position inside the ship, to a system position.Looking at the new customizable escort positions I see that this deals with most of your problems. The only thing that still lacks is that you can not easy specify specific ships at specific positions as Oolite will always generate a random mix of escorts.
I just did a quick test. I targeted the mother and typed in the console:
Code: Select all
PS.target.escorts[0].remove()
PS.target.escortGroup.addShip(PS.target.spawnOne("myEscort"))
escorts
property of a ship is read only, but adding a ship to the escortGroup
, automatic adds it to the escorts
array. Currently ships are added with a full set of random escorts. You could remove them immediately and add your custom escorts than back.Probably we could use a new shipKey:
initial_escorts
with as default value max_escorts
. That way you can create ships that don't start with the maximum of escorts.Hmm... You can't use this at all because you did not want them as real escorts in the first place. Than you are back to the first suggestion to use normal flying behaviour. To get easy access to each others parameters it is than useful to use a shipGroup with the behemoth defined as leader.
UPS-Courier & DeepSpacePirates & others at the box and some older versions
- JensAyton
- Grand Admiral Emeritus
- Posts: 6657
- Joined: Sat Apr 02, 2005 2:43 pm
- Location: Sweden
- Contact:
Re: Vectors and Quaternions and Headaches, Oh, my...
A possible fix is to allowEric Walch wrote:Looking at the new customizable escort positions I see that this deals with most of your problems. The only thing that still lacks is that you can not easy specify specific ships at specific positions as Oolite will always generate a random mix of escorts.
escorts
to be an array of roles. If it’s a number, it would be seeded with escort-role
, “escort” or “wingman” as appropriate at shipregistry load time. Indices passed to coordinatesForEscortPosition
would match the order of the array.Allowing holes (empty string entries) would be a reasonable extension.
Adding a separate formation-flying system for things that aren’t escorts would be too much complexity at this point.
E-mail: [email protected]
Re: Vectors and Quaternions and Headaches, Oh, my...
If you're tweaking things in this area, another nice key option might be a way to override Oolite's lessening of the number of escorts in certain systems. For example if I'm spawning a Thargoid fleet with 4 alien escorts (for example as an invasion fleet), it would be nice to have a n easier way to ensure it always does have 4 escorts, regardless of if the system is Corporate or Anarchy.
I know if can be done via scripting (in much the same way Eric describes above), but it always seems like unnecessary work having to get around an Oolite "feature" that's a little too generic in its application for my personal tastes.
A key such as escort_fixed = true; or somesuch (with default as false, for action as current set-up) would be nice, and I guess relatively simple to add into the code?
I know if can be done via scripting (in much the same way Eric describes above), but it always seems like unnecessary work having to get around an Oolite "feature" that's a little too generic in its application for my personal tastes.
A key such as escort_fixed = true; or somesuch (with default as false, for action as current set-up) would be nice, and I guess relatively simple to add into the code?
My OXPs via Boxspace or from my Wiki pages .
Thargoid TV
Dropbox Referral Link
Thargoid TV
Dropbox Referral Link
- JensAyton
- Grand Admiral Emeritus
- Posts: 6657
- Joined: Sat Apr 02, 2005 2:43 pm
- Location: Sweden
- Contact:
Re: Vectors and Quaternions and Headaches, Oh, my...
I’m not sure what you mean. I can’t find anything government-related in the escort set-up code.
E-mail: [email protected]
Re: Vectors and Quaternions and Headaches, Oh, my...
Iirc the number of escorts a ship gets can be reduced from the plist amount when in "safer systems"? Or am I remembering wrongly or it's an urban myth or something?
Going by the entry for the escorts shipdata key in the wiki:
Going by the entry for the escorts shipdata key in the wiki:
If there is no reference to govt type in the code, how does Oolite decide whether to randomly do the subtraction?Determines how many escorts an NPC shall have. Maximum is 16. In save systems Oolite may randomly decide to subtract escorts from the defined value.
My OXPs via Boxspace or from my Wiki pages .
Thargoid TV
Dropbox Referral Link
Thargoid TV
Dropbox Referral Link
- Eric Walch
- Slightly Grand Rear Admiral
- Posts: 5536
- Joined: Sat Jun 16, 2007 3:48 pm
- Location: Netherlands
Re: Vectors and Quaternions and Headaches, Oh, my...
You should always get the maximum number of escorts. Lowering the number of escorts, based on the government type, only happens for ships that are explicit added in a default "trader" role. Also there is no need to do aThargoid wrote:If you're tweaking things in this area, another nice key option might be a way to override Oolite's lessening of the number of escorts in certain systems. For example if I'm spawning a Thargoid fleet with 4 alien escorts (for example as an invasion fleet), it would be nice to have a n easier way to ensure it always does have 4 escorts, regardless of if the system is Corporate or Anarchy.
setUpEscorts
in the AI, as this now is always called internally for script added ships.UPS-Courier & DeepSpacePirates & others at the box and some older versions
- JensAyton
- Grand Admiral Emeritus
- Posts: 6657
- Joined: Sat Apr 02, 2005 2:43 pm
- Location: Sweden
- Contact:
Re: Vectors and Quaternions and Headaches, Oh, my...
If you’re certain of this (and I agree it should be the case)Eric Walch wrote:Also there is no need to do asetUpEscorts
in the AI, as this now is always called internally for script added ships.
setUpEscorts
should be aliased to doNothing
in the whitelist.E-mail: [email protected]
- JensAyton
- Grand Admiral Emeritus
- Posts: 6657
- Joined: Sat Apr 02, 2005 2:43 pm
- Location: Sweden
- Contact:
Re: Vectors and Quaternions and Headaches, Oh, my...
On further consideration, deferred to post-MNSR.Ahruman wrote:A possible fix is to allowescorts
to be an array of roles.
E-mail: [email protected]
Re: Vectors and Quaternions and Headaches, Oh, my...
Fair enuff, although the wiki probably needs a tweak to clarify the role to which the subtraction applies (and to correct save to safe).
I thought I'd seen less before when using a non-trader role, but that may be my mistake.
I thought I'd seen less before when using a non-trader role, but that may be my mistake.
My OXPs via Boxspace or from my Wiki pages .
Thargoid TV
Dropbox Referral Link
Thargoid TV
Dropbox Referral Link