-
Senior Member
[RESOLVED] [Math] position objects based on main object's position & rotation
I'm a little stumped by this.
If I'm positioning objects based on a main object's position and rotation, how do i calculate the X and Y of the "moon" objects?
PHP Code:
ancor_mc._rotation = 70;
box_mc._x = ancor_mc._x + 20;
box_mc._y = ancor_mc._y - 10;
anyone?
-
Funkalicious
A modified excerpt from my Turret class.
Code:
//Absolute position
public var X:Number;
public var Y:Number;
public var Rot:Number;
//Rel Position
protected var dX:Number;
protected var dY:Number;
protected var dMag:Number;
public var dRot:Number;
public var dRotation:Number = 0;//Relative turn compared to starting rotation
public function Turret(BATTLE:Battle,POS:Array, MAXTURN:Number, MAXROT:Number, STARTROT:Number,PAUZE:Number,POWER:Number,RANGE:Number,MOUSEPOS:Point,TEAM:int) {
dX = POS[0];
dY = POS[1];
dMag = Math.sqrt(dX*dX+dY*dY);
dRot = Math.atan2(dY, dX);
}
public function UpdatePos(ParentX:Number, ParentY:Number, ParentRotation:Number) {
var TotalRot:Number = ParentRotation+dRot;
X = ParentX + dMag* Math.cos(TotalRot);
Y = ParentY + dMag * Math.sin(TotalRot);
Rot = ParentRotation+startRot+dRotation;
}
It's basicly trig. If you know the angle and distance of a side, you can calculate the other sides by using sine and cosine. Since the length stays the same (dMag = delta Magnitude), you just add the rotation of the parent object to the angle to get the angle you need to calculate the other sides.
Last edited by TOdorus; 06-15-2009 at 11:50 AM.
-
Senior Member
<3 TOdorus
I must be doing something wrong, its behaving pretty weird. Here's my as2 code (create anchor_mc & moon_mc on stage to test):
PHP Code:
var dX:Number = 30;
var dY:Number = 30;
var dMag:Number = Math.sqrt(dX*dX+dY*dY);
var dRot:Number = Math.atan2(dY, dX);
anchor_mc._rotation = 0;
function updatePosition(_anchor:MovieClip, _moon:MovieClip)
{
var TotalRot:Number = _anchor._rotation + dRot;
_moon._x = _anchor._x + dMag * Math.cos(TotalRot);
_moon._y = _anchor._y + dMag * Math.sin(TotalRot);
_moon._rotation = TotalRot;
}
onEnterFrame = function(){
updatePosition(anchor_mc, moon_mc);
anchor_mc._rotation ++;
}
Why is it jumping? Can't wrap my head around trig
-
Funkalicious
:blush:
the Math class uses radians, so you should convert your _anchor._rotation to radians to get the right x and y positions
I don't know what exact rotation you want for your moon movieclip. If you want it to keep a relative rotation towards the anchor clip you need to define a start rotation.
Code:
moon._rotation = startRotation + anchor._rotation
You can always use Math.atan2(moon._x - anchor._x, moon._y -anchor._y) to get it facing... towards the anchor? I wouldn't know by head, but if you want it the other way around, just swap the values.
EDIT
but ofcourse you could always have a starting rotation that faces towards or away from it. Doh.
EDIT2
and if you want your moon to spin, just add it to the rotation
Code:
dRotation += turnSpeed
moon._rotation = startRotation + anchor._rotation + dRotation
Last edited by TOdorus; 06-15-2009 at 11:53 AM.
-
Senior Member
Thanks again dude.
After converting rotation to radians, I'm still having the same jumping issue.
PHP Code:
var dX:Number = 30;
var dY:Number = 30;
var dMag:Number = Math.sqrt(dX*dX+dY*dY);
var dRot:Number = Math.atan2(dY, dX);
anchor_mc._rotation = 0;
function updatePosition(_anchor:MovieClip, _moon:MovieClip)
{
var anchorRad:Number = Math.atan2((_anchor._y - _moon._y), (_anchor._x - _moon._x))
var TotalRot:Number = anchorRad + dRot;
_moon._x = _anchor._x + dMag * Math.cos(TotalRot);
_moon._y = _anchor._y + dMag * Math.sin(TotalRot);
_moon._rotation = TotalRot;
}
onEnterFrame = function(){
updatePosition(anchor_mc, moon_mc);
anchor_mc._rotation ++;
}
P.S. Which way the anchor rotates doesn't matter.
-
CostomJunky
Dude...
Code:
_moon._x = _anchor._x + dMag * Math.cos(_anchor._rotation*Math.PI/180);
_moon._y = _anchor._y + dMag * Math.sin(_anchor._rotation*Math.PI/180);
_moon._rotation = _anchor._rotation;
-
Senior Member
Thanks Xploder! And yes, i really am that dumb
Here's the working code if anyone cares:
PHP Code:
var dX:Number = 30;
var dY:Number = 30;
var dMag:Number = Math.sqrt(dX*dX+dY*dY);
var dRot:Number = Math.atan2(dY, dX);
function updatePosition(_anchor:MovieClip, _moon:MovieClip)
{
var rad:Number = _anchor._rotation*Math.PI/180
_moon._x = _anchor._x + dMag * Math.cos(rad);
_moon._y = _anchor._y + dMag * Math.sin(rad);
}
onEnterFrame = function(){
updatePosition(anchor_mc, moon_mc);
anchor_mc._rotation ++;
}
Again, thanks to both of you
-
Funkalicious
How you ever did create those AI tutorials is beyond me
-
Senior Member
I marked it resolved, but I shouldn't have done that. All Xploder's script does is rotate the moon around the anchor. That's not what I needed. Sorry if I wasn't being clear. Let me try again;
Let's say the anchor is at x100, y100, and the moon is at x125, y75. As the anchor starts rotating, how do I calculate the new x&y of of the moon?
-
Senior Member
Originally Posted by TOdorus
How you ever did create those AI tutorials is beyond me
well, i know that if you add A to B, you get C. But i have no idea about how A or B works
-
CostomJunky
Let me get this straight. So you want to set starting positions with AS, for both the "moon" and "earth" and the distance be determined by the initial positions? And then you want them to rotate about which axis? The center between the objects or just the earth?
Last edited by Xploder; 06-15-2009 at 02:08 PM.
-
Senior Member
Originally Posted by Xploder
Let me get this straight. So you want to set starting positions with AS, for both the "moon" and "earth" and the distance be determined by the initial positions? And then you want them to rotate about which axis? The center between the objects or just the earth?
You are absolutely correct. To answer your question; everything is rotating around earth's axis.
-
CostomJunky
How about this:
Code:
import flash.geom.Point
var EarthAxis:Point = new Point(100,100);
var MoonAxis:Point = new Point(125,175);
function setPosition(_object:MovieClip, pt:Point){
_object._x = pt.x;
_object._y = pt.y;
}
setPosition(anchor_mc, EarthAxis)
setPosition(moon_mc, MoonAxis)
var dMag = Point.distance(EarthAxis,MoonAxis);
function updatePosition(_anchor:MovieClip, _moon:MovieClip)
{
_moon._x = _anchor._x + dMag * Math.cos(_anchor._rotation*Math.PI/180);
_moon._y = _anchor._y + dMag * Math.sin(_anchor._rotation*Math.PI/180);
_moon._rotation = _anchor._rotation;
}
onEnterFrame = function(){
updatePosition(anchor_mc, moon_mc);
anchor_mc._rotation ++;
}
Last edited by Xploder; 06-15-2009 at 02:24 PM.
-
Senior Member
That allows me to set the initial x&y of everything, but once you start rotating anchor_mc, the moon loses its original position relative to the anchor. It's always on the right on the anchor...
-
Funkalicious
Don't make me make a drawing of this (you'll be going "oooooh" in understandment).
Code:
var dX:Number = 30;
var dY:Number = 30;
var dMag:Number = Math.sqrt(dX*dX+dY*dY);
var dRot:Number = Math.atan2(dY, dX); //in radians
function updatePosition(_anchor:MovieClip, _moon:MovieClip)
{
var rad:Number = _anchor._rotation*Math.PI/180 //now also in radians
var totalRad:Number = rad+dRot
_moon._x = _anchor._x + dMag * Math.cos(totalRad);
_moon._y = _anchor._y + dMag * Math.sin(totalRad);
}
onEnterFrame = function(){
updatePosition(anchor_mc, moon_mc);
anchor_mc._rotation ++;
}
-
Senior Member
ohhh! I didnt add dRot to rad before calculating cos/sin.
Thank you so much!
So, now that my arse is yours, what would you like to do with it?
-
Funkalicious
Just say "oooh"
-
Senior Member
That certainly teaches me how to fish
Thank you for taking your time to explain everything and thank you for your patience
-
ism
try this;
Code:
ancor_mc._rotation = 70;
var p = { x: 20, y: -10 };
ancor_mc.localToGlobal(p);
this.globalToLocal(p);
//
box_mc._x = p.x;
box_mc._y = p.y;
Graphics Attract, Motion Engages, Gameplay Addicts
XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|