Author Topic: direction between two objects  (Read 2034 times)

Offline gamerfan

  • int
  • **
  • Posts: 97
    • View Profile
direction between two objects
« 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?
« Last Edit: December 03, 2011, 11:05:29 am by gamerfan »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: direction between two objects
« Reply #1 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.

Offline gamerfan

  • int
  • **
  • Posts: 97
    • View Profile
Re: direction between two objects
« Reply #2 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.
« Last Edit: December 04, 2011, 07:37:33 am by gamerfan »