ivknv
ivknv

Reputation: 305

Problems with angle and target coordinates

When player shoots Bullet class is being initialized.

class Bullet(BaseClass):
    def __init__(self, x, y, tx, ty, angle):
        ...
        self.tx, self.ty = tx, ty
        # Here's the problematic part
        self.tx += random.uniform(-15, 15)
        self.ty += random.uniform(-15, 15)

        self.angle = get_angle(x, y, self.tx, self.ty)
        ...
        self.velx, self.vely = get_vel(self.angle, 18)

def get_angle(x1, y1, x2, y2):
    rise = y1 - y2
    run = x1 - x2
    angle = math.atan2(run, rise)
    angle = angle / (math.pi / 180)
    return angle

def get_vel(angle, offset):
   return (math.sin(angle * (math.pi / 180)) * offset, math.cos(angle * (math.pi / 180)) * offset)

When distance between x and tx or y and ty is small angle is getting weird. Sometimes bullets even go the opposite direction.

How do I fix that?

Upvotes: 0

Views: 68

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798546

You scale the constraint based on distance, up to a maximum value. It makes no sense for the aim point to be further in distance from the target than the shooter.

Upvotes: 1

Related Questions