Join us at the Oolite Anniversary Party -- London, 7th July 2024, 1pm
More details in this thread.

Vectors and Quaternions and Headaches, Oh, my...

Discussion and information relevant to creating special missions, new ships, skins etc.

Moderators: winston, another_commander

Post Reply
User avatar
Phasted
Competent
Competent
Posts: 51
Joined: Wed Jun 09, 2010 3:56 pm

Vectors and Quaternions and Headaches, Oh, my...

Post by Phasted »

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.
User avatar
Eric Walch
Slightly Grand Rear Admiral
Slightly Grand Rear Admiral
Posts: 5536
Joined: Sat Jun 16, 2007 3:48 pm
Location: Netherlands

Re: Vectors and Quaternions and Headaches, Oh, my...

Post by Eric Walch »

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

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;
}
I used it in the buoyRepairControl ship from buoyRepair.oxp. This global position you must than add the the position of the behemoth.

Code: Select all

behemoth.position.add(this.rotateBy(position, behemoth.orientation))
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:

Code: Select all

this.rotateBy = function (position, orientation)
{
	if (0 < oolite.compareVersion("1.75")) orientation.w = -orientation.w;
	return position.rotateBy(orientation);
} 
Again must this returned position be added to the behemoth position.

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");
No need to add timers yourself. The 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.
User avatar
Thargoid
Thargoid
Thargoid
Posts: 5525
Joined: Thu Jun 12, 2008 6:55 pm

Re: Vectors and Quaternions and Headaches, Oh, my...

Post by Thargoid »

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.
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Re: Vectors and Quaternions and Headaches, Oh, my...

Post by JensAyton »

Your unarticulated wish is my command. (Not guaranteed. Usually not true.)
User avatar
Phasted
Competent
Competent
Posts: 51
Joined: Wed Jun 09, 2010 3:56 pm

Re: Vectors and Quaternions and Headaches, Oh, my...

Post by Phasted »

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...
User avatar
Eric Walch
Slightly Grand Rear Admiral
Slightly Grand Rear Admiral
Posts: 5536
Joined: Sat Jun 16, 2007 3:48 pm
Location: Netherlands

Re: Vectors and Quaternions and Headaches, Oh, my...

Post by Eric Walch »

Phasted 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...
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 function 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"))
The 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.
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Re: Vectors and Quaternions and Headaches, Oh, my...

Post by JensAyton »

Eric 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.
A possible fix is to allow 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.
User avatar
Thargoid
Thargoid
Thargoid
Posts: 5525
Joined: Thu Jun 12, 2008 6:55 pm

Re: Vectors and Quaternions and Headaches, Oh, my...

Post by Thargoid »

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?
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Re: Vectors and Quaternions and Headaches, Oh, my...

Post by JensAyton »

I’m not sure what you mean. I can’t find anything government-related in the escort set-up code.
User avatar
Thargoid
Thargoid
Thargoid
Posts: 5525
Joined: Thu Jun 12, 2008 6:55 pm

Re: Vectors and Quaternions and Headaches, Oh, my...

Post by Thargoid »

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:

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.
If there is no reference to govt type in the code, how does Oolite decide whether to randomly do the subtraction?
User avatar
Eric Walch
Slightly Grand Rear Admiral
Slightly Grand Rear Admiral
Posts: 5536
Joined: Sat Jun 16, 2007 3:48 pm
Location: Netherlands

Re: Vectors and Quaternions and Headaches, Oh, my...

Post by Eric Walch »

Thargoid 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.
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 a setUpEscorts in the AI, as this now is always called internally for script added ships.
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Re: Vectors and Quaternions and Headaches, Oh, my...

Post by JensAyton »

Eric Walch wrote:
Also there is no need to do a setUpEscorts in the AI, as this now is always called internally for script added ships.
If you’re certain of this (and I agree it should be the case) setUpEscorts should be aliased to doNothing in the whitelist.
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Re: Vectors and Quaternions and Headaches, Oh, my...

Post by JensAyton »

Ahruman wrote:
A possible fix is to allow escorts to be an array of roles.
On further consideration, deferred to post-MNSR.
User avatar
Thargoid
Thargoid
Thargoid
Posts: 5525
Joined: Thu Jun 12, 2008 6:55 pm

Re: Vectors and Quaternions and Headaches, Oh, my...

Post by Thargoid »

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.
User avatar
Phasted
Competent
Competent
Posts: 51
Joined: Wed Jun 09, 2010 3:56 pm

Re: Vectors and Quaternions and Headaches, Eureka!!...

Post by Phasted »

Post Reply