How do I create collision detection with an object and part of a background? - libgdx

I am just starting to attempt to make my own game using java and libgdx, and the extent of my success so far has been showing the background image on screen, and spawning a little square the user controls with WASD. The background is just a solid color, with a vertical rectangle that is red inside and has black edges. I want to make it so the tiny square (player) can move anywhere within the red rectangle, but not be able to cross over the black edges (out of the rectangle). How would I go about doing this?
Since I am a complete beginner to this stuff I must ask these related questions...
Should the player be just a texture? Should the background be a texture? or a sprite?
I'm not sure the difference between the two yet.

I recommand you to read tutorials about libGDX and Box2D like this one : http://www.gamefromscratch.com/post/2014/08/27/LibGDX-Tutorial-13-Physics-with-Box2D-Part-1-A-Basic-Physics-Simulations.aspx
to answer you're questions, in short :
a texture is an image in memory
a sprite is a part of a texture (or the whole texture) transformed (translation, scale, rotation) to be drawn on screen.
so basically, in the view model, your player is represented by a sprite, your background is a sprite as well.
Player 1 and 2 are 2 different sprites but may reffer to the same texture (bitmap).
with box2D, in the physics model, your player will be represented as a dynamic body and your background as a static body.

Related

Pygame collision detection, transparent border

For my pygame project, I must detect collision betweeen various .png image that move on the screen.
I have put the image inside sprite, and the sprite inside group, then I use this :
pygame.sprite.spritecollide(perso, zombie_group, False)
However, sometime, my image don't touch, but pygame detect a collision...
This is due to the fact that my images are png with transparent borders.
The transparent border collide, and pygame detect this :(
Any idea to stop the transparent border from colliding ?
Ok the sprite will take the image he can't detect if it was on a trasnparent BG or a color bg, he basically is just seeing a rectangle right now.
If you are using irregular shapes and a rectangle approximation is not enough I would recommend using collide_mask also check masks it is probably what you want
update
Regarding performance from the tutorial :
There are other ways to do this, with ANDing sprite masks and so on,
but any way you do it in pygame, it’s probably going to be too slow.
For most games, it’s probably better just to do ‘sub-rect collision’ –
create a rect for each sprite that’s a little smaller than the actual
image, and use that for collisions instead. It will be much faster,
and in most cases the player won’t notice the inprecision.

How to make the sprite.BoundingRectangle fit the sprite exactly

In LibGDX, I'm trying to create a game where you play a character who must survive waves of zombies. The plan is, when a zombie touches the player, the player loses health, and the zombie despawns; this works well until any of the sprites are rotated. Here's a screenshot of what I can imagine to be why:
To check the collisions, I use:
if (sprite.getBoundingRectangle().overlaps(Game.pl1.sprite.getBoundingRectangle())) {}
.. this is inside the Zombie class, and Game.pl1.sprite refers to the sprite belonging to the player.
So the red box is where I'm assuming the bounding rectangle is, based on where zombies despawn around it.
This is what I want the bounding rectangle to be:
I'm not sure if I can achieve this just using sprite.getBoundingRectangle so I was thinking perhaps I would need to use Box2D or maybe convert the sprite to a texture.
You could get the dimensions for a rectangle the size of the sprite if it wasn't rotated, and then rotate that.

How to use bitmapdata.draw to get a specific area of the stage?

I'm using a bitmapdata.draw(stage) to draw the stage as a bitmap but I would like to draw an area which is 32 pixels left of the stage instead, so that the area I am drawing as a bitmap is no longer in the viewing window, but the bitmap being draw is (so the stage is a bitmap). How do I draw a specific area with multiple bitmaps and display objects?
This is a very nice article that can help you: http://www.tomauger.com/2013/flash-actionscript/actionscript-3-bitmapdata-draw-offset-and-positioning
You should basically use a Matrix and work your way around with it :)
My honest advise would be not to use such odd techniques as if you want to draw something it should be within the stage..

as3 how to ignore MOUSE_DOWN outside mask

I am working on a jigsaw-like game in as3 where irregularly shaped layers imported from photoshop are used to mask parts of their original background.
By setting cacheAsBitmap=true on the mask and its contents the result is a nice irregular shape with its transparent bounding portions left out.
However the invisible bounding areas are still detected at MOUSE_DOWN. I would prefer the mouse not be detected anywhere but on the visible masked part. At the moment I cannot detect the mouse on any other clips on the stage that might appear behind the overlapping transparent areas.
I have seen a solution here involving bitmap pixel detection which I have not found a way to apply as a solution. The contents of my masked areas are either shapes or MovieClips.
I hope someone can help me find a solution
The simplest and the most stable approach to prevent the mouse events on the transparent area of bitmap graphics is to create a separate vector shape as the target for mouse and set the mouseEnabled flag to false to the bitmap or set hitArea property to this shape.
You can create such shape manually in the Flash IDE for the tests and even for the production. Sometimes it's more suitable to write the bitmap tracert script that creates contour shape in runtime by checking the pixel transparency.

'Runner' Game, loop sliding background, remove children when outside

I've just started using actionscript 3 and I am currently trying to make a scrolling runner game in Flash CS5, similar to the Flood Runner games from Tremor games. The difference is, however, that my game is not an endless runner game and the character has a destination that she must reach before time runs out. Alot of the tutorials I've read on the subject use the player character's x and y positions to scroll, but in my game the background scrolls independent of the character. The tutorials I've read about this do not address my problems specifically.
TL;DR: I do not want to loop my background but have a series of multiple background images.
I am trying to figure out the best way to patch together multiple background images seamlessly. Currently, I have one background movie clip object at the maximum pixel width. The background object scrolls to the left independently of the position of the player character, who can only jump.
What I am thinking about doing is this:
Every time a point at the far right edge of one background image reaches the far right stage boundary, I have my actionscript call an addChild command for the next background object and instantiate it at the far right stage boundary. It will scroll at the same speed as the preceding background object.
I also need to figure out how to remove the background objects once they have completely exited the stage for memory purposes.
So, what would be the best way to tackle this?
Your basic concept will work, and to remove you just need to evaluate when the background image is off the screen :
if (backgroundImage.x < -backgroundImage.width)
{
// image is no longer on the screen.
removeChild(backgroundImage);
}