how to create a basic polygon? - libgdx

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.

Related

Finding the coordinates of a rotated object

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.

Flash Games: Make a world continuous, circular

I am writing now a flash game and I run into a an issue. I have a map for the game which is defined as a 2-D array, where each element represents a component of the map. The player is always in the center of the map.
The problem is when the player reaches one end of the map. Now it is empty space. I want that the player instead of seeing the empty space, to see another end of the map and in this way, the map will loo like it goes around.
So for example if the player goes to right he will eventually start seeing the the left side of the map and the world will look continuous.
Does anyone knows how to implement this functionality?
You could make the array 2 times and put the first one behind the second one again and than the second one behind the first etc etc..
It's done here with 2 pictures, just use the arrays instead:
//The speed of the scroll movement.
var scrollSpeed:uint = 2;
//This adds two instances of the movie clip onto the stage.
var s1:ScrollBg = new ScrollBg();
var s2:ScrollBg = new ScrollBg();
addChild(s1);
addChild(s2);
//This positions the second movieclip next to the first one.
s1.x = 0;
s2.x = s1.width;
//Adds an event listener to the stage.
stage.addEventListener(Event.ENTER_FRAME, moveScroll);
//This function moves both the images to left. If the first and second
//images goes pass the left stage boundary then it gets moved to
//the other side of the stage.
function moveScroll(e:Event):void{
s1.x -= scrollSpeed;
s2.x -= scrollSpeed;
if(s1.x < -s1.width){
s1.x = s1.width;
}else if(s2.x < -s2.width){
s2.x = s2.width;
}
}
You simply check if your player is about to get off the "right" or "left" edge of the map, and position him at the other edge. To draw a circular map, you can use the following technique: if you are about to draw a column of a number that exceeds the map's width, decrease that number by width and draw the column at resultant index; and if you are about to draw a column at index below zero, add width and draw the column at resultant index. If you are in troubles of making a hitcheck at continuous map's borders, you can employ the same trick to find neighbors. (The "circular array" is a pretty basic algorithmic problem, and is resolved in many ways already)
You have a few options here. You can do the pac-man style of just making your character pop up on the other side of the screen, but that would require you to abandon the cool bit of the character being in the middle at all times.
On to the real suggestions:
If you're not implementing your array as one solid object (i.e. making it draw individual collumns/rows at a time) then this is a no-brainer. Just have a function that returns the index of the next collumn/row, within certain bounds. Like, if your array is 40 elements wide, when it tries to draw element 41, subtract the size of the array, and make it draw element 1 instead.
If your array is one solid object (like if you drew it onto a stage object and are just manipulating that) and it's not very big, you could probably get away with drawing a total of four of them, and just having a new one cover up any whitespace that's about to appear. Like, as you approach the right edge of the first array, the second array moves to the right of it for a lawless transition.
If your array is a solid object and is very big, perhaps you could make eight buffer objects (one per edge and one per corner) that hold approximately half a screen's worth of the array. That way as you approach the right edge, you see the left edge, but then when you cross into the buffer zone, you could teleport the player to the corresponding position on the left of the array, which has the buffer for the right size. To the player, nothing has changed, but now they're on the other side of the world.

Creating a square pie mask in AS3

so I want to do something similar to this example:
http://www.flashandmath.com/howtos/circlefill/
Except, I want to do it with a square instead of a circle. Is there any way to do this with AS3? Google searches provide plenty about creating a circle mask, but next to nothing regarding the pointy equivalent!
You can do this pretty easily by using the example linked above. Use the circle you create with the example above, and create a square mask for it.
//this is your pie from the example linked above
//it could be a MovieClip or a Sprite, or possibly another type
//but we'll just call it a DisplayObject
var pieCircle:DisplayObject;
addChild(pieCircle);
//now create a square
var squareMask:Sprite = new Sprite();
//start drawing
squareMask.graphics.beginFill(0xff0000);
squareMask.graphics.drawRect(0, 0, pieCircle.width/2, pieCircle.height/2);
squareMask.graphics.endFill();
addChild(squareMask);
//now set the square as a mask of the pieCircle
pieCircle.mask = squareMask;

Flash drawing line overlapping check

I have an application where user have to draw a line on the canvas without overlapping it. Is there a way to test the overlapping? I have googled already but found result with circles and rectangle overlapping. My case is different. Here user will draw lines on canvas without overlap the line itself. May be I am missing something so any guidance is appreciated. Thanks
I take it you mean the user draws a line with some sort of pen tool, using the mouse.
Here's what I would do:
First, hold the path of the line drawn in a BitmapData object.
var lineBitmapData:BitmapData = new BitmapData(display.width,display.height,true,0x00000000);
This creates a transparent bitmap object with the user's line on it.
On each frame (or timer event, if you use timer) do the following:
1.capture the current mouse position and put it into a Point object.
var currentMousePosition:Point = new Point(mouse.x,mouse.y);
you will also need a point representing the upper-left corner of your bitmapData.
var pt1:Point = new Point(1,1);
2.perform collision detection between the current mouse position and the lineBitmapData
var result:Boolean = lineBitmapData.hitTest(pt1, 0xFF, currentMousePosition);
the second parameter in the hitTest method is the threshhold value. Basically, this needs to be set to the minimum alpha value that you want to count as a hit.
3.check the result of the hitTest. If it's false, this means what the user is about to draw this frame does not intersect what was already drawn. In this case, you add the bit that was drawn during the last frame to the lineBitmapData.
If the hitTest returns true, however, that means the user is about to make his line intersect, so your program needs to stop the drawing (or whatever behavior you want).
if(result){
myPenTool.stopDrawing();}else{
var drawnLastFrame:BitmapData = myPenTool.drawSingleFrameLine();
lineBitmapData.draw(drawnLastFrame);}
4.Update what the user sees on the screen with the new lineBitmapData

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);
}