Search code examples
pythontrigonometryangle

Using Trig To Calculate dx,dy From Angle (bullet shot from gun)


For a school project on CMU CS I am trying to use trig to calculate the speed/velocity for the dx and dy in order to make it move in the angle that the gun was pointing when shot. The code below is just related to the bullet and gun, thanks for your help! The grid goes from the top right(0) to bottom left (400.) (edit: forgot to mention game is top-down)

import math

player.gunAngle = angleTo(player.centerX,player.centerY,mouseX,mouseY) 
player.rotateAngle = player.gunAngle

-

bullet = Circle(gun.centerX,gun.centerY,4,rotateAngle = player.gunAngle)
bullet.angle = player.gunAngle
bullet.spawnX = gun.centerX
bullet.spawnY = gun.centerY

bullet.angle = math.radians(bullet.angle)

bullet.dx = 3 * math.cos(bullet.angle)
bullet.dy = 3 * math.sin(bullet.angle)

-

for i in bullets.children:
i.centerX += rounded(i.dx)
i.centerY -= rounded(i.dy)

When I shoot it, I think I did something wrong with the calculations but it goes it the wrong direction, even if I shoot multiple times in the same spot they all shoot in the same wrong direction, and im not sure what is wrong with the calculations so I don't know how to fix it. The grid goes from the top right(0) to bottom left (400.)


Solution

  • If you don't need the angle for something else, you can just use the vector components and convert the difference between player and mouse positions into a unit vector, then scale than vector's components by however fast you want the bullet to move:

    To get a unit vector you divide the components by the euclidean magnitude:

    gun_dx = mouseX-player.centerX
    gun_dy = mouseY-player.centerY
    gun_mag = math.sqrt(gun_dx*gun_dx + gun_dy*gun_dy)
    bullet.dx = bullet_speed * gun_dx / gun_mag
    bullet.dy = bullet_speed * gun_dy / gun_mag
    

    You will need to add a check for division by zero magnitude, either here or before firing the gun on mouse event.