1 decade ago
by wands
I hope someone could help me solve this problem.
Referring to this thread started 6 months back
http://impactjs.com/forums/help/position-bullets-with-rotating-spaceship/page/1
The thread tells us how to position, the firing position at the center and forward.
But if my muzzle is fire from the side instead. What would be the correct mathematical formula?
1 decade ago
by wands
Base on the quote from Dom below. I'd like to edit the formula to allow firing from the side forward instead. How do I change the mathematical formula Dom provided?
Quote from dominic
Cool looking game!
You need a bit of math to spawn the bullet at a rotated (to the ship's angle) and translated (forward to the ship's nose) point.
// Find the center of Player and move the bullet foward by 20px,
var forward = 20;
var sx = (this.pos.x + this.size.x/2) + (Math.cos(this.angle) * forward);
var sy = (this.pos.y + this.size.y/2) + (Math.sin(this.angle) * forward);
ig.game.spawnEntity(EntityBullet, sx, sy, {angle:this.angle});
This assumes your player&039;s #.angle
already is in radians, otherwise just use .toRad()
to convert (it&039;s a shorthand for the #*Math.PI/180
):
var sx = (this.pos.x + this.size.x/2) + (Math.cos(this.angle.toRad()) * forward);
var sy = (this.pos.y + this.size.y/2) + (Math.sin(this.angle.toRad()) * forward);
Also note that I&039;m passing the player's angle to the Bullet entity in the settings parameter (
{angle:this.angle}). This will directly set a #.angle
property on the Bullet entity in the this.parent();
init.
init: function( x, y, settings ) {
this.parent( x, y, settings ); // This will write the .angle property to 'this'
this.vel.x = Math.cos(this.angle.toRad() * this.maxVel.x;
this.vel.y = Math.sin(this.angle.toRad() * this.maxVel.y;
this.addAnim( 'idle', 0.2, [0] );
//rotate the angle of the animation sprite
this.currentAnim.angle = this.angle.toRad();
},
Again, mind the .toRad()
if you need it. Also, you shouldn&039;t have to set #.accel
when you set .vel
, and I&039;m not sure what the #bulletOffSet
was for.
1 decade ago
by Joncom
Quote from wands
my muzzle is fire from the side instead
I'm not sure I understand your question. What's wrong with the formulas provided? Positioning from the center-out WILL fire from the side.
1 decade ago
by wands
Hi.
What I meant was to fire on the left or right
The code finds the center of the entity and forward it. It's essentially still firing in the center of the entity. But say if I want to fire on the left or right instead?
1 decade ago
by Joncom
I think this is what you mean...
/* How to spawn a bullet to the side */
var distanceFromCenter = 10;
var x = this.pos.x + this.size.x/2; // center of entity x
var y = this.pos.y + this.size.y/2; // center of entity y
// Pick a side.
x = centerX + (this.isFacingLeft ? -1 : 1) * distanceFromCenter;
// Spawn bullet.
ig.game.spawnEntity(EntityBullet, x, y);
1 decade ago
by wands
Thanks for the reply.
If this is rotated, will I get a different result?
My player rotates and moves.
Riffing off of Joncom's code:
/* How to spawn a bullet to the side */
var distanceFromCenter = 10;
var x = this.pos.x + this.size.x/2; // center of entity x
var y = this.pos.y + this.size.y/2; // center of entity y
// rotate!
x += Math.cos(this.angle) * distanceFromCenter * (this.flip.x ? -1 : 1);
y += Math.sin(this.angle) * distanceFromCenter * (this.flip.y ? -1 : 1);
// Spawn bullet.
ig.game.spawnEntity(EntityBullet, x, y);
1 decade ago
by wands
Will test this out. Thank you
Page 1 of 1
« first
« previous
next ›
last »