# Move bullet toward player direction

## Recommended Posts

Hi guys, I've searched but I didn't find any answer. May be I didn't know exact search keyword to navigate me to the answer.

My question is, if I have a player sprite that dynamically change its direction (always in rotating left & right) then I want it to shoot toward its direction (not toward mouse pointer or any object, but its direction, its front), what should I do? I've tried to use game.physics.arcade.moveToXY but I don't know how to calculate the x & y for it.

Thank you.

##### Share on other sites

You basically want to have a forward vector in relation to the player's rotation angle, correct?

You can fire a bullet depending on the current rotation of the sprite/player. Given the position of the center of the player (simplified as a circle) and its rotation, you can find the X,Y coordinates of the front. With that, you can then create a forward vector and shoot in that direction.

##### Share on other sites

1 hour ago, Nesh108 said:

You basically want to have a forward vector in relation to the player's rotation angle, correct?

You can fire a bullet depending on the current rotation of the sprite/player. Given the position of the center of the player (simplified as a circle) and its rotation, you can find the X,Y coordinates of the front. With that, you can then create a forward vector and shoot in that direction.

I know how to get current rotation of the player sprite, by using player.angle and then I'll get the rotation angle. But what to do then? How to convert that rotation degree into an X&Y coordinates based on it?

##### Share on other sites

That moves away from gamedev and becomes trigonometry territory.

Just for you:

x = (r * sin( angle )) + player_pos.x

y = (r * cos( angle )) + player_pos.y

r: is the radius of the sprite of the player. From the center of the sprite to the (x,y) position that you calculate will give you the direction of your bullet.

##### Share on other sites

But, I'm sorry I'm still confused how to implement it. I've tried velocityFromRotation() but the result is, bullets fired toward 0,0 coordinate. What did I miss in this code below?

var game = new Phaser.Game(gameWidth, gameHeight, Phaser.AUTO, "SpaceBandits", { preload : preload, create : create, update : update });
var firingAllowed = true;

}

var enemyCounts = 10;

function create() {
game.physics.startSystem(Phaser.Physics.P2JS);
var tempXPos = game.world.width/enemyCounts;
for (var i = 1; i < enemyCounts; i++) {
var enemy = enemies.create(tempXPos * i, 100, 'enemyship');
game.physics.p2.enable(enemy,false);
}
cursors = game.input.keyboard.createCursorKeys();

playerShip = game.add.sprite(game.world.width/2, game.world.height - 100, 'playership');

game.physics.p2.enable(playerShip);

bullets.enableBody = true;
bullets.createMultiple(50, "bullet");
bullets.setAll("checkWorldBounds", true);
bullets.setAll("outOfBoundsKill", true);

};

function update() {
enemies.forEachAlive(moveenemies,this);
if (cursors.up.isDown){
playerShip.body.thrust(100);
}
else if (cursors.down.isDown){
playerShip.body.reverse(100);
}
if (cursors.left.isDown) {playerShip.body.rotateLeft(100);}
else if (cursors.right.isDown){playerShip.body.rotateRight(100);}
else {playerShip.body.setZeroRotation();}

if (this.spaceKey.isDown){
if(firingAllowed){
fire();
firingAllowed = false;
setTimeout(function(){ firingAllowed = true }, 300)
}
}
};

function moveenemies (enemy) {
accelerateToObject(enemy,playerShip,20);
}

function accelerateToObject(obj1, obj2, speed) {
if (typeof speed === 'undefined') { speed = 20; }
var angle = Math.atan2(obj2.y - obj1.y, obj2.x - obj1.x);
obj1.body.force.x = Math.cos(angle) * speed;
obj1.body.force.y = Math.sin(angle) * speed;
}

function fire(){
bullet.reset(playerShip.x, playerShip.y);
}

This codes is modified from one of official Phaser examples.

##### Share on other sites

4 minutes ago, habibieamrullah said:

But, I'm sorry I'm still confused how to implement it. I've tried velocityFromRotation() but the result is, bullets fired toward 0,0 coordinate. What did I miss in this code below?

var game = new Phaser.Game(gameWidth, gameHeight, Phaser.AUTO, "SpaceBandits", { preload : preload, create : create, update : update });
var firingAllowed = true;

}

var enemyCounts = 10;

function create() {
game.physics.startSystem(Phaser.Physics.P2JS);
var tempXPos = game.world.width/enemyCounts;
for (var i = 1; i < enemyCounts; i++) {
var enemy = enemies.create(tempXPos * i, 100, 'enemyship');
game.physics.p2.enable(enemy,false);
}
cursors = game.input.keyboard.createCursorKeys();

playerShip = game.add.sprite(game.world.width/2, game.world.height - 100, 'playership');

game.physics.p2.enable(playerShip);

bullets.enableBody = true;
bullets.createMultiple(50, "bullet");
bullets.setAll("checkWorldBounds", true);
bullets.setAll("outOfBoundsKill", true);

};

function update() {
enemies.forEachAlive(moveenemies,this);
if (cursors.up.isDown){
playerShip.body.thrust(100);
}
else if (cursors.down.isDown){
playerShip.body.reverse(100);
}
if (cursors.left.isDown) {playerShip.body.rotateLeft(100);}
else if (cursors.right.isDown){playerShip.body.rotateRight(100);}
else {playerShip.body.setZeroRotation();}

if (this.spaceKey.isDown){
if(firingAllowed){
fire();
firingAllowed = false;
setTimeout(function(){ firingAllowed = true }, 300)
}
}
};

function moveenemies (enemy) {
accelerateToObject(enemy,playerShip,20);
}

function accelerateToObject(obj1, obj2, speed) {
if (typeof speed === 'undefined') { speed = 20; }
var angle = Math.atan2(obj2.y - obj1.y, obj2.x - obj1.x);
obj1.body.force.x = Math.cos(angle) * speed;
obj1.body.force.y = Math.sin(angle) * speed;
}

function fire(){
bullet.reset(playerShip.x, playerShip.y);
}

This codes is modified from one of official Phaser examples.

And the main issue is in this fire() function:

function fire(){
bullet.reset(playerShip.x, playerShip.y);
}

##### Share on other sites

``````function fire(){
bullet.reset(playerShip.x, playerShip.y);
// velocityFromRotation(rotation, speed, point) → {Phaser.Point}
}``````

##### Share on other sites

27 minutes ago, samme said:
``````function fire(){
bullet.reset(playerShip.x, playerShip.y);
// velocityFromRotation(rotation, speed, point) → {Phaser.Point}
}``````

Many thanks samme, it somehow helps me to get there. I still have to tweak it... Because I see bullet shooting direction is not consistent.