Home Tutorials Download Beta Store Forum Documentation KnowledgeBase Wiki Blog

ShiVa3D

Return to Game programming

angle between

All about the StoneScript

angle between

Postby harold35 » 20 Apr 2010, 08:52

I have see a post on the forum, about how to calculaten angle between 2 points in 3d space with this link :

http://www.euclideanspace.com/maths/alg ... /index.htm

So to follow the method to calculate an angle i have made this code :

Code: Select all
local  xv1,yv1,zv1 = object.getTranslation (this.getObject ( ),object.kGlobalSpace)---- position du vehciule
local xv2,yv2,zv2 = x,y,z ---- position de la prochaine balise

xv1,yv1,zv1 = math.vectorNormalize (xv1,yv1,zv1  ) --- normalisation position du vehicule
xv2,yv2,zv2 = math.vectorNormalize (xv2,yv2,zv2  ) --- normlasation position de la balise

local dotproduct1 = math.vectorDotProduct (xv1,yv1,zv1,xv2,yv2,zv2  )--- dotproduct des deux positions

local value= math.acos (dotproduct1) 
log.message (value)


But i don't understand the result... I thinking that with this method can expect a result between 0 and 180... But in fact not... Can somebody with a better mathematic level can help me to solve this problem ?
harold35
Platinum Boarder
Platinum Boarder
 
Posts: 464

Re:angle between

Postby Piedcarre » 20 Apr 2010, 09:06

In fact, the angle you want is not in the 3D space but in the (x,z) plane, am i wrong?
Secondly, I think the 2 vectors you use are not correct, you use objects translations instead of directions.

In a few words, try with directional vectors, and set their Y direction to 0.
User avatar
Piedcarre
Platinum Boarder
Platinum Boarder
 
Posts: 433

Re:angle between

Postby harold35 » 20 Apr 2010, 09:41

ok thank i will try this :)
harold35
Platinum Boarder
Platinum Boarder
 
Posts: 464

Re:angle between

Postby harold35 » 20 Apr 2010, 12:52

hummm... So i try with this method... I have a result between 0 and 180 but it doesn't solve my problem finaly...

I work on a car IA that must follow dumies... My car must look at the nex dumy to make is path... So i need to know angle between my car an the next point to give to my car the good orientation...

I have made a test with object.lookat, it work fine but i want to use méthod with an angle and orientation because it wood be better if the player and the ia have the same speed rotation...

Somebody have any idear of what is wrong in my mathematics ?
harold35
Platinum Boarder
Platinum Boarder
 
Posts: 464

Re: angle between

Postby gn0me » 03 Apr 2012, 22:10

That's a pretty old topic, but the problem is not solved.
I found the solution myself, may be useful for someone.
It works only for a 2d space ( actually it might work for 3d with some changes )

Code: Select all
-- Vectors must be normalized.

local aX, aY, aZ = math.vectorNormalize ( X,0,Z ) -- 1st vectors
local bX, bY, bZ = math.vectorNormalize ( dirx,0,dirz ) -- 2nd vector
local angle1=math.atan2(aZ,aX)
local angle2=math.atan2(bZ,bX)

-- add 360 so we got only positive values and get rid of one of problems
angle1=amgle1+360
angle2=amgle2+360

local FinalAngle =  angle1 - angle2

-- Check if one of the angles are in the wrong quadrant
-- and fix it if so.

if ( (math.abs ( FinalAngle ))>180 ) then
if (angle1<angle2)then
    angle1=angle1+360
else
    angle2=angle2+360
end
end

FinalAngle =  angle1 - angle2

| Shiva 1.9.2b2 / Celeron G530 / HD 6770 | Android 4.04 |
User avatar
gn0me
Expert Boarder
Expert Boarder
 
Posts: 86
Location: pl


Return to Game programming