碰撞检测关闭,Sprite + Tile

时间:2015-09-04 07:54:29

标签: java libgdx sprite collision-detection tile

我的游戏有一个不断向上移动的玩家。我想要它,所以当他击中一个区块的底部时,你输了,如果他击中一侧的挡块,他就会反弹回来。没有物理参与。无论出于何种原因,碰撞检测都没有按照预期的那样工作。例如,在下面的代码中,每次玩家击中底部时我都会重置位置。然而,它总是在他甚至击中瓷砖底部之前重置。为什么会发生这种情况?我该如何解决?

以下是我的播放器代码(GameIcon):

package com.xx4everPixelatedxx.gaterunner.sprites;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.xx4everPixelatedxx.gaterunner.GateRunner;
import com.xx4everPixelatedxx.gaterunner.gameObjects.Block;
import com.xx4everPixelatedxx.gaterunner.gameObjects.Spike;

import javax.xml.soap.Text;

/**
 * Created by Michael Jan on 8/17/2015.
 */
public class GameIcon extends Sprite {
    private float vX = 3;
    private float vY = 3;
    private float r = 9;

    private Texture texture;

    public GameIcon(int x, int y) {
        super(new Texture(Gdx.files.internal("icon_players/icon1.png")));
        setPosition(x, y);
//        texture = new Texture(Gdx.files.internal("icon_players/icon1.png"));
//        setTexture(texture);
    }

    public void update() {
            addPosition(vX, vY);
            setRotation( (getRotation() + r) % 360);
            setOriginCenter();
    }

    public void update(TiledMap map) {
        addPosition(vX, vY);
        setRotation((getRotation() + r) % 360);
        setOriginCenter();

        //block
        for(MapObject object : map.getLayers().get(2).getObjects().getByType(RectangleMapObject.class))
        {
            Rectangle rect = ((RectangleMapObject)object).getRectangle();
            if(Intersector.overlaps(getBoundingRectangle(), rect))
            {
                setPosition(GateRunner.WIDTH/2 - GateRunner.WIDTH/20, GateRunner.HEIGHT/10);
                if(getY() <= rect.getY() - getHeight() + vX)
                {
                    System.out.println("bottom");
                    setPosition(GateRunner.WIDTH/2 - GateRunner.WIDTH/20, GateRunner.HEIGHT/10);
                }
                else
                {
                    System.out.println("side");
                    negateVelocityX();
                    negateRotation();
                }

            }
        }

        //spike
        for(MapObject object : map.getLayers().get(3).getObjects().getByType(RectangleMapObject.class))
        {
            Rectangle rect = ((RectangleMapObject)object).getRectangle();
            if(rect.overlaps(getBoundingRectangle()))
            {

            }
        }
    }

    public void addPosition(float x, float y) {
        setPosition(getX() + x, getY() + y);
        setOriginCenter();
    }

    public void negateVelocityX() {
        if(vX < 0)
        {
            addPosition((int)(getWidth()*0.05), 0);
        }
        if(vX > 0)
        {
            addPosition(-(int)(getWidth()*0.05), 0);
        }
        vX = -vX;
    }

    public void negateRotation() {
        r = -r;
    }

    public float getvX() {
        return vX;
    }

    public void setvX(int vX) {
        this.vX = vX;
    }

    public float getvY() {
        return vY;
    }

    public void setvY(int vY) {
        this.vY = vY;
    }

    public float getR() {
        return r;
    }

    public void setR(int r) {
        this.r = r;
    }
}

0 个答案:

没有答案