AS3 working out fire point of a ship that can rotate

Started by
1 comment, last by Canvas 10 years, 11 months ago

I'm trying to work out where to spawn a bullet from my 40by40 pixel ship, at the moment the ship is facing north, the nose is 20 pixels away from the centre, so here is my code to try and work it out


if( rotate < 0 )
{
actRotate = 360 - (rotate * -1) ;
trace(actRotate);
}
else
{
actRotate = rotate;
}
//var rotateTemp:Number = this.rotation * (180 / Math.PI);
//this.x = xPos;
//this.y = yPos;
_speed_x = Math.sin(rotation*0.0174532925)*7.5;
_speed_y =- Math.cos(rotation*0.0174532925)*7.5;

var noseX:Number = xPos + (NoseOfShip * Math.cos(actRotate));
var noseY:Number = yPos + (NoseOfShip * Math.sin(actRotate));

this.x = noseX;
this.y = noseY;

Ok so let me just cover somethings, if i rotate my ship to the right by 90 it will give 90 as the value, if i rotate the ship by 180 degrees the value of rotate is -180, if i rotate the ship aniclockwise by 90 it is -90. Now i am really confused on why Flash CS6 does this sad.png, so I tried to create a convertor above as you can see, if rotate is minus, then just find out what it was ment to be. now this code only works if the ship is -90 (270) degrees. if the ship is 0,90,-180(180) the bullet is fighted from the nose by to its left by 20 pixels. if someone could give me a hand it would be very helpful, also if you need anymore information please post here

Thank you.

Advertisement
a few things:

1. what is rotation, and what is actRotate? assuming that the nose is always in front of the ship, and the ship always travels forward, then you should just be able to use the ship's rotation, instead of a separate variable.(i'm just assuming here).
2. you only have to add 360 when the value is negative to get it in the range of 0-360. actionscript maps rotations between -180 to 180 instead of 0 to 360.
3. you seem to understand that Math.cos/sin are in terms of radians, but don't apply this when you calculate the noseX/Y. as actRotate is in degrees, and not radians.
4. you don't need to re-map your rotation from [-180,180] to [0, 360]. Math.sin/cos work in a way that the values wrap around infinity, 0 is the same as 360 to the pair of functions.

edit:
5. remember that circles start from the right, if your sprite's ship is drawn vertically such that the nose is at the top of the sprite, then you need to apply a 90 degree offset to your rotation to make it relative to the nose of the ship.
6. you can also do what you did with the speed variables, but for w/e reason your not doing the same when figuring out where the nose is.

Right now i'm kindof assuming your copy+pasting alot of code, and don't really understand what your doing. As your use of sin/cos is right in one spot, but completely diffrent in another.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

Naa i've been coding for ages, but got an assignment to be handed in on Wednesday, and I just want to get something simple to work, also my maths is very bad biggrin.png, I will give that a go and let you know how it goes, ok i have rotated the sprite, but when you say work out the nose? the nose is from the centre to the cannon?

This topic is closed to new replies.

Advertisement