Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (757) Games in Android Showcase (229) games submitted by our members Games in WIP (844) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 vector math question. Help pls!!  (Read 2123 times) 0 Members and 1 Guest are viewing this topic.
hata

Junior Newbie

Java games rock!

 « Posted 2004-10-08 15:01:16 »

My question is I have two objects. ObjA at posA and ObjB at posB. ObjA has a normal(facing) and want to know how much deg to turn in order to face ObjB.

vectorBtoA = posA - posB
arccos(dotproduct(normalize(ObjA's facing)), normalize(vectorBtoA)))

what I get is a degree but don't have a sign to indicate which way to turn. Any suggestion or better approach?
Thank you very much.
Abuse

JGO Ninja

Medals: 64

falling into the abyss of reality

 « Reply #1 - Posted 2004-10-09 15:11:39 »

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16 `Vector ab = objectB.getPosition().minus(objectA.getPosition());ab.normalize();Vector normalA = objectA.getDirection();normalA.normalize();double theta = Math.arccos(normalA.dot(ab));// I've got no idea what this operation is called;// it's kinda like a cross product... but gives a scalar, not a vectordouble cross = ab.x * normalA.y - normalA.x * ab.y;if(cross<0) theta = -theta;`
crystalsquid

Junior Devvie

... Boing ...

 « Reply #2 - Posted 2004-10-10 10:05:38 »

Quote
// I've got no idea what this operation is called;
// it's kinda like a cross product... but gives a scalar, not a vector

double cross = ab.x * normalA.y - normalA.x * ab.y;

if(cross<0) theta = -theta;

Abuse: It's still the cross product
You are generating just the z component of the cross product from the x and y. You can tell whether the y is to the right/left of the x by the sign.

Hata: Do you really need the angle & the sign?
If you just wish to orient the object A to face object B, then the best way is to build the orientation matrix directly, otherwise you will be doing a lot of normalising & trig to get the same result.

For this code, I will assume X is the axis for the 'facing' vector, and Y is up:

(Pseudocode)
 1  2  3  4  5  6  7  8  9  10  11 `// Build the true x axis:vectorX = posA-posB;vectorX.normalize();// Now build a true z axis:vectorY.set(0.0f, 1.0f, 0.0f);vectorZ.cross( vectorX, vectorY );vectorZ.normalise();// Now build the true Y axis:vectorY.cross( vectorZ, vectorX);`

The orientation matrix is actually these three vectors packed into the matrix, either as rows (vectorX, VectorY, VectorZ) or as columns depending on your coordinate scheme.

- Dom
 Games published by our own members! Check 'em out!
hata

Junior Newbie

Java games rock!

 « Reply #3 - Posted 2004-10-10 14:44:19 »

i think Abuse's answer is quite smart and it certainly solves this 2d problem

thank you Crystalsquid also, i think what u r talking about is orthogonal martix, rite? the reason i asked this question is becos i do want to find out the degree and the sign.

Thank you all.
crystalsquid

Junior Devvie

... Boing ...

 « Reply #4 - Posted 2004-10-11 07:25:37 »

Ah, ok.

You can also do this in one step, as the size of the cross product result is the sin of the angle:

 1  2  3  4  5  6  7  8  9  10  11 `Vector ab = objectB.getPosition().minus(objectA.getPosition());  ab.normalize();  Vector normalA = objectA.getDirection();  normalA.normalize();   double cross = ab.x * normalA.y - normalA.x * ab.y; double theta = Math.arcsin(cross);`

- Dom
hata

Junior Newbie

Java games rock!

 « Reply #5 - Posted 2004-10-13 07:38:59 »

that is even better. thanks
Pages: [1]
 ignore  |  Print

 EgonOlsen (45 views) 2018-06-10 19:43:48 EgonOlsen (25 views) 2018-06-10 19:43:44 EgonOlsen (47 views) 2018-06-10 19:43:20 DesertCoockie (202 views) 2018-05-13 18:23:11 nelsongames (127 views) 2018-04-24 18:15:36 nelsongames (126 views) 2018-04-24 18:14:32 ivj94 (867 views) 2018-03-24 14:47:39 ivj94 (128 views) 2018-03-24 14:46:31 ivj94 (771 views) 2018-03-24 14:43:53 Solater (143 views) 2018-03-17 05:04:08
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org