0
\$\begingroup\$

How do I make an object (like a bullet) go forward according to its rotation in vanilla nodejs (my game server language)? For example, if an object's rotation is 0 degrees, then it goes straight up.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 0 degrees actually goes to the right \$\endgroup\$ Commented Sep 29, 2016 at 6:00
  • \$\begingroup\$ It depends; although 0 = right and continuing anti-clockwise is most popular, 0 = up and continuing clockwise is also widely used. \$\endgroup\$ Commented Sep 29, 2016 at 6:23
  • \$\begingroup\$ Yeah, but at the minute we're talking about programming language (more specifically JS) where 0 = right is standard (aka. Most big languages use that) \$\endgroup\$ Commented Sep 29, 2016 at 6:51

1 Answer 1

2
\$\begingroup\$

Enter trigonometry!

You need to have an angle and the speed of the bullet to get a velocity vector for the bullet.

Getting a directional vector can be done the following way (v is the vector, x and y are it's components)

v.x := cos(angle) * speed
v.y := sin(angle) * speed

If you don't multiply with speed you get a vector with a length of 1 (a unit vector), which is the directional vector of the bullet

Note: if you have your angle in degrees, then you need to convert in to radians by doing newAngle := angle / 180 * PI

\$\endgroup\$
8
  • \$\begingroup\$ Is trigononetry better than trigonometry? \$\endgroup\$ Commented Sep 29, 2016 at 10:32
  • \$\begingroup\$ @jgallant phone keyboard \$\endgroup\$ Commented Sep 29, 2016 at 10:49
  • 1
    \$\begingroup\$ watermelon hotdog \$\endgroup\$ Commented Sep 29, 2016 at 10:51
  • \$\begingroup\$ Lol i hate it this hear i had to learn geometry yet last year I got a perfect score on geometry... >:( \$\endgroup\$ Commented Sep 29, 2016 at 15:15
  • \$\begingroup\$ @BigBenGamerGuyKSPMC Yep, you need a lot of math for game development, trigonometry, linear algebra (vectors and matrices), etc. \$\endgroup\$ Commented Sep 29, 2016 at 15:21

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.