Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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 1158 times)
0 Members and 1 Guest are viewing this topic.
Offline 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.
Online 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 vector

double 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
Offline 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 Wink
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!
Legends of Yore - The Casual Retro Roguelike
Offline 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  Cheesy

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

Junior Newbie




Java games rock!


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

that is even better. thanks Smiley
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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (20 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (64 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (42 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!