I am developing my first game in LibGdx using scene2D.How can I render shapes for
checking collision of different shaped entities?Currently I am rendering Rectangle and Circle shapes.But it does not looks proper for my requirements.
Which is the best way to render shapes for checking collision?
If you're using scene2d you don't necessarily need to render some shapes using ShapeRenderer. scene2d provides you with a debug mode which will e.g. draw the set bounding boxes around an actor. This happens in drawDebug()
Of course, you can override this method to add additional shapes (will use a ShapeRenderer inside). For example if you have an actor representing a ball, you might want to draw a circle around the border of the actor to see the clickable area in debug mode. Of course, you should then also override the collides() method accordingly to have correct collision detection.
If you plan to do collision detection between different actors in your game, consider using box2D or have a look at the Intersector class.
Related
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.
In my game I have some gold that the player can collect. My problem is I want to individually refernce textures of gold so i can take that particular instance of gold off the screen (player picked it up). How would I go about doing this as i am pretty sure you cannot do this with regular textures. Would i have to create a shape and fill it with the gold texture so I can delete that particualr gold piece? Thanks
I think you confuse Texture (which is basically a loaded image that you can draw) with game entities. Depending on how you implement your game, you can spawn multiple bodies (Box2D), actors (Scene2D) or your simple data containers (width, height, x, y) representing each coin and draw each one on the screen using the same texture. Actually, that's the preferred way to handle assets: you load images once and then simply reuse them for each sprite.
I suggest looking into the following classes:
Sprite (makes it easier to draw textures).
Image (allows to display simple images on Scene2D stage).
Box2DSprite (third party utility class that makes it easier to draw Box2D worlds).
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.
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.
what would be your recommendation for drawing shapes (rects, circles...) onto BitmapData, and how to effectively switch between colors.
Is there any way to get graphics context from BitmapData so I could easily paint shapes using graphics.draw...()?
Why do you want to use a BitmapData? Not sure what you're after, but after reading a couple of your questions it seems you're a kind of fighting against how flash works. I think you'll make things much easier for yourself if you use what's available already. BitmapData objects are meant mainly to manipulate pixels and don't expose methods for drawing shapes. A Graphics object (available through Sprite, Shape, etc) on the other hand, allows you to draw vector shapes easily.
So, the best approach for this, I guess, would be using the drawing API to do what you want and then, if needed, convert the graphic to a BitmapData.