What I want to know is if I can calculate an angle similar to how I'm doing in 2D and update the x,y,z position of a bullet based on that angle in a similar manner I'm doing the update function below.
I wrote this function in Ruby to find the target angle between two 2D (x,y) vectors, but now I want to find out how to do this in 3D in a similar way:
def target_angle(point1, point2) x1 = point1 y1 = point1 x2 = point2 y2 = point2 delta_x = x2 - x1 delta_y = y2 - y1 return Math.atan2(delta_y, delta_x) end
Given an object (like a bullet in this case), I can shoot the object given a target_angle between the player (x,y) and the mouse (x,y), as such in the bullet update function:
def update wall_collision # the angle here is the target angle where point 1 is the player and # point 2 is the mouse @x += Math.cos(angle)*speed @y += Math.sin(angle)*speed end
Is there a similar method to calculate a target angle in 3D and use that angle in a similar manner as my update function (to shoot a bullet in 3D)? How can I make this work for two 3D vectors (x, y, z), where you have the player position (x,y,z) and some other arbitrary 3d point away from the player.