#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# How to determine if an object is facing another object?

Posts: 3Member
I am writing a basic space ship simulation and I need to tell if one object is facing another object. So the ship can determine if it should fire. (I use an objects x axis directional vector as the direction the object is facing)

At the moment I determine the xy plane of object A and see how far away object B is from this plane. Then I determine the zx plane of object A and see how far away object B is from this plane. Then I use Pythagorus to determine how far away object B is from the line formed by object A's x axis directional vector.

This is very computationaly expansive. Is there a better simpler way to tell if an object is in line with another (ie Object A has object be in it's sights)

Thanks

CB
· ·

• Posts: 129Member
I don't know if there is a better way to do what you are doing. But I have a few ideas how you could limit doing these calculations.

1. Don't do these calculations each frame, only do these calculations
when it is possible for a ship to fire. This keeps calculations down especially in large firefights. And if it is possible to fire, check for range first, and if there are no ships in range do the next calculation for this ship after say 10 frames?

2. I don't know if this one is less costly than Pythagoras, but you could to some extent replace Pythagoras with checking how far the ships are from each other on every axis. That means, if they are in the same cube:

MAX_VECTOR_RANGE is ships actual range, but cube needs to be larger, so that the sphere of shiprange would fit into the cube of ship range

MAX_RANGE=pyth(MAX_VECTOR_RANGE,MAX_VECTOR_RANGE,MAX_VECTOR_RANGE);

if((ship1.x-ship2.x)-MAX_RANGE){
if((ship1.y-ship2.y)-MAX_RANGE){
if((ship1.z-ship2.z)-MAX_RANGE){
...You should check for range with Pythagorus here to get precise answer whether the other ship is in range
...do calculations...
}
}
}

I just noticed I am really weak in explaining things...
But I keep trying

3. This would cost you A LOT of memory. If the space you have is endless, I don't think this is the method to use. Anyway, you could have 3d grid where each smaller cube has list of ships that are in it.

That is about all I could think of at the moment... I hope this helps a bit...

XLoom
· ·
• Posts: 66Member
Hi cb.
Some vector maths would help you out here. To explain briefly, you need to learn about the dot product and normals. The Dot product is great, it tells you exactly when an object is facing another, or even in the general direction or way off. A normal tells you a direction that something is facing. To find if a gun is facing towards a target. Dot product gun's direction normal with the normalised direction vector towards the target. The result will be from -1 to +1. 1 is dead on, -1 is facing away.

· ·
• Posts: 3Member
: Hi cb.
: Some vector maths would help you out here. To explain briefly, you need to learn about the dot product and normals. The Dot product is great, it tells you exactly when an object is facing another, or even in the general direction or way off. A normal tells you a direction that something is facing. To find if a gun is facing towards a target. Dot product gun's direction normal with the normalised direction vector towards the target. The result will be from -1 to +1. 1 is dead on, -1 is facing away.
:
:

Thanks to everybodies ideas and help in this thread.

· ·