www.jpct.net

jPCT - a 3d engine for Java => Support => Topic started by: gamerfan on December 03, 2011, 10:59:49 am

Title: direction between two objects
Post by: gamerfan on December 03, 2011, 10:59:49 am
I have an enemy and a weapon object.I want to get the direction of the bullet for hitting the enemy.For that, I did like this:

Code: [Select]
SimpleVector direction = (enemy.getTransformedCenter().calcSub( weapon.getTransformedCenter()));
or
Code: [Select]
SimpleVector direction = (enemy.getTranslation().calcSub( weapon.getTranslation()));

or
Code: [Select]
SimpleVector direction = (enemy.getZAxis().calcSub(  weapon.getZAxis()));

Among them, which one I have to use?
Title: Re: direction between two objects
Post by: EgonOlsen on December 03, 2011, 07:55:19 pm
The third one is...something...at least it's a vector, but i've no idea what its value is going to be useful for ;)

The first one is correct and will work in all cases. The second one is also correct if the centers of the objects in question are the origin (or at least close to) and there are no child/parent objects involved. If that's given, the second one is a little faster than the first one.
Title: Re: direction between two objects
Post by: gamerfan on December 04, 2011, 07:19:40 am
Thanks @EgonOlsen. What happens  in my situation is  when the pointer of weapon moves closer to the enemy(almost face to face), Z value becomes greater than zero from third statement.But when the weapon moves away or back to the enemy, the Z value becomes zero and this  happens as long as the enemy is static.May be this kind of behavior can be used to simulate whether the enemy is near or not.Not sure.