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.
-
1\$\begingroup\$ 0 degrees actually goes to the right \$\endgroup\$Bálint– Bálint2016-09-29 06:00:22 +00:00Commented 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\$congusbongus– congusbongus2016-09-29 06:23:57 +00:00Commented 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\$Bálint– Bálint2016-09-29 06:51:42 +00:00Commented Sep 29, 2016 at 6:51
1 Answer
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
-
\$\begingroup\$ Is trigononetry better than trigonometry? \$\endgroup\$jgallant– jgallant2016-09-29 10:32:09 +00:00Commented Sep 29, 2016 at 10:32
-
\$\begingroup\$ @jgallant phone keyboard \$\endgroup\$Bálint– Bálint2016-09-29 10:49:43 +00:00Commented Sep 29, 2016 at 10:49
-
1\$\begingroup\$ watermelon hotdog \$\endgroup\$jgallant– jgallant2016-09-29 10:51:31 +00:00Commented 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\$Big Ben GamerGuyKSPMC– Big Ben GamerGuyKSPMC2016-09-29 15:15:38 +00:00Commented 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\$Bálint– Bálint2016-09-29 15:21:34 +00:00Commented Sep 29, 2016 at 15:21