Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 12-09-2010, 02:27 PM   #1
bastrix
Registered User
 
Join Date: Sep 2010
Posts: 34
Default [AS3] Calculating angle for shooting bullets

Hi all,

This is what im trying to do:

I have a character in middle of the screen that I can move around. The idea is that I can aim with the mouse, so wherever the mouse is pointing the character will shot a bullet in that direction until its out of the screen.

I've done the maths before where I had the rotation and I could calculate how to move an object depending on the actual rotation value. But how would I do this if I only have the Cords of my character and the mouseX and mouseY cords of where I just clicked?

Any idea?

Thanks

Last edited by bastrix; 12-09-2010 at 02:32 PM.
bastrix is offline   Reply With Quote
Old 12-09-2010, 03:25 PM   #2
hey
Senior Member
 
Join Date: Jul 2009
Location: PA
Posts: 132
Default

Something like this:

ActionScript Code:
dx = object.x - mouseX; dy = object.y - mouseY; Radians = Math.atan2(dy, dx);
Then you can have the projectile move with something like:
ActionScript Code:
Projectile.x += Math.cos(Radians) * 5; Projectile.y += Math.sin(Radians) * 5;

Last edited by hey; 12-09-2010 at 03:29 PM.
hey is offline   Reply With Quote
Old 12-09-2010, 03:27 PM   #3
lordofduct
Senior Member
 
lordofduct's Avatar
 
Join Date: Feb 2008
Location: West Palm Beach, FL
Posts: 3,872
Default

trig yo...


The specific function you want is ArcTangent, and there is a nice arctan that solves the repeating aspect of it (because top and bottom can resolve the same)


Math.atan2(j, i)

where j and i are the members of the vector <i,j> pointing from the player to the mouse.

j = mouseY - player.y
i = mouseX - player.x
__________________
www.lordofduct.com - come read my blog!

If you want to know how to program, take a math class, take a lot of math classes!
lordofduct is offline   Reply With Quote
Old 12-09-2010, 04:15 PM   #4
bastrix
Registered User
 
Join Date: Sep 2010
Posts: 34
Default

Thats what I needed... thanks! I think I'm gonna have go through a few trigonometry tutorials ...
bastrix is offline   Reply With Quote
Old 12-09-2010, 04:23 PM   #5
northmantif
dont Re Member
 
Join Date: May 2009
Location: Poland
Posts: 739
Default

ActionScript Code:
var speed:Number = 10; var bull:MovieClip;//represents bullet dynamically adding from library; var bulletAngle; addEventListener(Event.ENTER_FRAME, moving); function moving(e:Event):void {         mc.rotation = Math.atan2( mc.y -  mouseY,  mc.x - mouseX) / Math.PI * 180 -90;         //mc represents something that should fire the bullet (cannon, soldier etc.)         //and in my case it is a movieClip placed manually on stage } stage.addEventListener(MouseEvent.CLICK, shot); function shot(e:MouseEvent):void {          bull = new Bullet();    //new instance of Bullet                addChild(bull);         bull.x = mc.x         bull.y = mc.y;         bull.rotation = (mc.rotation -90)* Math.PI/180;         bulletAngle = bull.rotation ;         bull.addEventListener(Event.ENTER_FRAME, movingBull); } function movingBull(e:Event):void {     e.target.x += Math.cos(bulletAngle)*speed;     e.target.y += Math.sin(bulletAngle)*speed; }
that's what I use
northmantif is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:35 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.