To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > General Help > Math and Physics

Reply
 
Thread Tools Search this Thread Display Modes
Old 07-29-2009, 08:56 PM   #1
adamvan101
Junior Member
 
Join Date: Jul 2009
Posts: 22
cos/sin * distance from guntip to rotation point?

hello,
i want to get shot bullets to appear at the tip of the gun instead of the center of the body. there's a similar thread here (actually its exactly what i want) but i dont understand the solution by asgrunt.

http://www.actionscript.org/forums/showthread.php3?t=213237

can anyone help please?

thanks,
adamvan101
adamvan101 is offline   Reply With Quote
Old 07-30-2009, 03:46 AM   #2
Nialsh
Sporadic FK User
 
Nialsh's Avatar
 
Join Date: Dec 2003
Location: Houston
Posts: 317
Yeah, that was a pretty vague answer. The idea behind this is that you're representing things as vectors. The following are vectors that you'll deal with:
A. The reference point of the character (_x, _y)
B. The offset of the shoulder from the character's reference point
C. The offset of the tip of the gun from the shoulder

Adding these will give you the position of the tip of the gun, with respect to the origin.

G = A+B+C

The components of the G will be helpful for placing your bullet:
Gx = Ax + Bx + Cx
Gy = Ay + By + Cy

I don't know exactly what variables you have, but your final answer will probably look something like this:

Code:
bullet._x = character._x + Bx + L*Math.cos(angle)
bullet._y = character._y + By + L*Math.sin(angle)
'angle' is the angle of the arm in radians and L is the length of the arm (probably a constant).

Bx and By are constants too. In my image, I took a guess that your reference point is at the bottom of the character, in which case you would set them to about 0 and 20, respectively. That may not be the case for you, so play with the numbers and see what looks good.

Hope this helps. You might want to read a bit about vectors.

Neal
Attached Images
File Type: png BOT.png (28.1 KB, 33 views)

Last edited by Nialsh; 07-30-2009 at 04:12 AM.
Nialsh is offline   Reply With Quote
Old 07-30-2009, 09:58 AM   #3
adamvan101
Junior Member
 
Join Date: Jul 2009
Posts: 22
i definitely will read about vectors haha. thanks for a more detailed answer. one queston: my game is a birds-eye-view so its slightly different than that robot one. should 'L' be the length from the rotation point of my character (the center) to the end of the gun?

thanks again!,
adamvan101
adamvan101 is offline   Reply With Quote
Old 07-30-2009, 04:45 PM   #4
Nialsh
Sporadic FK User
 
Nialsh's Avatar
 
Join Date: Dec 2003
Location: Houston
Posts: 317
Sounds right. And your B term will probably be zero.
__________________
undefined
Nialsh is offline   Reply With Quote
Old 07-30-2009, 05:58 PM   #5
adamvan101
Junior Member
 
Join Date: Jul 2009
Posts: 22
i tried to do that. i had this:
PHP Code:
setProperty(bullet, _x, char._x+L*Math.cos(angle));
and same with y but Math.sin.
L = 15, then i tried with 30. do neither worked. do the values for L seem right?
adamvan101 is offline   Reply With Quote
Old 07-31-2009, 01:23 PM   #6
Nialsh
Sporadic FK User
 
Nialsh's Avatar
 
Join Date: Dec 2003
Location: Houston
Posts: 317
What behavior did you get?
__________________
undefined
Nialsh is offline   Reply With Quote
Old 07-31-2009, 11:03 PM   #7
adamvan101
Junior Member
 
Join Date: Jul 2009
Posts: 22
i dont even know exactly what it was. the best i can say is the bullet appeared in random places near the character everytime
adamvan101 is offline   Reply With Quote
Old 08-01-2009, 03:48 PM   #8
Nialsh
Sporadic FK User
 
Nialsh's Avatar
 
Join Date: Dec 2003
Location: Houston
Posts: 317
Sounds like you might be using degrees instead of radians. If you're using a _rotation property to initialize your angle, it should look like this:
Code:
angle = char.arm._rotation * Math.PI/180;
__________________
undefined
Nialsh is offline   Reply With Quote
Old 08-10-2009, 09:25 PM   #9
adamvan101
Junior Member
 
