0
\$\begingroup\$

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

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Nov 7, 2011 at 13:18

1 Answer 1

2
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .