Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a line ahead of my moving object to serve as a raycast and its aligned with the object's movement direction. But I want also some extra lines according to an angle related to the 'head' of the moving object.

This position is always changing; I also have a velocity vector to see the direction of the object as well, I use that to trace the front line ray of my object but not sure how to use to calculate this new line.

In this picture, my object is moving to the right and up. I want to trace a new line but 30 degrees from this one. How do I do that?

enter image description here

share|improve this question
1  
The problem is underspecified. There is an infinite set of lines at 30 degrees to a given axis and passing through a specified point on that axis, and they form a cone. You'll need another constraint or two to determine which of those lines to draw. –  Peter Taylor Nov 7 '11 at 13:18
add comment

1 Answer

Assuming you're talking about 2D, the rotational matrix in general is ...

\begin{bmatrix} \cos \theta & -\sin \theta \ \sin \theta & \cos \theta \end{bmatrix}

Or, for 30 degrees to the right (\theta = -\frac{\pi}{6}) specifically ...

\begin{bmatrix} \frac{\sqrt{3}}{2} & \frac{1}{2} \ -\frac{1}{2} & \frac{\sqrt{3}}{2} \end{bmatrix}

Multiply your directional vector by this matrix to get your new directional vector.

Alternatively, you can use complex numbers for rotation. Your directional vector is then d = x + iy with i2 = -1, and the number to multiply it by to get a rotation by is r = \cos \theta + i \sin \theta - or in your specific case:

r = \frac{\sqrt{3}}{2} - \frac{1}{2} i

In 3D, you'll need to define what you mean by "30 degrees to the side" first. As Peter Taylor correctly comments, there is a whole cone of directional vectors which fulfil this constraint.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.