Join Date: Jul 2009
Posts: 22
sorry i went on vacation - i hope you're still there.
my character rotation setting things are
Code:
char.onMouseMove = function(){
var __x = _xmouse-this._x;
var __y = _ymouse-this._y;
angle=Math.atan2(__y,__x)*180/Math.PI;
this._rotation = angle + 180;
}
not gonna lie - i have no idea what this is doing :/
ok i get it all... but i dont get the "angle=" line
adamvan101 is offline   Reply With Quote
Old 08-11-2009, 09:49 AM   #10
fjgamer
Member
 
Join Date: Nov 2003
Location: Tennessee
Posts: 43
Smile

Code:
char.onMouseMove = function(){
var __x = _xmouse-this._x;
var __y = _ymouse-this._y;
angle=Math.atan2(__y,__x)*180/Math.PI;
this._rotation = angle + 180; //what is the "+180" for?
}
I made a few adjustments:
Code:
char.onMouseMove = function(){
var rotX = _parent._xmouse-this._x;
var rotY = _parent._ymouse-this._y;
angle=Math.atan2(rotY,rotX)*180/Math.PI;
this._rotation = angle;
}
My AS2/AS1 is a little rusty. AS3 is better, so I use that:
Code:
function updateAngle() {
var rotX:Number = mouseX-char.x;
var rotY:Number = mouseY-char.y;
var angle:Number=Math.atan2(rotY,rotX)*180/Math.PI;
char.rotation = angle;
}
The code isn't really much different, though, and if I wanted to keep the "this" style code, I would only need to call the target of the event listener. In my code example, however, I didn't bother to make it use a listener. Closer to what you currently have, though, you could put the code inside the char MovieClip, like so:
Code:
function updateAngle() {
var rotX:Number = MovieClip(parent).mouseX-x;
var rotY:Number = MovieClip(parent).mouseY-y;
var angle:Number=Math.atan2(rotY,rotX)*180/Math.PI;
rotation = angle;
}
I never really do that, though, so my syntax might have some small mistakes. I hope I helped. Good luck with your games.
fjgamer is offline   Reply With Quote
Old 08-11-2009, 09:45 PM   #11
adamvan101
Junior Member
 
Join Date: Jul 2009
Posts: 22
i'm sorry... my imcompetance is probably frustating you. first, the "+180" is because the character's starting rotation is 180. so i need that to turn him around. second, i tried the first thing - my code that you changed - and it only worked if i removed "parent.". then i tried the code that you gave me to put in the character itself and that didn't work either.
adamvan101 is offline   Reply With Quote
Old 08-12-2009, 05:49 PM   #12
Nialsh
Sporadic FK User
 
Nialsh's Avatar
 
Join Date: Dec 2003
Location: Houston
Posts: 317
Quote:
Originally Posted by adamvan101 View Post
sorry i went on vacation - i hope you're still there.
my character rotation setting things are
Code:
char.onMouseMove = function(){
var __x = _xmouse-this._x;
var __y = _ymouse-this._y;
angle=Math.atan2(__y,__x)*180/Math.PI;
this._rotation = angle + 180;
}
not gonna lie - i have no idea what this is doing :/
ok i get it all... but i dont get the "angle=" line
This code already works right? What you need is the bullet initialization code.

I think you're getting confused because I was talking about a variable named 'angle' but there's already one in your code serving a slightly different purpose.

There are two ways to measure angles: degrees and radians. Radians are used by Flash's trig functions; degrees are used by Flash's _rotation property. The conversion factor between them is 2pi/360=pi/180, which is why that ratio keeps showing up.

Let's define 'angle' in radians. Now all your stuff should work.
Code:
char.onMouseMove = function(){
	var __x = _xmouse-this._x;
	var __y = _ymouse-this._y;
	angle=Math.atan2(__y,__x); //now in radians
	this._rotation = angle*180/Math.PI + 180; //convert to degrees, turn 180
}

onMouseDown = function() {
	b = attachMovie( ... ); //make bullet
	b._x = char._x+L*Math.cos(angle);
	b._y = char._y+L*Math.sin(angle);
	b._rotation = char._rotation; //might add +180 or +/-90
}
__________________
undefined
Nialsh is offline   Reply With Quote
Old 08-15-2009, 09:19 PM   #13
adamvan101
Junior Member
 
Join Date: Jul 2009
Posts: 22
Wow thanks a lot for all your help. you've really done a lot and i totally appreiciate it. thanks again. it works now
adamvan101
adamvan101 is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > General Help > Math and Physics

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 -4. The time now is 11:59 AM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.