I'm developing a game in j2me. I have a sprite and a rectangle moving around. how to check the collision between the sprite and rectangle.. ?
LCDUI Sprite class has the collidesWith methods. You should use one of them.
There is a good sample at this other answer: J2ME 2D - collisions detecting
Related
I'm trying to render some label textures with transparent background using OpenTK in Xamarin. At first the labels seemed display properly (see picture 1) but when the view rotated, the some label background are not transparent any more (see picture 2).
The enabled BlendFunc is GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha).
My question is how can I always have labels transparency on despite of their positions?
The same code and shader can run properly on Android Devices by the way.
Ah yes, the good old transparency problem. Unfortunately this is one that a graphics programmer has to solve on his own.
For just a few labels, the most straight foward solution is likely to sort your labels by z-depth and then render them from farthest to closest. You'd probably need to do some matrix math on that label position to adjust for viewport rotation.
For the 3d game I'm working on I have chosen to implement the order-independent transparency method called WBOIT by Morgan McGuire, which is fairly simple to implement and yields relatively good results.
I'm coming from Phaser + Tiled world, where, if I need some rectangle area in game world (like Player's area, spawning area, and so on) I can just draw rectangle in Tiled and then get it coordinates from Phaser.js and use as I need. And I seem to stuck to do similar things in Godot.
For some of tasks I can use Area2D with rectangle inside and collision events. But it is not always enough.
How can I just define rectangle on screen, and get its coordinates? For Sprite object and for Node2D I cannot get bounding rectangle. I can use Area2D + Rectangle and refer to rectangle's 'extent' property to get width/height, but that seems to be overhead for me - Area2D is used in collision detection.
What can I do in general? And what could be done in Godot for following scenarios?
Camera limits. I have Sprite with background gradient which I scale to needed world size, and I'd like to set camera limits on that Sprite's width/height.
Hero movement limits. Half of world is not accessible for player, so any move to x > MIDDLE shall be denied. I can just setup constant MIDDLE in the code, but I'd like to draw allowed area as rectangle and refer to it coordinates.
Spawn area. Mark some place of world (that could be just point, not rectangle) where new objects shall be created by code.
You could use the Rect2 class in a script to define custom rectangles. You can then use it to check whether it contains a Vector2 or another Rect2.
I am building a Star Fox like game. The player needs to control a ship in order to move trough gaps in the walls. Here are my problems:
I need to somehow detect collision with wall (if any)
How do I make the wall (Rect) slowly get bigger until it reaches a point?
Full code
If the solution can be done with classes, that would be great!
Here's the documentation for the pygame.Rect class: https://www.pygame.org/docs/ref/rect.html#pygame.Rect.inflate
To detect collision, the pygame.Rect class has methods to detect collision between Rects. There are a few there, so you could use collidelist() to check if the player's ship's Rect collides with any of the wall Rects.
The class also has two methods inflate() and inflate_ip which can be used to increase the size of any Rects.
I am making a game with Phaser using P2 physics and I want to make a "Hollow" sprite. What I mean is I want a sprite in the shape of a square/rectangle that the player can be inside, so it cant be a solid body. I would also like to make one with a hole in it so the player can move inside. Currently the ways that I can think of of making it are a custom polygon or multiple sprites, Are there simpler ways? and, If I make a custom polygon that has no hole can I avoid a line going through it at the end of the polygon?
I was wondering if someone could give me simple example of how to always draw sprites in pygame on --the top layer-- whilst an animation is being blitted on screen?
The senario I'm trying to solve is having an animated sprite (eg a man walking on the spot) and various other objects passing him, whilst he is still animated.
My solution so far has the animation layering on top of the "passing objects" which is not what I want.
Thanks in advance.
I think you've partially answered your own question here.
It's a matter of organizing what gets drawn first. Some side-scrolling 2D games use a "layer" solution, in which there is a background layer, a middleground layer, and a foreground layer, and the drawing system renders one layer after another.
I've also seen Pokémon top-down style games simply sort the sprites by their vertical position, so sprites "nearest" to the camera are drawn last, and thus on top of the other sprites.
See how the current implementation of Scene2D in libGDX gives each Actor a z-index property which can later be used to organize Actors into layers.
Just draw your sprites with a LayeredUpdates group:
class pygame.sprite.LayeredUpdates
LayeredUpdates is a sprite group that handles layers and draws like OrderedUpdates.
LayeredUpdates(*spites, **kwargs) -> LayeredUpdates
You can set the default layer through kwargs using ‘default_layer’ and an integer for the layer. The default layer is 0.
If the sprite you add has an attribute layer then that layer will be used. If the **kwarg contains ‘layer’ then the sprites passed will be added to that layer (overriding the sprite.layer attribute). If neither sprite has attribute layer nor **kwarg then the default layer is used to add the sprites.
and give your sprites the correct layer value.