Finding the coordinates of a rotated object - actionscript-3

I have 2 objects of interest for this problem.
Obj1 is the parent object, a circle sprite
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?

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.

Related

How do I get the global point of a rotated object?

I'm trying to get the global point (x,y) of an object. This object is a child of a rotated parent, MovieClip. Without rotation of the parent, it's easy to find the global point of the child, parent.localToGlobal. However, when I'm rotating the parent, it seems that localToGlobal returns the incorrect x,y.
localToGlobal deals with position not transformation. You are simply using the wrong tool for the job. I'm guessing you don't want to deal with matrices but you gonna have to.
mydisplayobject.transform.pixelBounds;//global space occupied by object
mydisplayobject.transform.concatenatedMatrix//the matrix representing all combined transofrmations

how to create a basic polygon?

I want to create a polygon that will collide with a rectangle using overlapse(). The only problem I have is that I don't know how to create a polygon, and I mean only a polygon. Every time I tried to find it on the internet I find the most complicated codes ever. So if can you just show me how to create a polygon and explain how to declare it's shape it will be greatly appreciated.
The polygon looks about like this:
note: it should be symmetrical but i am not sure if i drew it symmetrical...
According to the documentation on the polygon class, you can simply use it's constructor to define a new polygon. Example:
//we want to create a simple square shaped polygon
//we create an array to hold every vertex we need, there are 4 vertices
//this constructor uses 1 float array as parameter, where every two element define the position of 1 vertex
polygon = new Polygon(new float[]{ //so we pass a new float array to the constructor
0,0, //this is the x,y of the first vertex
width,0, //the second vertex
width,height, //the third
0,height}); //and the last
This is a simple example, but you should be able to figure out how to expand the shape. I'll give one more example to add an extra vertex to create a "house shape":
polygon = new Polygon(new float[]{ //so we pass a new float array to the constructor
0,0, //this is the x,y of the first vertex
width,0, //the second vertex
width,height, //the third
(width*0.5),(height*1.5), //this one is now extra, it adds one vertex between the last and the one before it
0,height}); //and the last
Let me know if you need more information.

Libgdx rotating ellipse for collision detection

