Libgdx - How to move object forward by direction? - libgdx

Example: I have 3 object with 3 rotation so how can i move object forward by object's direction like that.
http://i.stack.imgur.com/pTWDf.png

If you are using sprites to hold to values of each object, set the x/y += direction you want the objects to travel. Otherwise, if you are drawing the image through a spritebatch you can use batch.draw(texture, x, y) and set the x and y coordinates that way.

Related

Java get point location from angle change

This may be an issue that I simply do no know the proper terminology to research the answer to this, I am pretty sure the solution is a function of trig.
I have a method which accepts an X/Y position coordinate and an angle in degrees. It should return an updated X/Y based on the rotation angle provided.
For example, A point is usually located at x=0,y=2 (top middle). Now I need to rotate it to it's side by 90 degrees. In my mind I know it's location is now x=2,y=0 (middle right) but I do not know the equation to produce this.
I think I need to first determine the quadrant of the starting point, and then perform the proper trig function from there. This is for a game I am developing using libgdx which is where the Vector2 object comes from.
I have come this far:
public Vector2 getPointsRotated(Vector2 startPoint, float angle){
Vector2 newPoint = new Vector2(0,0);
// determine the starting quadrant
int quad=0;
if((startPoint.x>=0)&&(startPoint.y>=0)){quad=0;}
if((startPoint.x<0)&&(startPoint.y>=0)){quad=1;}
if((startPoint.x<0)&&(startPoint.y<0)){quad=2;}
if((startPoint.x>=0)&&(startPoint.y<0)){quad=3;}
if(quad==0){
// doesn't work
newPoint.x = (float) ((newPoint.x)* (Math.sin(angle)));
newPoint.y = (float) ((newPoint.y)* (Math.cos(angle)));
}
// ...
// other quadrants also don't work
// ...
return newPoint;
}
Thanks for any help.
Update:
I have been avoiding and returning to this problem for a few days. Now after finally posting the question here I figure it out within minutes (for ppl using libgdx anyway).
Libgdx provides a rotate function for Vector2s
so something like:
Vector2 position = new Vector2(0,2);
position.rotate(angle);
works perfectly.
I find rotation matrices are very helpful for this sort of problem.
https://gamedev.stackexchange.com/questions/54299/tetris-rotations-using-linear-algebra-rotation-matrices

How to move an object with actionscript from with in itself?

I want to be able to change the x value of an object from with in its own timeline.
The object has been created with actionscript so it does not have an instance name.
So I'm just wondering if there is a command like self or something so I can move an object.
Eg
self.x = 0;
You can do it with
this.x = 0;
or just
x = 0;
But if your display objects tree is deeply nested, these values will be relative to the object they've been added to. So if you need relative coordinates - just move it with this.x or x, and if you need global ones - use localToGlobal method

An objects coordinates relating to a frame on a timeline

To anybody who may care to help:
I am looking to create an animation (perhaps frame by frame) that corresponds with the coordinates of an object. Specifically, I want to have a draggable object's coordinates (locked to the x-axis) indicate where the playhead of a specific movie clip should be.
In other words, let's say that I have a 100px wide stage and I want each px location of an object on that stage to correspond to a particular frame of a movieclip.
In concept, I feel that it should be as easy as loading an objects coordinates into a variable, then passing that variable on with a simple math equation, adjusting it for movieclip length... but right about then my brain gets fried.
Finding out how to lock a draggable object to the x-axis has been pretty easy, but from there I'm stumped. I'm not particularly well versed in AS3 but I do like to think I understand the concepts.
Thank you in advance.
Try the following:
import flash.events.Event;
//the min (left-most) coord your draggable mc can be dragged
var minX:int=0;
//the max (right-most) coord your draggable mc can be dragged
var maxX:int=100;
var frameTo:uint;
//enterframe listener to check drag_mc x position continuously
addEventListener(Event.ENTER_FRAME, enterframe_handler);
function enterframe_handler(e:Event):void
{
//drag_mc is your draggable movieclip, anim_mc is the animation
//drag_mc.x should always be between minX and maxX: (minX <= drag_mc.x <= maxX)
//(drag_mc.x/(maxX - minX) gives us the "percentage" (from 0 to 1)
//multiply by the animation's total frames lenght,
// and add 1 (because frame numbers begin at 1)
frameTo = 1 + Math.floor((drag_mc.x/(maxX - minX))* anim_mc.totalFrames);
//set animation to target frame!
anim_mc.gotoAndStop(frameTo);
}

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.

Where does the .x and .y property of a Sprite in actionscript 3.0 measured from? from the centre of the object? or.....?

Where does the .x and .y property of a movieclip in actionscript 3.0 measured from? from the centre of the object? or.....?
For instance, given a pro grammatically drawn Sprite:
graphics.beginFill(0x000000);
graphics.moveTo(9.00000000,-7.00000000);
graphics.lineTo(13.00000000,0.00000000);
graphics.lineTo(9.00000000,6.00000000);
graphics.lineTo(-11.00000000,6.00000000);
graphics.lineTo(-14.00000000,0.00000000);
graphics.lineTo(-11.00000000,-7.00000000);
graphics.lineTo(9.00000000,-7.00000000);
graphics.endFill();
Where will sprite.x and sprite.y measure from?
The top left hand corner? Or center of the sprite? or...?
Please enlighten me, thank you guys!
Best Regards.
The origin is always the top left corner of the object. x grows positively towards right and negatively to left; y grows positively towards the bottom and negatively towards top.
0,0 ---- 5,0
| |
| |
| |
0,5 ---- 5,5
Thus the origin of stage/root object is top left corner of the SWF because its coordinates are 0,0. If you add a display object to the root object and set its x and y to 5, (mc.x = 5; mc.y = 5;), and draw a line on its local coordinates from 0,0 to 15,15 that line would be drawn from 5,5 to 20,20 on the global coordinates.
Check out localToGlobal and globalToLocal methods of the DisplayObject class.
I think your confusion comes from the layered nature of the coordinate systems in Flash. When you draw your Sprite, the x and y values you pass to the graphics methods (e.g. lineTo) are measured relative to the coordinate system of the sprites. Moving the sprite's .x and .y will move everything in the sprite's graphics. So, if the sprite was initially at (0,0) and ran the above code, much of the drawing is off the screen (because it draws to negative x and y values. If, after the above code was run, you moved the sprite to (14,7), all the lines would be visible (just barely).