Skip to main content
Fixed a logic error
Source Link
Feltope
  • 471
  • 3
  • 10

Just offset the shot when you instance a new bullet.

I assume your always working in world space given how you instance a new bullet so like this.

Vector2D offset = playerPositionplayer.velocity.perpendicular().normalize().scaleBy(playerWidth * 0.5);

// Some vector.perpendicular() functions will be left handed some will be right.
// so you might have to negate this offset value to get it on the proper side of the player.

var bulletPosition = playerPosition.clone().add(offset); // Copy of the player position

// now you should have no problems calculating your angle etc since you have a valid starting point for your bullet.

Edit: I just noticed that your also calculating the direction of travel for the bullet from the player's position and not the bullets position when you first instantiate a new bullet.

var bulletDirection = Vector2D.substract(targetPosition, bulletPosition).normalize(); // Difference between the bullet and the target, normalized
new Bullet(bulletPosition, bulletDirection);

Edit: Your right I screwed up. You need to use the players facing as your offset.

// If the player's velocity is independent to the player rotation 
// (ie. a space game or drifting etc) use the rotation as the vector your facing.
Vector2D offset = player.velocity.perpendicular().normalize().scaleBy(playerWidth * 0.5);
bulletPosition = player.position.add(offset);

you should now be able to calculate the direction of travel of the new bullet like I did above.

Just offset the shot when you instance a new bullet.

I assume your always working in world space given how you instance a new bullet so like this.

Vector2D offset = playerPosition.perpendicular().normalize().scaleBy(playerWidth * 0.5);

// Some vector.perpendicular() functions will be left handed some will be right.
// so you might have to negate this offset value to get it on the proper side of the player.

var bulletPosition = playerPosition.clone().add(offset); // Copy of the player position

// now you should have no problems calculating your angle etc since you have a valid starting point for your bullet.

Edit: I just noticed that your also calculating the direction of travel for the bullet from the player's position and not the bullets position when you first instantiate a new bullet.

var bulletDirection = Vector2D.substract(targetPosition, bulletPosition).normalize(); // Difference between the bullet and the target, normalized
new Bullet(bulletPosition, bulletDirection);

Just offset the shot when you instance a new bullet.

I assume your always working in world space given how you instance a new bullet so like this.

Vector2D offset = player.velocity.perpendicular().normalize().scaleBy(playerWidth * 0.5);

// Some vector.perpendicular() functions will be left handed some will be right.
// so you might have to negate this offset value to get it on the proper side of the player.

var bulletPosition = playerPosition.clone().add(offset); // Copy of the player position

// now you should have no problems calculating your angle etc since you have a valid starting point for your bullet.

Edit: I just noticed that your also calculating the direction of travel for the bullet from the player's position and not the bullets position when you first instantiate a new bullet.

var bulletDirection = Vector2D.substract(targetPosition, bulletPosition).normalize(); // Difference between the bullet and the target, normalized
new Bullet(bulletPosition, bulletDirection);

Edit: Your right I screwed up. You need to use the players facing as your offset.

// If the player's velocity is independent to the player rotation 
// (ie. a space game or drifting etc) use the rotation as the vector your facing.
Vector2D offset = player.velocity.perpendicular().normalize().scaleBy(playerWidth * 0.5);
bulletPosition = player.position.add(offset);

you should now be able to calculate the direction of travel of the new bullet like I did above.

added 394 characters in body
Source Link
Feltope
  • 471
  • 3
  • 10

Just offset the shot when you instance a new bullet.

I assume your always working in world space given how you instance a new bullet so like this.

Vector2D offset = playerPosition.perpendicular().normalize().scaleBy(playerWidth * 0.5);

// Some vector.perpendicular() functions will be left handed some will be right.
// so you might have to negate this offset value to get it on the proper side of the player.

var bulletPosition = playerPosition.clone().add(offset); // Copy of the player position

// now you should have no problems calculating your angle etc since you have a valid starting point for your bullet.

Edit: I just noticed that your also calculating the direction of travel for the bullet from the player's position and not the bullets position when you first instantiate a new bullet.

var bulletDirection = Vector2D.substract(targetPosition, bulletPosition).normalize(); // Difference between the bullet and the target, normalized
new Bullet(bulletPosition, bulletDirection);

Just offset the shot when you instance a new bullet.

I assume your always working in world space given how you instance a new bullet so like this.

Vector2D offset = playerPosition.perpendicular().normalize().scaleBy(playerWidth * 0.5);

// Some vector.perpendicular() functions will be left handed some will be right.
// so you might have to negate this offset value to get it on the proper side of the player.

var bulletPosition = playerPosition.clone().add(offset); // Copy of the player position

// now you should have no problems calculating your angle etc since you have a valid starting point for your bullet.

Just offset the shot when you instance a new bullet.

I assume your always working in world space given how you instance a new bullet so like this.

Vector2D offset = playerPosition.perpendicular().normalize().scaleBy(playerWidth * 0.5);

// Some vector.perpendicular() functions will be left handed some will be right.
// so you might have to negate this offset value to get it on the proper side of the player.

var bulletPosition = playerPosition.clone().add(offset); // Copy of the player position

// now you should have no problems calculating your angle etc since you have a valid starting point for your bullet.

Edit: I just noticed that your also calculating the direction of travel for the bullet from the player's position and not the bullets position when you first instantiate a new bullet.

var bulletDirection = Vector2D.substract(targetPosition, bulletPosition).normalize(); // Difference between the bullet and the target, normalized
new Bullet(bulletPosition, bulletDirection);
Source Link
Feltope
  • 471
  • 3
  • 10

Just offset the shot when you instance a new bullet.

I assume your always working in world space given how you instance a new bullet so like this.

Vector2D offset = playerPosition.perpendicular().normalize().scaleBy(playerWidth * 0.5);

// Some vector.perpendicular() functions will be left handed some will be right.
// so you might have to negate this offset value to get it on the proper side of the player.

var bulletPosition = playerPosition.clone().add(offset); // Copy of the player position

// now you should have no problems calculating your angle etc since you have a valid starting point for your bullet.