Firing bullet with rotation

18 posts

Flag Post

ok, i know how to fire a bullet from say a tank or sumthing using arctan and the xmouse and ymouse cords, but how can i fire with accuracy using just rotation. I have a game that has a stationary gun that rotates the whole game and i want to be able to fire a bullet out of it when i hit spacebar or so. what math would i need to send the bullet from the barrel toward the position of the gun when i hit spacebar. can neone help me out?? thx

 
Flag Post

Put this on the bullet, or change appropriately if your using it in the main timeline:

onClipEvent (load) {
	this._rotation = _root.tank._rotation;
	rad = this._rotation * Math.PI/180;
	speed = 5;
}
onClipEvent (enterFrame) {
	this._x += Math.sin(rad)*speed;
	this._y += Math.cos(rad)*speed;
}

change the asterisk to a ‘*’
it should work, if it doesn’t or you need anything else just ask.

 
Flag Post

ok thx alot i need this hopefully it works

 
Flag Post

Sure thing. the code stuffed up with the ‘*’ so before and after the bold starts just put an asterisk.

 
Flag Post

Fixed your post. Use the pre-formatted text tags when posting code.

Also, you’ll want to move away from onClipEvents. They are a terrible, outdated code practice.

 
Flag Post

Yeah the math looks okay, but the onClipEvents are bad coding practice.

 
Flag Post

lol thanks for fixing my post. And I never use code on MCs anymore, just did it so he could easily convert to whatever method he was using (duplicateMC, linking to the library or w/ever). If you dont want it like that shake, use this on the main timeline:

_root.bullet._rotation = _root.tank._rotation;
rad = _root.bullet._rotation * Math.PI/180;
speed = 5;
onEnterFrame = function(){
    _root.bullet._x += Math.sin(rad)*speed;
    _root.bullet._y += Math.cos(rad)*speed;
}
 
Flag Post

root.bullet.rotation = root.tank.rotation;
rad = root.bullet.rotation * Math.PI/180;
speed = 5;
onEnterFrame = function(){
root.bullet.x += Math.sin(rad)speed;
root.bullet.y += Math.cos(rad)
speed;
}

This was the “_root level” version

Here is the AS 2.0 version:

var rotationB:Number = bullet._rotation;
var rotationT:Number = tank._rotation;
var speed:Number = 5;

this.onEnterFrame = function():Void {
   rotationB = bullet._rotation;
   var radian:Number = rotationB * Math.PI / 180;
   bullet._x += Math.cos(radian) * speed;
   bullet._y += Math.sin(radian) * speed;
}

Also note that the cos and sin function calls were reversed in your example, KaBomb, because cosine is the x axis and sine is the y axis. Yeah I shoulda noticed that before… oops.

 
Flag Post


You dont want to put your degree to radian conversion math inside an EnterFrame loop (very common mistake). Since its a constant value, you calculate it once and define it in a variable you can use later without recalculating:


// Degree to radian value (0.017453292519943295)
public static var DTR:Number = Math.PI / 180;


It can then be used as so:


var radian:Number = rotation * DTR;


Is AS3 you can define it as a static const (static constant) in a class as well.


 
Flag Post

thx guys that just fixed it all up, but one thing, this doesnt have to do with the topic, but is there a way of using the x and y cords of a movie clip inside of another movieclip, but using the root cords, so if im like bullet._x = gun.barrel.x ( the x cord is like 4 because its in the mc ) but i wanna use _root cord, so say if the gun is in the middle of a 300×300 screen the bullet would go to say (300×296). i hope im being clear, lol. ok thx

 
Flag Post

Yes, use the localToGlobal and globalToLocal methods. Look them up in the Flash help and come back if you have trouble. You can also to the same thing with simple math at times, just by adding the coordinates of the parent clips to the objects coordinates. eg if root.clip.bullet.x is 50 and root.clip.x is 200, then the bullets global _x coordinate would be 250 ( root.clip.x + root.clip.bullet.x ). That works best when the clip isnt nested too far. If it is nested pretty far down, it gets a bit more involved.

 
Flag Post

You dont want to put your degree to radian conversion math inside an EnterFrame loop (very common mistake).

This is presumably to avoid making Math function calls every frame to save resources. That makes sense :)

 
Flag Post

This is presumably to avoid making Math function calls every frame to save resources


Yes, but its not only that, its also to avoid calculating a number every frame that will never ever change. Math.PI/180 will always be 0.017453292519943295769 (or so), so its not optimal to ask flash every frame to figure it out when its just going to come up with the same answer. The same goes for any other constant number.

 
Flag Post

oh ok thx alot, i didnt know there was a method for that, ill try it out.

 
Flag Post

ive tried to do that, and idk if im doin it right, but I created an object called myPoints with 2 props, x_ and y_. but then i try to set them as equal to a movieclip( root.bulletChecker.point.x) or w/e it does not change, it is always the same value. neone know whats wrong?

 
Flag Post

Your props in the point object need to be x and y not x_. The easy was is to just create a new Point() object.



import flash.geom.Point;

var mc:MovieClip = this.createEmptyMovieClip(“clip”, 1 );
mc.x = 100;
mc.
y = 100;

var submc:MovieClip = mc.createEmptyMovieClip( “subclip”, 1 );
submc._x = 10; // x for movieclip x position
submc.
y = 10; // _y for movieclip y position

// Create a point based on the position of the sub clip
var myPoint:Point = new Point ( clip.subclip.x, clip.subclip.y ) ;

trace ( myPoint ); // (x=10, y=10)

// Convert
mc.localToGlobal( myPoint );

trace ( myPoint ); // (x=110, y=110)


That example creates everything, but of course in your game the clips would already exists, so all you really need to do is create the point object and then convert it from local to global, then it will have the info you need. Note that it doesnt do anything to the clip, it just modifies the point object in order to give you the right numbers.



import flash.geom.Point;
var myPoint:Point = new Point ( clip.subclip.x, clip.subclip.y ) ;
clip.localToGlobal( myPoint );


Replace the names with the instance names in your file.

 
Flag Post

oh i never saw the import header file for it. thx tho. yeah i knew about the x and y thing i was just trying to make an example, sry lol.

 
Flag Post

A ‘dumb’ bullet with constant speed and rotation just needs the dX & dY calculated on load, then applied every frame.