# confused about calculating facing vector

This topic is 2697 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

I've been going through the steering behaviours chapter of Mat Buckland's AI book. In all the examples everything is moving and so the heading / facing vector is always the moving object's normalized velocity vector. In his examples he doesn't calculate a heading vector for anything with a velocity that's practically zero.

I'm trying to create a game object that I can rotate (for example like the ship in Asteroids) and then move in the direction it's facing. Is it simply a case of specifying the look at position and rotating that position by the same number of radians I'm rotating the ship? If I'm specifying a look at position should it be normalized? Or is that just for a velocity vector as you don't need the magnitude just the direction?

My brain's fairly sore at this point as I think I've confused myself by reading examples from too many websites.

Thanks,

T

##### Share on other sites

I'm trying to create a game object that I can rotate (for example like the ship in Asteroids) and then move in the direction it's facing. Is it simply a case of specifying the look at position and rotating that position by the same number of radians I'm rotating the ship? If I'm specifying a look at position should it be normalized? Or is that just for a velocity vector as you don't need the magnitude just the direction?

It sounds like you might be confusing a 'look-at' point with a 'look' or forward vector.

You definitely don't want to use a 'look-at' point for this. It sounds like you're working in 2-d, so (e.g.) a single angle would probably be a reasonable choice for representing the object's orientation. (You could also use a unit-length vector.)

The important thing to note is that you can easily convert back and forth between representations as needed. If the speed has sufficient magnitude, you can compute the orientation either as an angle or as a coordinate basis (two orthonormal vectors) from the velocity. You can also easily build the basis from the angle, and so on. If the speed has small magnitude, you can just keep the previous orientation (in the case of steering behaviors, that is). And so on.

##### Share on other sites

[quote name='theodore italik' timestamp='1297890969' post='4775114']
I'm trying to create a game object that I can rotate (for example like the ship in Asteroids) and then move in the direction it's facing. Is it simply a case of specifying the look at position and rotating that position by the same number of radians I'm rotating the ship? If I'm specifying a look at position should it be normalized? Or is that just for a velocity vector as you don't need the magnitude just the direction?

It sounds like you might be confusing a 'look-at' point with a 'look' or forward vector.

You definitely don't want to use a 'look-at' point for this. It sounds like you're working in 2-d, so (e.g.) a single angle would probably be a reasonable choice for representing the object's orientation. (You could also use a unit-length vector.)

The important thing to note is that you can easily convert back and forth between representations as needed. If the speed has sufficient magnitude, you can compute the orientation either as an angle or as a coordinate basis (two orthonormal vectors) from the velocity. You can also easily build the basis from the angle, and so on. If the speed has small magnitude, you can just keep the previous orientation (in the case of steering behaviors, that is). And so on.
[/quote]

Thanks for replying jyk, I really appreciate it. Sorry for not following up sooner but I haven't had the time.

Using your suggestion of an angle (in radians) to keep track of the rotation, I'm using the below to rotate and move the ship in the desired way. I do intend to clean up it using a Vector class but I wanted to post the code while it's working

 player.position.x += Math.sin(player.rotation) * speed; player.position.y += Math.cos(player.rotation) * speed;

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631706
• Total Posts
3001835
×