Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
3 registered members (NewbieZorro, TipmyPip, AndrewAMD), 14,749 guests, and 7 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 4 of 5 1 2 3 4 5
Re: rotation around a sphere [Re: JibbSmart] #392028
01/18/12 16:40
01/18/12 16:40
Joined: Oct 2008
Posts: 513
Carlos3DGS Offline OP
User
Carlos3DGS  Offline OP
User

Joined: Oct 2008
Posts: 513
Thanks alot! It seems I was closer to the solution on my first attempt than on the latter.

I implemented as-is and it worked great! (excluding some special cases)
http://www.youtube.com/watch?v=cGHNkYPWsJU

The first 1:45 of the video is showing the "pathfinding" working flawlessly.
At 1:45 I start showing the special cases where it dosn't.

Observing this strange behaviour I think I have discovered why it messes up. It seems like it is happening when it reaches the equivalent point, but on the opposite side of the planet. In the video you can observe this not only happens when traveling north-to-south or south-to-north poles, it also happens with any opposite points (West-to-East, NE-to-SW, etc...)

I have designed a function to detect if the point is on the same side of the planet or not (relative to a plane cut oriented by the Origin's normal from the planet core).

Code:
int hemisphere_compare(VECTOR* Origin, VECTOR* Destination, ENTITY* Planet)
{
	VECTOR Plane_Normal;
	VECTOR Point;
	int hemisphere;
	
	//get values relative to planet core
	vec_diff(Plane_Normal,Origin,Planet.x);
	vec_diff(Point,Destination,Planet.x);
	//normalize vectors
	vec_normalize(Plane_Normal,1);
	vec_normalize(Point,1);
	
	//calculate hemisphere of Destination
	//relative to a plane-cut oriented by Origin's normal
	hemisphere=vec_dot(Plane_Normal,Point);
	
	if(hemisphere>0)//if on my same hemisphere
	{
		return(1);
	}
	else//if on opposite hemisphere
	{
		return(0);
	}
}




This function should be usefull to fix the problem, but I'm not sure where to plug this in your function (probalby above the last two lines, to do something different with those last two lines afterwards?).
But what I am mostly confused with is... how to modify that last part of your function depending on the results of mine?

current brain status: dazed and confused
confused

EDIT:
I have done further tests drawning an axsis going through the planet's core aligned to our vehicle's normal (blue line).
http://www.youtube.com/watch?v=a-1EbjmkwMk
This proves the problem is not when going through the same point on the opposite side?
current brain status: even more dazed and extremely confused...

EDIT2:
Done further tests, this time debugging V_Path (green line).
V_Path is the temporary path for the pan calculations. It is a rotated version of our destination.
http://www.youtube.com/watch?v=ntPkiiZTV8Y
Aha! Finally found the culprit! It seems the problem occurs when our temporary path is aligned vertically.
(I'm a little less confused now, I understand what is causing this but it seems I can't figure out how to fix it)

Last edited by Carlos3DGS; 01/18/12 18:16. Reason: further testing and example videos

"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
Re: rotation around a sphere [Re: Carlos3DGS] #392042
01/18/12 18:14
01/18/12 18:14
Joined: Mar 2006
Posts: 3,538
WA, Australia
J
JibbSmart Offline
Expert
JibbSmart  Offline
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
Interesting. I think the problem is that, as you've noticed earlier, alignToVec doesn't completely align the axes in one go (this is due to rounding errors that get exacerbated with a larger angle between the two axes). Since on the other side of the planet the vector could be a little behind or a little in front of your vehicle (in object space), these little inaccuracies cause the vehicle to change its mind about which direction it should go.

Here are a few ideas:

First, we can confirm this by adding a few more alignToVec calls (like you did earlier) and see if that makes the problem less likely to occur (that is, it'll hopefully mean the destination needs to be much closer to the opposite pole for the problem to occur).

Secondly, if the vehicle adjusts its angle gradually rather than instantly (add a max_rotation parameter to the look_at function and make sure the function never rotates the vehicle more than the amount given in this parameter -- make it 5*time_step, for example, and the vehicle won't adjust its pan by more than 5 degrees per tick), its turning circle might let the vehicle get far enough away from its destination's pole that it can escape that "inaccurate zone" and continue to its destination.

Thirdly, if the vehicle only checks the angle to its destination once a second or so, it should be able to get far enough away from the pole after the first check that any checks after that will be fairly consistent.

Any one of these should help, I think. The first is fairly inefficient, but could be used to confirm whether or not that's the root of the problem. In the end, a combination of the second and third solutions might be ideal, but the first would be the quickest way to confirm what the actual problem is.


Formerly known as JulzMighty.
I made KarBOOM!
Re: rotation around a sphere [Re: JibbSmart] #392043
01/18/12 18:49
01/18/12 18:49
Joined: Oct 2008
Posts: 513
Carlos3DGS Offline OP
User
Carlos3DGS  Offline OP
User

Joined: Oct 2008
Posts: 513
Originally Posted By: JibbSmart
First, we can confirm this by adding a few more alignToVec calls (like you did earlier) and see if that makes the problem less likely to occur (that is, it'll hopefully mean the destination needs to be much closer to the opposite pole for the problem to occur).


Actually my first tests (not posted on this thread) had very strange results do to that. I did think of that and modified your function to get instant rotations:
Originally Posted By: JibbSmart's alignToVec thread
Call "alignToVec(........, 1)" once every frame over several frames to complete the alignment (this will be quite smooth and quite quick), or several times in one frame for a complete alignment in only one frame.
If you would rather deal with the imprecisions yourself, remove the "* vec_length(rotAxis);".

Modified version of your function I am currently using:
Click to reveal..
Code:
function alignToVec(ANGLE* entAng, VECTOR* vec, VECTOR* axis, var factor) {
	vec_rotate(axis, entAng);
	vec_normalize(axis, 1);
	vec_normalize(vec, 1);
	VECTOR rotAxis;
	vec_cross(rotAxis, vec, axis);
	var angle = -acos((float)vec_dot(axis, vec)) * factor; // * vec_length(rotAxis);
	ANGLE rotAngle;
	ang_for_axis(rotAngle, rotAxis, angle);
	ang_add(entAng, rotAngle);
}



Originally Posted By: JibbSmart
Secondly, if the vehicle adjusts its angle gradually rather than instantly (add a max_rotation parameter to the look_at function and make sure the function never rotates the vehicle more than the amount given in this parameter -- make it 5*time_step, for example, and the vehicle won't adjust its pan by more than 5 degrees per tick), its turning circle might let the vehicle get far enough away from its destination's pole that it can escape that "inaccurate zone" and continue to its destination.

Could be a solution but... When reaching the "problematic zones" wouldn't it do a wierd little turn before going back to it's correct path? I would like to avoid that strange behaviour if possible.
Another problem I see with this approach is if the destination is close to the moving vehicle, if it's not far enough for it to fully rotate towards it before reaching the destination, it would result in the vehicle going around it's destination in circles forever.

Originally Posted By: JibbSmart
Thirdly, if the vehicle only checks the angle to its destination once a second or so, it should be able to get far enough away from the pole after the first check that any checks after that will be fairly consistent.

I like this idea more, but it leaves the solution to randomness. If by any chance the delay between checks lands close to the "problematic zone" it could result in the vehicle going back and fourth. I don't feel confortable leaving the solution to a random delay that could potencially land on a problematic zone sometimes.


Originally Posted By: Carlos3DGS
EDIT:
I have done further tests drawning an axsis going through the planet's core aligned to our vehicle's normal (blue line).
http://www.youtube.com/watch?v=a-1EbjmkwMk
This proves the problem is not when going through the same point on the opposite side?
current brain status: even more dazed and extremely confused...

EDIT2:
Done further tests, this time debugging V_Path (green line).
V_Path is the temporary path for the pan calculations. It is a rotated version of our destination.
http://www.youtube.com/watch?v=ntPkiiZTV8Y
Aha! Finally found the culprit! It seems the problem occurs when our temporary path is aligned vertically.
(I'm a little less confused now, I understand what is causing this but it seems I can't figure out how to fix it)

I am sorry, I was editing my previous post while you were answering. I wanted to repost this part just in case you missed it. (check the green line in the second video at 1:20 and after)

I think the solution could be to check if V_Path is straight up or down (that seems to be the problem), and if it is aligned vertically then do not rotate the vehicle and let it contiue in a straight line untill the the rotated destination (V_Path) is not vertically aligned anymore. Not sure if that could be the solution, just a random thought...

EDIT:
Nevermind, I think I understood you wrong before. Is this a gimbal-lock problem where I translate the gimbal lock centered around an arbitrary rotated axsis because I used vec_rotateback()?
If so... Is there any way to overcome this problem removing the "vec_to_angle(V_Ang, V_Path);" in the look_at function? And mabe replacing it using your alignToVec() function to perform the calculation?

Last edited by Carlos3DGS; 01/18/12 19:14. Reason: I was bein stupid again...

"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
Re: rotation around a sphere [Re: Carlos3DGS] #392046
01/18/12 19:21
01/18/12 19:21
Joined: Mar 2006
Posts: 3,538
WA, Australia
J
JibbSmart Offline
Expert
JibbSmart  Offline
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
Quote:
I think the solution could be to check if V_Path is straight up or down (that seems to be the problem), and if it is aligned vertically then do not rotate the vehicle and let it contiue in straight line untill the the rotated destination (V_Path) is not vertically aligned anymore. Not sure if that could be the solution, just a random thought...
Yes, that sounds ideal. V_Path is (in theory, anyway) straight up or down when the destination is directly opposite the vehicle's position (that is, if the vehicle was to tunnel straight down through the planet it'd surface at its destination). As such, in this case, there is no "shortest path" to the destination, and the vehicle can move in any direction it wants (ideally, I guess, continue in the direction it was already heading). (EDIT: I see your edit, but I still think this is the ideal solution, once we get that green line to stop wobbling so much laugh )

The way the green line wobbles, it appears that the alignToVec function isn't very accurate, and I think this is the problem:
Quote:
Modified version of your function I am currently using:...
That's actually how I originally wrote it, only to find that rounding errors caused a lot of trouble. This is because vec_length(rotAxis) can get quite small. I added in that multiplication so that the final angle always undershoots the rotation, so it still looks smooth as you approach the appropriate orientation; without it, the final result can overshoot, resulting in weird behaviour (which could well be the cause of that wobbly V_Path).

So I recommend uncommenting that extra bit and seeing if multiple alignToVec(...)s reduce the problem.

I hope that's useful (and correct!).

Last edited by JibbSmart; 01/18/12 19:25.

Formerly known as JulzMighty.
I made KarBOOM!
Re: rotation around a sphere [Re: JibbSmart] #392051
01/18/12 20:22
01/18/12 20:22
Joined: Oct 2008
Posts: 513
Carlos3DGS Offline OP
User
Carlos3DGS  Offline OP
User

Joined: Oct 2008
Posts: 513
Tried it, and I thought we had it... It took a while but finally it happened again. (I guess I was just being lucky for a while)

Results:
http://www.youtube.com/watch?v=3ggFWxYgIhs


Curent version of alignToVec:
Code:
function alignToVec(ANGLE* entAng, VECTOR* vec, VECTOR* axis, var factor) {
	vec_rotate(axis, entAng);
	vec_normalize(axis, 1);
	vec_normalize(vec, 1);
	VECTOR rotAxis;
	vec_cross(rotAxis, vec, axis);
	
	//USE ONLY ONE OF THE FOLLOWING TWO LINES!!!!!!!
	//var angle = -acos((float)vec_dot(axis, vec)) * factor; // instant rotation
	var angle = -acos((float)vec_dot(axis, vec)) * factor * vec_length(rotAxis); //smooth rotation
	
	ANGLE rotAngle;
	ang_for_axis(rotAngle, rotAxis, angle);
	ang_add(entAng, rotAngle);
}



Current version of look_at:
Code:
void look_at(ENTITY* Vehicle, VECTOR* Destination, ENTITY* Planet)
{
	VECTOR V_Nrml;
	ANGLE V_Ang;
	VECTOR V_Path;

	// copy my angle
	vec_set(V_Ang, Vehicle.pan);
	
	//get our vehicle's normal from the planet core
	vec_diff(V_Nrml, Vehicle.x, Planet.x);

	// orientate temporary angle as if on a perfect sphere
	alignToVec(V_Ang, V_Nrml, vector(0, 0, 1), 1);
	alignToVec(V_Ang, V_Nrml, vector(0, 0, 1), 1);
	alignToVec(V_Ang, V_Nrml, vector(0, 0, 1), 1);
	alignToVec(V_Ang, V_Nrml, vector(0, 0, 1), 1);
	alignToVec(V_Ang, V_Nrml, vector(0, 0, 1), 1);

	// get destination position relative to the vehicle's orientation
	vec_diff(V_Path, Destination, Vehicle.x);
	vec_rotateback(V_Path, V_Ang);
	
	//DEBUG path
	vec_set(testv1,V_Path);
		vec_normalize(testv1,1000);
		vec_add(testv1,Planet.x);
		draw_line3d(testv1,NULL,100);
		draw_line3d(testv1,COLOR_GREEN,100);
		draw_line3d(Planet.x,COLOR_GREEN,100);

	// convert to an angle
	vec_to_angle(V_Ang, V_Path);
	
	//AVOID GIMBAL LOCK!!!
	if(V_Ang.tilt>85||V_Ang.tilt<-85)
	{
	   return;
	}

	// now, V_Ang's pan is our angle
	ang_rotate(Vehicle.pan, vector(V_Ang.pan, 0, 0));
}




Spherical movement is such a pain! cry

Also... look closely at the video. The green (V_Path) line does seem to have a problem when vertical.
But... look even closer at the blue line and red dot when I am on the opposite side of the planet than the vehicle (the same side as destination). The vehicle is not opposite of destination when the problem happens! (it is sort of close by, but not directly opposite)

I'm confused again... seems the problem is not when opposite of the destination?!?!?!?


"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
Re: rotation around a sphere [Re: Carlos3DGS] #392057
01/18/12 21:10
01/18/12 21:10
Joined: Mar 2006
Posts: 3,538
WA, Australia
J
JibbSmart Offline
Expert
JibbSmart  Offline
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
That is weird. Hmm... V_Path, in theory, should only be vertical when the destination is on the exact opposite side of the planet to the vehicle. I wonder if alignToVec was more imprecise than I thought, or if there's another issue we're not seeing. Sorry, that's all I've got for now frown


Formerly known as JulzMighty.
I made KarBOOM!
Re: rotation around a sphere [Re: JibbSmart] #392059
01/18/12 21:11
01/18/12 21:11
Joined: Oct 2008
Posts: 513
Carlos3DGS Offline OP
User
Carlos3DGS  Offline OP
User

Joined: Oct 2008
Posts: 513
I made another debug version where I can toggle the planet's INVISIBLE flag to see the vectors' behaviour better (also reduced the vehicle's speed to see the vector movement easier).

I still don't understand why it's happening, but for sure the destination is not opposite of the vehicle.

I thought I would upload a video of this so you can take a look, mabe you can understand what is happening better than I can being able to see the vectors through the planet:
http://www.youtube.com/watch?v=hvLp5axXRjQ
(a problematic test begins at 1:54)

green=V_Path

blue=aligned from vehicle to planet core and out through the other side

red=from vehicle to destination

red dot=destination


"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
Re: rotation around a sphere [Re: Carlos3DGS] #392073
01/18/12 22:39
01/18/12 22:39
Joined: Mar 2006
Posts: 3,538
WA, Australia
J
JibbSmart Offline
Expert
JibbSmart  Offline
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
Okay. It looks to me like V_Ang is not being affected by alignToVec at all -- notice how the green line moves as the vehicle climbs up mountains or tips into valleys. This seems to be the source of the problem -- the vehicle tilts up a mountain, and decides the target is behind it, so it goes back down the mountain, leveling out on flat ground, only to discover the target is behind it again.

This means alignToVec is either not modifying V_Ang, or it's modifying it incorrectly (I have just now noticed that V_Ang also modifies its two VECTOR* parameters, which is really a bad idea [my bad, that is -- that's how I wrote it] -- it should probably be modified to copy those parameters to two local VECTORs and then modify those, so that only the ANGLE is modified at the end).


Formerly known as JulzMighty.
I made KarBOOM!
Re: rotation around a sphere [Re: JibbSmart] #392091
01/19/12 02:14
01/19/12 02:14
Joined: Oct 2008
Posts: 513
Carlos3DGS Offline OP
User
Carlos3DGS  Offline OP
User

Joined: Oct 2008
Posts: 513
I have done more tests to prove your concept, and I think you have found the problem.
I move my vehicle into a valley, and then click on a destination on the opposite side of the planet... He gets stuck every time.
I think you nailed it! Now I'm trying to think of how to fix it. But why would alignToVec not be working?


"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
Re: rotation around a sphere [Re: Carlos3DGS] #392092
01/19/12 03:10
01/19/12 03:10
Joined: Mar 2006
Posts: 3,538
WA, Australia
J
JibbSmart Offline
Expert
JibbSmart  Offline
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
Let's see... well, I guess the first step would be to stop the function modifying the other VECTORs -- only the ANGLE should be modified, and I wonder if re-using the same vectors over and over again after they've been modified has something to do with the problem.
Code:
function alignToVec(ANGLE* entAng, VECTOR* vec, VECTOR* axis, var factor) {
	VECTOR locVec, locAxis;
	vec_set(locVec, vec);
	vec_set(locAxis, axis);
	vec_rotate(locAxis, entAng);
	vec_normalize(locAxis, 1);
	vec_normalize(locVec, 1);
	VECTOR rotAxis;
	vec_cross(rotAxis, locVec, locAxis);
	
	//USE ONLY ONE OF THE FOLLOWING TWO LINES!!!!!!!
	//var angle = -acos((float)vec_dot(locAxis, locVec)) * factor; // instant rotation
	var angle = -acos((float)vec_dot(locAxis, locVec)) * factor * vec_length(rotAxis); //smooth rotation
	
	ANGLE rotAngle;
	ang_for_axis(rotAngle, rotAxis, angle);
	ang_add(entAng, rotAngle);
}

The next thing I would try is to make sure we're passing the correct information -- alignToVec(&V_Ang, &V_Nrml, vector(0, 0, 1), 1);

We add in the "&" to make sure we're passing a pointer to those parameters -- something Lite-C normally does automatically, but under occasional circumstances it won't. If the wrong address is being passed, V_Ang won't get modified, and we get the behaviour we've been seeing.

Fingers crossed!


Formerly known as JulzMighty.
I made KarBOOM!
Page 4 of 5 1 2 3 4 5

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1