James Dawson
James Dawson

Reputation: 5409

Updating a sprite co-ordinate based on its direction

I have a sprite representing a bullet and its basic implementation is as follows:

function Bullet(x, y, rotation) {
    this.x = x;
    this.y = y;
    this.direction = rotation;
    this.speed = 5;
}

Bullet.prototype.update = function() {
    // Move the bullet forward
    this.x = Math.sin(this.rotation) * this.speed;
    this.x = Math.cos(this.rotation) * this.speed;
}

What I'm trying to do here is move the bullet forward in the direction it's facing and relative to its speed. However, when calling the update() method this.x and this.x is NaN.

What's the correct way of making a sprite move in the direction it's facing if given its x, y and rotation information?

Upvotes: 0

Views: 72

Answers (1)

dfsq
dfsq

Reputation: 193311

You have a typo. This:

this.x = Math.sin(this.rotation) * this.speed;

should be

this.x = Math.sin(this.direction) * this.speed;

Upvotes: 1

Related Questions