Search code examples
actionscript-3flashair

Finding the coordinates of a rotated object


I have 2 objects of interest for this problem.

  1. Obj1 is the parent object, a circle sprite
  2. Obj2 is a nested child object, a square sprite

Obj1 has a child called objHolder, inside objHolder is Obj2. objHolder has its pivot point set to the middle of Obj1, and Obj2 is placed near the circumference of Obj1. The objective is to rotate objHolder so that Obj2 looks like it's hovering around the circumference of Obj1.

Every frame, objHolder would have a new rotation value base on some other input values from my interface.

My question is, how do I obtain the x,y coordinates of Obj2 (relative to Obj1, not the stage) every frame?

If I use localToGlobal() it would not take into account the rotation value. Is there an easier way?


Solution

  • Well, it should take rotation into account. You describe that you have a nested structure like this: Obj1 <- objHolder <- Obj2. Then, objHolder is located at center of visible Obj1, and Obj2 is offset from the center of objHolder. Now, if you give objHolder some rotation, you should see Obj2 rotate and travel a circle. Does it do this? If no, then your display list is not how you describe. If Obj2 does rotate but doesn't travel, then you have Obj2's pivot point at center of objHolder, move it away.

    Anyway, the answer is to use both translations, first from source to stage, then from stage to target. If you want coordinates of one object in the system of another object, do this:

    p=target.globalToLocal(source.localToGlobal(new Point()));
    

    In your case, source is Obj2 and target is Obj1. And, new Point() is a point with coordinates (0,0) in the source object's coordinate system, aka the pivot point of source.