I have trouble trying to work out how to shoot a bullet in 1 of 8 directions.

The player can only point in compass directions 0, 45, 90, 135, 180, etc.

So what I need is a formula that I can use in spawnEntity to send the bullet in the direction the player is facing at a constant speed.

I've tried this;

    sinAngle = Math.cos(90) * 1000;
    cosAngle = Math.sin(90) * 1000;

    bulletX = (this.pos.x);
    bulletY = (this.pos.y);
    return { x: bulletX, y: bulletY, sin: sinAngle, cos: cosAngle };

The call is like this then;

   trajectory = this.calculateAngle();
   ig.game.spawnEntity(EntityBullet, trajectory.x, trajectory.y, { vel: { x: trajectory.sin, y: -trajectory.cos } });

But everything is in radians and I'd like it to be in compass degrees.

有帮助吗?

解决方案

If all you want to do is convert degrees to radians, then you simply use: d * (π/180) where d is degrees.

If you also want to be able to say the compass direction (as in North, South, East, West), create an enum and convert these to degrees and use a degrees-to-radians function.

其他提示

It's been awhile since I've done any game programming, but you can use MathHelper.ToDegrees to convert your radians to degrees.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top