Sense of an object

I have two vectors vec1 = (170,0,185) and vec2 = (90,0,220).

This code:

[java]System.out.println(vec1.angleBetween(vec2));[/java]



And print:

0.0





Am I doing something wrong?

angleBetween returns (in radians) the angle between two vectors. It is assumed that both this vector and the given vector are unit vectors (iow, normalized).


it must be unit vector :)

A unit vector is a vector of length 1, sometimes also called a direction vector


sorry i dont known it need unit vector. so you should "short" your vector to unit vector to get it work

http://hub.jmonkeyengine.org/javadoc/com/jme3/math/Vector3f.html#angleBetween(com.jme3.math.Vector3f)



Says: It is assumed that both this vector and the given vector are unit vectors (iow, normalized).



Still, 0 is an odd result but you might try normalizing them first. Though I suspect that will not be doing what you want to do since it’s finding the angle between two vectors pointing out of the same spot. I think you want to find the angle between two positions but then I’d question “relative to what”?



What do you want the angle for? In my experience, 99% of the time it isn’t necessary.

Mifth told to use



[java]Transform tr = new Transform();[/java]



@atybmx: so maybe you should try it :wink:

I normalized the vector and it’s work.

But you are right, what I really need it`s know if the object 2 is in right,left,front,back of object 1

Maybe I’m not able to tell you what I want

You need to calculate the position of object 2 relative to the orientation of object 1… ie: if you have object 2’s world position then you can do

http://hub.jmonkeyengine.org/javadoc/com/jme3/scene/Spatial.html#worldToLocal(com.jme3.math.Vector3f,%20com.jme3.math.Vector3f)



…on object 1 with object 2’s position. The position you get will be local to the direction object 1 is facing (ie: it’s orientation). I can’t remember for sure but I think the Z axis is the direction it’s “facing”.



So with the result of world to local:

negative X means it’s left

positive X means it’s right

negative/positive Z mean it’s in front or behind.



Curious, why do you want to do this?

Fantastic

Exactly what I needed



I want to do a small NPC that follows a way,

I have points in the world and the NPC needs to follow a sequence of points, so the NPC needs to know in which direction is the next point to follow it.

Do you Understand?

ahhh, i have done it :slight_smile:

but NPC in my world is a character that stand on terrain

so first i use getHeightmapHeight as Z position.

X and Y rotation you want to rotate “intime” but i have instantly rotate so i can’t help with code.

but for instant(FOR EACH WAYPOINT):



x and z are trully X and Y



[java]

Vector2f dir1 = new Vector2f(0, 5);

Vector2f dir2 = new Vector2f(x - vect.x, z - vect.z);

final double angleRadians = dir1.angleBetween(dir2);

final float angle = -(float) (360 * (angleRadians / (2 * FastMath.PI)));

model.rotateY = angle;

model.setAnim(“move”);

model.moveLocation = new Vector3f(x, y, z);

[/java]

then i just update it in UpdateLoop (Why in UpdateLoop you should know - but if you dont know, i can tell its needed)

[java]

Quaternion roll180 = new Quaternion();

roll180.fromAngleAxis(((2 * FastMath.PI) / 360) * object.rotateY, new Vector3f(0, 1, 0));

modelNode.setLocalTransform(model.node.getLocalTransform().setRotation(roll180));

[/java]

i dont know if i have it properly, but it work for me perfectly :slight_smile:



but from this you can create “intime” rotation :slight_smile: so i think i helped now

updateLoop i mean simpleUpdate(), yes :slight_smile:

and yes, Transform is better, but i think with my solution it’s easy now to do “intime” rotation :slight_smile: (just remember angle to rotate and rotate it in time)