I am working on a platformer and I don't want to release the whole .fla file because I have lots of classes and such.
I have ran into a problem where I have the length of the tan and the angle needed. I want this bullet to be placed a good 50 pixels away from the player in the angle of where the mouse is located.
I have the the rotation of the gun working smoothly in my game ( you can find my game here: http://www.swfcabin.com/open/1277010772 ) but when I add a bullet to the position using rise over run it doesn't go there.
Here is the code I use for my class:
Actionscript Code:
var distanceX:Number = mouseX - (ourPlayerBody.GetWorldCenter().x* ThePlatformer.pixelize); var distanceY:Number = (mouseY - (ourPlayerBody.GetWorldCenter().y - (ourPlayerBody.GetLocalCenter().y/2))* ThePlatformer.pixelize)* -1;
var angleInRadians:Number = Math.atan2(distanceY, distanceX); var angleInDegrees:Number = angleInRadians *180/Math.PI;
// Hypotenuse, angleInDegrees is our angle
var sineOfAngleInRadians:Number = angleInDegrees *Math.PI/180; sineOfAngleInRadians = Math.sin(sineOfAngleInRadians); var sineOfAngleInDegrees:Number = sineOfAngleInRadians *180/Math.PI; var sineLength:Number = ThePlatformer.bulletDistance* sineOfAngleInDegrees;
var cosineOfAngleInRadians:Number = angleInDegrees *Math.PI/180; cosineOfAngleInRadians = Math.cos(cosineOfAngleInRadians); var cosineOfAngleInDegrees:Number = cosineOfAngleInRadians *180/Math.PI; var cosineLength:Number = ThePlatformer.bulletDistance* cosineOfAngleInDegrees;
var whereToMove:Point = new Point(sineLength, cosineLength * -1); var whereToMoveMeters:b2Vec2 = new b2Vec2(whereToMove.x/ ThePlatformer.pixelize, whereToMove.y/ ThePlatformer.pixelize); bulletBody.SetPositionAndAngle(whereToMoveMeters, angleInDegrees *Math.PI/180);
Local center is half the objects width and height in meters. Global center is the location of where the center of the object is in world's position.
The method I used to try and solve for the position of the bullet I did is shown in the image attachment