How would I calculate the amount I must add or subtract to x and y axis to get the cube to move in the direction its facing? The rotation is a float 0 - 360 and 0 is straight up.
\$\begingroup\$
\$\endgroup\$
5
-
\$\begingroup\$ Is the 40° measured counter-clockwise from the right (positive z-axis), as is the mathematical convention? Or are you using a different angle convention? \$\endgroup\$– DMGregory ♦Commented Feb 12, 2018 at 14:39
-
\$\begingroup\$ It is measured clockwise as a float 0 - 360.f \$\endgroup\$– Louis Van Der WaltCommented Feb 12, 2018 at 14:44
-
\$\begingroup\$ 0 being straight up? \$\endgroup\$– DMGregory ♦Commented Feb 12, 2018 at 15:10
-
\$\begingroup\$ Yes, 0 is straight up \$\endgroup\$– Louis Van Der WaltCommented Feb 12, 2018 at 15:11
-
\$\begingroup\$ If you created this diagram in MS Paint, there's a text option where you can type letters rather than drawing them by hand. \$\endgroup\$– AcccumulationCommented Feb 12, 2018 at 19:28
Add a comment
|
1 Answer
\$\begingroup\$
\$\endgroup\$
2
In a coordinate system with:
y+
pointing upx+
pointing rightangle
measured clockwise from straight up
Then a unit vector pointing in the direction given by a particular angle
has:
u.x = sin(angle)
u.y = cos(angle)
Assuming you're careful to convert between degrees & radians or other units as needed for the particular trig library you're using (most work in radians, some work in degrees, some work in fractions of a full circle, etc.)
To move more or less than one unit in this direction, just apply a scalar multiple to both axes:
v.x = distance * sin(angle)
v.y = distance * cos(angle)
-
\$\begingroup\$ This is pretty foundational trig & vector algebra, so you might find you get a lot of benefit by taking a quick web course on the topics, eg. on Khan Academy or YouTube \$\endgroup\$– DMGregory ♦Commented Feb 12, 2018 at 16:26
-
\$\begingroup\$ And if you can render the cube, you should be able to move it, since the displacement from "back" to "front" is the same direction as moving "forward". \$\endgroup\$ Commented Feb 12, 2018 at 19:30