A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: rotation problems when firing

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    85

    Question rotation problems when firing

    Hello folks,

    I have a big problem when I try to rotate my projectiles along with the ship's rotation.

    I want my spaceship shoot two shots at once. So I used my first array of projectiles and placed them at the right side of the ship (one shot on the left and one on the right). So far so good.

    But when I shoot with my spaceship and I turn around the rotations of the projectiles look very strange. It's hard to describe so here's an image that shows the error in action.




    I have huge problems rotating the projectiles ALONG with the spaceship so that it looks really good. Currently there is one single shot only. My shooting gets screwed up when I rotate and fire at once.
    The goal is to create a dual firing cannon like in the picture shown.

    Here is some code that places the projectile at the left of the ship (that doesn't really work):

    Code:
    var projectileRadians:Number = (player.frame / 180) * 3.14159;								
    tempProjectile.x = (player.point.x + 3) + 7 * Math.cos(projectileRadians);
    tempProjectile.y = (player.point.y + 3) + 7 * Math.sin(projectileRadians);
    				
    tempProjectile.nextX = tempProjectile.x;
    tempProjectile.nextY = tempProjectile.y;
    			
    tempProjectile.dx = rotationVectorList[player.frame].x;
    tempProjectile.dy = rotationVectorList[player.frame].y;
    This updates the projectile:
    Code:
    nextX += (dx * (speed + Math.abs(xAdjust))) * step;
    nextY += (dy * (speed + Math.abs(yAdjust))) * step;
    Last edited by drpelz; 09-04-2011 at 07:45 PM.

  2. #2
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    I'm not sure if this would work, but couldn't you try setting one of the projectiles to follow the other? Here's what I mean
    Code:
    stage.addEventListener(Event.ENTER_FRAME, followit);
    
    function followit(event:Event):void
    {
    /*
    250 = middle of your stage
    or if your ship is moving along the stage would be: 
    Ship.y
    */
    	if (projectileA.y != 250)
    	{
    		projectileB.y = projectileA.y;
    	}
    	else if (projectileA.y == 250)
    	{
    		projectileB.x = projectileA.x;
    	}
    }

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    85
    Hm...I don't know if that's gonna work. I will try that for sure. Thanks anyway for the hint!

  4. #4
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    Let's assume you have a turret that faces the right when the rotation property is 0. It has 2 barrels that reach out 10 pixels to the right of the origin of the turret, and one barrel is 5 pixels above the origin and the other is 5 pixels below. You want both bullets to appear on the ends of both barrels.
    PHP Code:
    var turretRotationInRadians:Number turret.rotation Math.PI 180;
    bulletLeft.Math.cos (turretRotationInRadians) * 10 Math.sin (turretRotationInRadians) * -5;
    bulletLeft.Math.cos (turretRotationInRadians) * -Math.sin (turretRotationInRadians) * 10;
    bulletRight.Math.cos (turretRotationInRadians) * 10 Math.sin (turretRotationInRadians) * 5;
    bulletRight.Math.cos (turretRotationInRadians) * Math.sin (turretRotationInRadians) * 10;
    //Left is the barrel on the left when the turret is facing north 
    I think that's as complex as it gets. Though it might not be right cause I might have mixed up the positive and negative signs. The last time I used trigonometry was before summer started so..

    For problems like this I tackle them by thinking what formula will get the bullets to appear at the point I want them to (although this sounds like a bad approach to any problem). I'll only explain the x position of the left bullet. When the turret faces east (0 rotation) the bullet's must be +10. Facing south (90 rotation) it must be +5. West is -10. North is -5. So what gives me +10 at 0 rotation and -10 and 180 rotation? That would be
    PHP Code:
    Math.cos (rotation Math.PI 180) * 10
    Then what gives me +5 at 90 rotation and -5 at 270 rotation?
    PHP Code:
    Math.sin (rotation Math.PI 180) * 5
    Put it all together and you get
    PHP Code:
    Math.cos (......) * 10 Math.sin (......) * 5
    This is how I solved this exact same issue when I came across it.

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    85
    Thank you very much! I will try this asap. I hope this will work because other suggestions failed so far...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center