Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (623) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 vector math question. Help pls!!  (Read 1320 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 Knight

Medals: 17

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;

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
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
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

You cannot reply to this message, because it is very, very old.

 BurntPizza (30 views) 2015-04-23 03:42:11 theagentd (35 views) 2015-04-22 16:23:07 Riven (49 views) 2015-04-16 10:48:47 Duke0200 (59 views) 2015-04-16 01:59:01 Fairy Tailz (42 views) 2015-04-14 20:13:12 Riven (44 views) 2015-04-12 21:36:37 bus hotdog (60 views) 2015-04-10 02:39:32 CopyableCougar4 (66 views) 2015-04-10 00:51:04 BurntPizza (70 views) 2015-04-06 22:06:58 ags1 (69 views) 2015-04-02 10:58:48
 theagentd 23x BurntPizza 17x wessles 15x kingroka123 11x alwex 11x 65K 11x kevglass 8x Rayvolution 7x Roquen 7x Riven 7x chrislo27 7x Hanksha 7x Olo 7x Ecumene 7x ra4king 7x KevinWorkman 6x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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