Andengine + Box2d. Remove sprite, when it isn't visible for user - android

I write some wallpaper. It represents the many sprites which fall down. When its fly out of screen I want to detach them from Scene. How can I do this optimally?
Now I use this code:
for (int i = 0; i < getScene().getChildCount(); i++) {
if (i < getScene().getChildCount())
removeSprite((Sprite) getScene().getChildByIndex(i));
}
private boolean removeSprite(final Sprite sprite) {
if ((sprite.getX()>CAMERA_WIDTH+sprite.getWidth()*2 || sprite.getX()<-sprite.getWidth()*2) && (sprite.getY()>CAMERA_HEIGHT+sprite.getHeight()*2 || sprite.getY()<0-sprite.getWidth()*2)) {
final PhysicsConnector spritePhysicsConnector = physicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(sprite);
physicsWorld.unregisterPhysicsConnector(spritePhysicsConnector);
physicsWorld.destroyBody(spritePhysicsConnector.getBody());
getScene().unregisterTouchArea(sprite);
getScene().detachChild(sprite);
}
}
But its not right way. What are the best solutions exist?

You can enable culling on Sprite object. That way it won't be rendered when it's out of screen.

Related

Is Unit and Android unable to precisely follow a finger?

I'm creating a Android game with Unity and I'm trying to make an object to follow my finger. This is the code I'm using.
for (int i = 0; i < Input.touchCount; i++)
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.touches[i].position), Vector2.zero);
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(Input.touches[i].position);
if (Input.GetTouch(i).phase == TouchPhase.Moved && hit.collider != null)
{
transform.position = new Vector2(touchPosition.x, touchPosition.y + sizeY);
}
}
I put the collider bellow the object and I added the size of the object to the Y, so it appears above my finger.
The problem is that if I move my finger slightly faster, I lose the object, that stays behind. Since the object lags behind, if I move my finger faster I end up out of the collider area and the object just stop moving.
The only way I found that works is to get rid of the Raycast thing and make the object follow the touch anywhere in the screen (I don't like that).
I just want to know if this is the best I can get from Unity and Android or if there a is way to make an object follow the finger in a one-by-one movement, with precision, without lag.
I'm testing on a Samsung Galaxy S8 just with the object and nothing else.
You are not alone with this issue. Unity's Input API is frame-rate based and may not keep with the speed of the touch movement on the screen. If you're working on something that requires fast response based on the touch on the screen, use the new Unity's InputSystem. This API is still in experimental mode.
Its Touchscreen class can be used to access the touch on the screen. Your current code should translate into something like below:
public int touchIndex = 0;
void Update()
{
Touchscreen touchscreen = UnityEngine.Experimental.Input.Touchscreen.current;
if (touchscreen.allTouchControls[touchIndex].ReadValue().phase != PointerPhase.None &&
touchscreen.allTouchControls[touchIndex].ReadValue().phase == PointerPhase.Moved)
{
Vector2 touchLocation = touchscreen.allTouchControls[touchIndex].ReadValue().position;
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(touchLocation), Vector2.zero);
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touchLocation);
transform.position = new Vector2(touchPosition.x, touchPosition.y + sizeY);
}
}

LibGDX texture bleeding issue

