Just enough trig to fire bullets in a space shooter! From the #librelounge channel on irc.freenode.net trying to remember just enough trigenometry to make bullets move on angles [14:59] which I think is honestly not much but I remember almost nothing dustyweb: is this linear motion or by "move on angles" you mean change their angle as they travel? [15:00] davexunit: just linear motion, so I think it's almost nothing. Probably to aim and fire at the enemy ship I just calculate the angle and then just extend from the initial position outward along the tangent? [15:01] leaving aiming aside for a moment. if you want to make something move at an angle (let's call it "theta") at a certain speed you can do this: dx = cos(theta) * speed, dy = sin(theta) * speed [15:02] (dx, dy) becomes your bullet velocity vector. [15:03] davexunit: I think I see that seems very simple theta is assumed to be in radians, not degrees, in the above example. [15:04] radians are the right way to do it yes :) to make one of your ships aim at something, you need to first calculate the difference between the target position and the ship's position. [15:06] let's call that (dx, dy) again. it's a vector going from the ship that is shooting towards the target. then you need to normalize that vector to get a vector that is 1 unit in total length. (I can explain this further if you want) [15:07] then finally you multiply the normalized vector by the speed of the bullet being fired and you have your desired velocity vector. 4| T 3| 2| 1| S 0| '------------ 0 1 2 3 4 5 so if S is shooter and T is target S is at (0,1) T is at (4,4) [15:09] dx would be 4 dy would be 3 (4,4) - (0,1) = (4,3) so yeah you are correct yeah so next I need to calculate the angle I guess if you used that as your velocity, the bullet would reach the target instantaneously so, if you want to shoot *directly* at the target, you don't need to compute the angle. press fire to win [15:11] teleport bullet directly to target do you know what the magnitude of a vector is? nope :D and/or are you familiar with the distance formula between two points? I am familiar with that, yes [15:12] no worries, it's so simple. it's just M-x magnitude * dustyweb checked to see if that existed foiled! ;) I bet M-x calc has it though so, magnitude is the same thing as calculating the distance between two points, only simpler. so our vector is (4,3) right? [15:13] yes let's calculate the distance between that point and (0,0) sqrt((4 - 0) ^ 2 + (3 - 0) ^ 2) simplified: sqrt(4 * 4 + 3 * 3) or 5 [15:14] right because a^2 + b^2 = c^2 right? yes you can derive the distance formula that way take generic vector (dx,dy) magnitude = sqrt(dx * dx + dy * dy) that tells you how long the vector is [15:15] that's cool and stuff, but we can use it to "normalize" a vector. which means we're gonna turn that vector of length 5 into a vector of length 1. doing that is also easy. the normalized vector is (dx / magnitude, dy / magnitude) [15:16] why did we want to do that? because now we can *scale* that vector by the bullet speed to get the desired velocity for the bullet! hold up, I think I get it but let me double check :) yeah I'm pausing here the distance, by the distance formula, is 5 long. so, if we're assuming we're moving at "one unit per second" "one space unit per time unit" if we want to get *real* generic ;) then we can simply divide the vector by the length of the line segment yes [15:18] sorry but yes, you're right and then, for instance, in 5 time units it would reach the target that makes a *heck* of a lot of sense thanks davexunit :) normalizing a vector is a way to make direction vectors. [15:19] so if you want the bullet two move 2 space units per tick, multiply the normalized vector by 2. to move* great :D [15:20] this was an awesome tutorial davexunit [15:21] exactly what I needed if you want to do something more complex, like aim a bit off center of the target, then you need to do something different, but I didn't want to open that particular can of worms. davexunit: well couldn't a "lazy" way to do that just be change where the target is by offsetting it yes eg, maybe we want to fire two spaces in front of the target because it's moving towards us and that's going to often be close enough [15:22] cool great something simple like that could work these are bots firing at the player, they don't have to be galaxy brain :) they shouldn't even :) sometimes situations arise where you need to compute the exact angle between two points, and for that I believe you need to invoke the arctan function but sounds like you won't need that. davexunit: I think I won't [15:23] arctan(dy,dx) will get you the angle of the vector, though. just fyi ;) davexunit: this was a great tutorial. mind if I pastebin it and post it to the fediverse? maybe some other people will benefit :) [15:24] sure! good luck with terminal phase thank you davexunit you're welcome oh one gotcha: don't normalize a vector of length 0 because then you get divide by 0 errors [15:33] an enemy trying to shoot at something directly on top of it could create that scenario. davexunit: ah! good point. [15:36]