Java-Gaming.org
 Featured games (81) games approved by the League of Dukes Games in Showcase (495) Games in Android Showcase (114) games submitted by our members Games in WIP (563) 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 1146 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: 12

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 Member

... 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 Member

... 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.

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 Dwinin (28 views) 2014-09-12 09:08:26 Norakomi (57 views) 2014-09-10 13:57:51 TehJavaDev (74 views) 2014-09-10 06:39:09 Tekkerue (37 views) 2014-09-09 02:24:56 mitcheeb (57 views) 2014-09-08 06:06:29 BurntPizza (45 views) 2014-09-07 01:13:42 Longarmx (28 views) 2014-09-07 01:12:14 Longarmx (34 views) 2014-09-07 01:11:22 Longarmx (34 views) 2014-09-07 01:10:19 mitcheeb (40 views) 2014-09-04 23:08:59
 BurntPizza 34x princec 19x Riven 18x Rayvolution 17x ags1 16x basil_ 15x KevinWorkman 14x kevglass 12x theagentd 12x nsigma 11x LiquidNitrogen 10x SHC 8x The Lion King 7x HeroesGraveDev 7x cylab 6x Gibbo3771 6x
 List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50List of Learning Resources2014-07-31 16:26:06List of Learning Resources2014-07-31 11:54:12HotSpot Optionsby dleskov2014-07-08 01:59:08
 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