How to make the sprite.BoundingRectangle fit the sprite exactly - libgdx

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.

Related

Changing the animation and velocity in water

I'm trying to make a 2D game in Java, and I'm using Libgdx. But, I don't know how to change the animation and velocity in water, for instance, changing the movement from walking to swimming.
Can anyone help me with the code and methods please?
Assuming that you use rectangular areas for your water and your sprites, you might want to look at checking if a rectangle (your water) contains another rectangle (your sprite). If you use non-rectangle shapes, you should find the appropriate methods to check if the hitbox of your sprite is inside your water.
If your water contains your sprite, you can handle (update, render, etc.) it differently.

AS3 Can you make a sidescroller camera?

I've seen in flash sidescrollers they mostly use sidescroller by making the background move.
But if I had a background and I wanted the player to move and the camera to follow, how would I do it?
There are several ways to accomplish this. The first is to put all of the background objects into a container MovieClip and move that instead of the player. This also makes other aspects of having a camera, such as zoom and rotation, relatively simple, since you can just scale and rotate the container MovieClip.
Another method is putting all background objects into an array. Every time there is movement, you loop through the array and move the objects. This requires more math and separate x/ y variables for each object, so it's not as convenient.
One thing to note is that if you're going to rotate the camera, you might experience vibration issues as your camera gets further away from the origin. To prevent this, you'll have to shift all of the objects in your container MovieClip closer to the origin when they get too far away.
If you want the camera to ease towards the player's position, you just move 1/10 (or some other fraction) of the distance to the camera's target position each frame.

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

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.

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

Creating a stage bigger then the screen view

I'm trying to create a stage which has an image of the background, called lvl1.jpg. This image is 1920x6000 pixels. I want to start the game by creating the stage and adding the player on top. I'm placing a few stationary enemies on the level which the player should avoid.
I want to achieve this by letting the stage (or background image) and the enemies moving up a few pixels with each update. The size of the game file screen is 1000 by 600 pixels. So my gameclass is larger then my screen size will be. Is this possible? If so, how can I achieve this?
Yes, it is possible, but you should not directly use such a big bitmap anywhere, it's killing performance. And yes, it is possible for the game class to be bigger than stage size, look at Epic War 2 game for an example - it has a big battle scene that scrolls left-right on demand of the player.
In order for you to make such a game class, you should limit the actual screen presence to only the visible parts of your level. For this, research blitting techniques, which have a core concept of one visible Bitmap object with the size of a stage, whose BitmapData is redrawn each frame to display background, objects, player, monsters, etc.
As a first approach, you can have your Game class contain all the objects, including player, and change game.y so that the player is always within the stage boundaries.