How can I make a mask for a rect? - pygame

Can I make a mask of a rect, if so, how? I'm trying to make collisions between a mask and a rect, and since I can't make a rect of a mask(No pixel perfect collision then) I figured I'd ask if I could make a mask of a rect? Like given the dimensions and x/y positon of a rect, how could I make a mask based on that?

Create a pygame.mask.Mask object and set all bits 1 with fill()
rect_mask = pygame.mask.Mask((rect.width, rect.height)))
rect_mask.fill()
Use pygame.mask.Mask.overlap to for the collision detection between Mask objects. The offset parameter of the method overlap() is the relative position of the othermask in relation to the pygame.mask.Mask object.
e.g.:
object 1: x1, y1, mask1
object 2: x2, y2, mask2
offset = (x2 - x1, y2 - y1)
if mask1.overlap(mask2, offset):
print("hit")
See also PyGame collision with masks and Pygame mask collision.

Related

Libgdx - How to move object forward by direction?

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.

How to convert flash circle object coordinates for canvas/raphael.js?

I have some simple flash animations that I am converting into HTML5 image maps, that trigger some drawing upon <area> rollovers.
Problem being is that all the circles (representing roll-over points) are given to me as flash circle object coordinates. The points in question are formed at the joining of imaginary lines that go from the circle’s topmost and leftmost points (marked by the dashed red lines in the image below).
But to draw my circles in HTML5 (using raphael.js currently), I have to to give a center coordinate: var c = paper.circle(x, y, radius).
So, for example, if I have flash circle object (532.20,30.35) with a height and width of 19.80 (again from the point where the imaginary lines intersect) how can I calculate the values I need for drawing a circle at its center point in the canvas?
You just subtract the radius from the x and y:
x = x - radius;
y = y - radius;
In your case:
radius = 19.8 * 0.5
x = 532.2 - radius;
y = 30.35 - radius;
This will draw the circle center at the original x and y (it moves the circle left and up).
If you want the circle in the effectively same position as the original but have to move the coordinate system then you add radius instead.

Is there a way in pygame to detect if a rect is fully inside a mask?

Is there a way in pygame to detect if a rect is fully inside a mask?
I know that there is rect in rect collision and collidepoint (point in rect collision) but is there any way to detect if a rect is inside a mask?
Thanks
I don't believe there is any function supplied by pygame to do that. It would definitely be somewhat challenging, because of the "shapeless" possibilities of a mask. One possibility which might work for you would be to first get a list of the bounding rects of the mask (a list of rects which together "contain" all of the points of the mask) and check if your rect is inside any of these rects:
bounding_rects = mask.get_bounding_rects()
for b_rect in bounding_rects:
if b_rect.contains(test_rect):
print "Rect is inside mask"
Of course with this solution, if the rect is inside the mask, but not inside any one particular bounding rect, the test will fail. Here's an idea of how you might handle that, with the tradeoff of a little bit of lost precision:
bounding_rects = mask.get_bounding_rects()
mask_rect = bounding_rects[0].unionall(bounding_rects)
if mask_rect.contains(test_rect):
print "Rect is inside mask"
This solution unions all of the bounding rects of the mask into one rect, which has the possibility of covering area that none of the bounding rects covered (if two rects have a gap of 10 pixels between each other and are unioned together, the new rect will contain that gap)

AS3: angle of reflection

I am stumped on working out the angle of reflection in my AS3 project, I have the formula but the formula relies on you knowing the angle of the object that is reflected against (to work out the perpendicular angle) the trouble is the objects are all at 0 rotation but are shaped differently (e.g the four sides of the stage have a line across them which is at 0 rotation but some are horizontal and some vertical).
How would I work out the angle of reflection purely from the angle of the object that is being reflected, taking into account AS3 returns rotation values between -180 and 180.
Anyone got an idea?
This doesn't answer my question so much as it only works on objects reflecting of horizontal or vertical objects but this function will get you the angle of reflection based upon the rotation of the object which is being reflected...
var reflection:int = (incidence > 0) ? 180 - incidence: -180 - incidence;
where incidence which is the rotation of the object which is being reflected.

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).