I try to use 2 ellipses to detect a collision if they overlap. I have to rotate the ellipses but I can't figure out how this works. I'm working with the "com.badlogic.gdx.math.Ellipse" class but it seems to have no method for rotating. Any ideas? Thx in advance!!
Unfortunately, LibGDX doesn't have in-built rotating functions for ellipses.
Instead, I'd either be resorting to a circle in which rotation does not matter, or use polygons to check intersection.
Polygons are formed through an array of float values (vertices), where, every even element of the array is the horizontal component (x) and the odd, the vertical component (y).
Polygon polygon1 = new Polygon(vertexSet1);
Polygon polygon2 = new Polygon(vertexSet2);
Then, by using an Intersector, you can then check whether these polygons have intersected. The more vertices, the more accurate your shape will be. Just remember to have 6 or more elements in your vertex array, as the 6 floats will give 3 (x, y) points which is the minimum required for a polygon.
if (intersector.overlapConvexPolygons(polygon1, polygon2) {
//do your intersection code
}
The polygons themselves have commands to translate, scale and rotate, allowing for the rotations you mentioned above.

rotation angle and spawn point of laser beam in flash game

I am trying to figure out the rotation angle and generation point of a laser beam that shoots up from a Cannon on Mouse Click. I am using ActionScript 3 and Flash for the same.
I rotate the beam based on my mouse cursor position which I feel works just fine. The
The issue is the generation point of my laser beam and it goes out of order. I want it to snap to the cannon ie its rotation point has to be the cannon. How do I do this in Flash?
Please have a look at the image file to get so that I am more clear.
Here is the code snippet that does the rotation and position logic in actionscript
laserBeamRight = new RightLaserBeam();
stage.addChild(laserBeamRight);
laserBeamRight.x = 812.65;
laserBeamRight.y = 400.1;
var angle2:Number = Math.atan2(stage.mouseY - laserBeamRight.y, stage.mouseX - laserBeamRight.x);
laserBeamRight.rotation = 180 * angle2/Math.PI;
I have hardcoded values for the position. They represent the right cannons position in the stage.
Here is the image file that shows the problem.
So I want the beam is targeting the mouse crosshair which is fine but I want it to be fixed and rotated around the cannon.
Another image with two beams at different angles. X position is right but Y position looks out of place because of the angle
One last image which clearly shows my problem.
The X position is right so is the Y position but it is originating from the center point of the beam and not the end point or the tail of the beam. I want the tail of the beam to be snapped to the cannons position. I tried changing the pivot point of the beam movie clip inside flash to the tail but that did not help.
Any idea?
First of all, adjust laser MC so that its anchor point is at its beginning instead of the middle, then align it to the turret's center as you do already. Then, when you are about to hit an object at specific (x,y), calculate rotation using Math.atan2() for the translated coordinates of mouse cursor (employ turret.globalToLocal(new Point(event.stageX,event.stageY)), then you should scale the laser so its end will hit the cursor position, change its scaleX. Note that you should do all transitions in one coordinate space (turret's), you apparently turn the turret's cannon part already, so you can do the same for your laser, and add it as part of a turret, probably positioning it behind the cannon part. An example:
// laser MC is like this: *>>>>>>>-------
// * is anchr point, laser is aligned righwards
const laserlength:Number=500; // the length of the laser, the X of the point you want to hit the cursor
....
function fireLaser(e:MouseEvent):void {
// creates a laser
var laser:Laser=new Laser();
laser.x=turret.centerX; // where to position the laser beginning
laser.y=turret.centerY; // relative to the turret
turret.addChildAt(laser,0); // add as a part of turret, behind everything
var cursor:Point=turret.globalToLocal(new Point(e.stageX,e.stageY));
laser.rotation=Math.atan2(cursor.y-laser.y,cursor.x-laser.x)*180/Math.PI;
// rad-to-deg conversion
laser.scaleX=Point.distance(cursor,new Point(turret.centerX,turret.centerY))/laserlength;
// lasers.push(laser); // expecting you to do this so laser should fade with time
}

How to track a point on rotating MovieClip?

I have a MovieClip, that is representing a character in my game. Id like to "create bullets" shooting out from the tip of my characters gun. Problem is that when my character turns around, also the point rotates around the MovieClips pivot.
Is it possible to anyhow easily track this point, so that I could dynamically create new objects at the same location.
I tried to add a new MC as a child to my character, with the initial position at the guntip. In some systems child-objects "follow" their parents around, but it didnt seem to work here.
Is there any other "native" way of doing this, or do I just have to have a Polar-coordinates representation of the point relative to character-MovieClips origin, and add the MC rotation to theta, so that I can calculate the X and Y coordinates?
Try localToGlobal() and globalToLocal() methods to transform coordinates from your character movieclip to its parent.
Set up the movie clip with the gun (I'm assuming it's at the end of an arm?) so that the gun tip is straight across from the pivot point.
Then pass the method that fires the bullet three parameters: the x and y position of the gun MC, and its current angle.
The code for your bullets initial position might look something like this:
public function CreateBullet(x,y:Number, degree:Number)
{
// set start position
this.x = x + ARMLENGTH*Math.cos((degree/180)*Math.PI);
this.y = y + ARMLENGTH*Math.sin((degree/180)*Math.PI);
}
Where ARMLENGTH is the distance from the pivot point to the end of the gun.
Two caveats, Flash can do weird things with angles, so you might have to make an if statement in CreateBullet() with inverted degrees if the player if facing backwards. Also, if you have the gun MC as a child of your character, you might have to make a Point where the pivot point is then do a localToGlobal on it. There's a good reference for that here.