direction between two objects

Started by gamerfan, December 03, 2011, 10:59:49 AM

Previous topic - Next topic

gamerfan

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:


SimpleVector direction = (enemy.getTransformedCenter().calcSub( weapon.getTransformedCenter()));

or

SimpleVector direction = (enemy.getTranslation().calcSub( weapon.getTranslation()));


or

SimpleVector direction = (enemy.getZAxis().calcSub(  weapon.getZAxis()));


Among them, which one I have to use?

EgonOlsen

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.

gamerfan

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