I'm new to LibGDX and was trying to implement parallax background.
Everything went good until I faced such issue: I get some stripes when scrolling background. You can see it in attached image:
So I looked deeper into an issue and figured out that this some sort of texture bleeding. But the case is that my textures already have [Linear, Nearest] filter set and TexturePacker uses duplicatePadding. Actually, I don't know any other methods to solve this issue. Please help!
Here's some of my code:
TexturePacker
TexturePacker.Settings settings = new TexturePacker.Settings();
settings.minWidth = 256;
settings.minHeight = 256;
settings.duplicatePadding = true;
TexturePacker.process(settings, "../../design", "./", "textures");
AssetLoader
textureAtlas = new TextureAtlas(Gdx.files.internal("textures.atlas"));
for (int i = 0; i < 2; i++) {
Background.skies.add(textureAtlas.findRegion("background/sky", i));
Background.skies.get(i).getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Nearest);
}
for (int i = 0; i < 2; i++) {
Background.clouds.add(textureAtlas.findRegion("background/cloud", i));
Background.clouds.get(i).getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Nearest);
}
for (int i = 0; i < 8; i++) {
Background.cities.add(textureAtlas.findRegion("background/city", i));
Background.cities.get(i).getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Nearest);
}
Background.moon = textureAtlas.findRegion("background/moon");
Background.forest = textureAtlas.findRegion("background/forest");
Background.road = textureAtlas.findRegion("background/road");
Background.moon.getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Nearest);
Background.forest.getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Nearest);
Background.road.getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Nearest);
BackgroundDrawer
private void drawParallaxTextureList(Batch batch, List<TextureAtlas.AtlasRegion> list,
float moveX, float posY) {
for (int i = 0; i < list.size(); i++) {
boolean needDraw = false;
float shift = GameScreen.VIEWPORT_WIDTH * i;
float drawX = 0.0f;
if (shift - moveX <= -(GameScreen.VIEWPORT_WIDTH)) { // If it's behind the screen
if (i == 0) { // If it's first element
if (moveX >= GameScreen.VIEWPORT_WIDTH * (list.size() - 1)) { // We need to show first after last
needDraw = true;
drawX = (GameScreen.VIEWPORT_WIDTH) - (moveX - ((GameScreen
.VIEWPORT_WIDTH) * (list.size() - 1)));
}
}
} else if (shift - moveX < (GameScreen.VIEWPORT_WIDTH - 1)) {
needDraw = true;
drawX = shift - moveX;
}
if (needDraw) {
batch.draw(list.get(i), (int) drawX, (int) posY);
}
}
}
NOTE: I don't use any camera for drawing right now. I only use FitViewport with size of 1920x1280. Also, bleeding sometimes appears even in FullHD resolution.
UPDATE: Setting both Nearest filters for minification and magification with increasing paddingX and disabling antialiasing solved issue, but final image become too ugly! Is there way to avoid disabling antialiasing? Because without it, downscale look awful.
Try to set both min and mag filters as Nearest
.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
In GUI TexturePacker there is an option to extrude graphics - it means repeating every of border pixel of texture. Then you can set both filters to Linear
.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
but unfortunately I cannot see this option in the TexturePacker.Settings object you are using. You can try to set Linear to both but I'm pretty sure it won't be working (Linear filter takes nearest 4 texels to generate the one so it will probably still generate issues).
Try to use GUI Texturepacker then with extrude option maybe
A few possible reasons for this artifact:
Maybe the padding is not big enough when the sprite resolution is shrunk down. Try changing your texture packer's filterMin to MipMapLinearNearest. And also try increasing the size of paddingX and paddingY.
Maybe you're seeing dim or brightened pixels at the edge of your sprite because you're not using pre-multiplied alpha and your texture's background color (where its alpha is zero) is white. Try setting premultiplyAlpha: true. If you do this, you need to also change the SpriteBatch's blend function to (GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA) to render properly.
You seem to be rounding your sprite positions and sizes to integers when you draw them. This would work in a pixel perfect game, where you're sure the sprites are being rendered exactly at 1:1 resolution to the screen. But once the screen size does not match exactly, your rounding might produce gaps that are less than 1 pixel wide, which will look like semi-transparent pixels.

Why isn't my touch screen input working on Android?

I wanted to make a little game with touch inputs and this is the code I made for it: (This is inside the Update method)
//Check if Input has registered more than zero touches
if(Input.touchCount > 0){
//Store the first touch detected.
Touch myTouch = Input.touches[0];
//Check if the phase of that touch equals Began
if (myTouch.phase == TouchPhase.Began)
{
//If so, set touchOrigin to the position of that touch
touchOrigin = myTouch.position;
if(touchOrigin.x < -2){
horizontalInput = -1;
} else if(touchOrigin.x > 2){
horizontalInput = 1;
}
if(touchOrigin.x > -2 && touchOrigin.x < 2 && touchOrigin.y < 0){
verticalInput = 1;
}
} else if(myTouch.phase == TouchPhase.Ended){
horizontalInput = 0;
verticalInput = 0;
}
}
This is my scene: http://i.stack.imgur.com/PGbd3.jpg
When I tried it on my android phone it only moved to the right and I don't know why.
(My camera's position is: x: -6,6; y:-2,6) And I'm using an orthographic camera
This is becase you're using Touch.position, which returns values based on SCREEN space, rather than WORLD space.
What you need to do is convert the point from Screen to World space before applying your conditions. You can do that by using Camera.ScreenToWorldPoint.

Snake collision detection with itself

I am currently writing some simple snake game in java. The snake objects has an array full of bodyparts. these bodyparts all contain a rectangle.
the snake's update method is like that:
public void update() {
if(isMoving) {
if(System.currentTimeMillis() - moveTimer > moveSpeed) {
if(parts.get(0).getX()+dx*size >= 0+GameView.GRID_PADDING_LEFT && parts.get(0).getY()+dy*size >= 0+GameView.GRID_PADDING_TOP && parts.get(0).getX()+dx*size <= ((GameView.GRID_WIDTH-1)*size)+GameView.GRID_PADDING_LEFT && parts.get(0).getY()+dy*size <= ((GameView.GRID_HEIGHT-1)*size)+GameView.GRID_PADDING_TOP) {
for(int i = parts.size()-1; i > 0; i--) {
parts.get(i).setLocation((int)(parts.get(i-1).getX()), (int)(parts.get(i-1).getY()));
}
parts.get(0).setLocation(parts.get(0).getRect().left+dx*size, parts.get(0).getRect().top+dy*size);
moveTimer = System.currentTimeMillis();
}else {
die();
}
}
}
}
All in one, the snake moves it's head into dx direction times one cellspace (size) --> dx*size and dy*size.
Then the rest of the body moves, the part behind the head gets the coordinates of the head, the part behind that part gets the coodrinates from the part in front of him etc.
Now I am trying to achieve some collision with the snake itself. I've tried several methods, e.g.:
if(parts.size() > 2) {
for(int i = 3; i < parts.size(); i++) {
if(parts.get(0).getRect().intersect(parts.get(i).getRect())) {
die();
}
}
}
But as soon as the bodypart array reaches the limit of 2 parts, the die() method gets called. I don't know why, because the 2nd part should sit two bodyparts behind the head, which makes it impossible to intersect with the head-rectangle.
Where's my problem?
Oh, that was a really big fail i made here.
When adding a new part, the new part gets the coordinates of the head. Of course will intersect with the head when it gets it's coordinates on create!!
Nevertheless, thanks for the replies!
new addPart method, corrected the parts.get(0) --> parts.get(parts.size()-1)
public void addPart(int amount) {
for(int i = 0; i < amount; i++) {
parts.add(new BodyPart(parts.get(parts.size()-1).getX(), parts.get(parts.size()-1).getY(), parts.get(parts.size()-1).getHue()-5));
}
}

How to optimize my Android flash game?

I have a game that I converted to my Android phone. I tried both renders, and found out that CPU mode is a better option than GPU for my type of game. In GPU mode, the game has a lot of lag and the graphics are not sharp as it is in CPU mode. The purpose of the game is to kill enemies coming from the top of the screen before they hit the bottom. When the game starts, it's not laggy at all, but then it starts to lag when there are a few enemies on the screen and they start coming down faster. This is the code of spawning the enemies:
function makeEnemies():void
{
var chance:Number = Math.floor(Math.random() * 150);
if (chance <= level && enemies.length < 4)
{
tempEnemy = new Enemy();
tempEnemy.x = Math.round(Math.random() * 480);
tempEnemy.cacheAsBitmap = true;
addChild(tempEnemy);
tempEnemy.scaleX = 1.5;
tempEnemy.scaleY = 1.5;
enemies.push(tempEnemy);
tempEnemy.speed = enemyBaseSpeed + ((level - 1) * speedLevelInc);
if (tempEnemy.speed > MAX_SPEED)
{
tempEnemy.speed = MAX_SPEED;
}
}
}
function moveEnemies():void
{
var tempEnemy:MovieClip;
for (var i:int =enemies.length-1; i>=0; i--)
{
tempEnemy = enemies[i];
if (tempEnemy.dead)
{
score++;
score++;
roachLevel.score_txt.text = String(score);
enemies.splice(i,1);
}
else
{
tempEnemy.rotation += (Math.round(Math.random()*.4));
tempEnemy.x -= (Math.sin((Math.PI/180)*tempEnemy.rotation))*tempEnemy.speed;
tempEnemy.y += (Math.cos((Math.PI/180)*tempEnemy.rotation))*tempEnemy.speed;
if (tempEnemy.x < 10)
{
tempEnemy.x = 11;
}
if (tempEnemy.x > stage.stageWidth - offset)
{
tempEnemy.x = stage.stageWidth - offset;
}
if (tempEnemy.y > stage.stageHeight)
{
removeEnemy(i);
lives--;
roachLevel.lives_txt.text = String(lives);
}
}
}
}
This code is always looped to spawn multiple enemies. All my images have been converted to bitmaps. Is there any other way for me to make my game with no lag? The faster they get, the more lag they have and the user can't kill them anymore. Please help!
There are certain optimizations you can make (whether or not it's enough I can't tell):
you don't need cacheAsBitmap if you've converted everything to bitmaps, so comment this out
don't set scale! this will definitely slow things down. Since scale always seems to be 1.5, just resize your graphics
don't set tempEnemy.rotation on each iteration, if possible. Can you make tempEnemy a MovieClip that "pre-rotates" each frame in the timeline? This will be way more efficient for Flash. The rotation can be set as a Number property in the MovieClip class if you still need it for calculating tempEnemy.x and y.
(There are several minor things you can optimize, like setting var K = (Math.PI/180)*tempEnemy.rotation) in the enemies loop-- but this won't gain you a lot)

Categories

Resources