2017-05-30 2 views
0

Dans mon jeu de haut en bas, comment puis-je faire entrer en collision mon joueur lorsqu'il traverse le lit de bébé? J'utilise intersectRectangles.Détection de collision entre les rectangles libgdx

Voici mon code

Rectangle player = new Rectangle(); 
Rectangle babycrib = new Rectangle(); 
Rectangle intersection = new Rectangle(); 


// Load the sprite sheet as a texture 
    cat = new Texture(Gdx.files.internal("spriteCatsheet.png")); 
    catsprite = new Sprite(cat); 
    player = new Rectangle(); 
    player.x = Gdx.graphics.getWidth() - player.width - 350; 

    baby = new Texture(Gdx.files.internal("baby.png")); 
    sprite_baby = new Sprite(baby); 
    babycrib = new Rectangle(); 
    sprite_baby.setPosition(180,4000); 

mise à jour Méthode

public void update(){ 
    deltaTime = Gdx.graphics.getDeltaTime(); 
    camera.update(); 
} 

En méthode render

// check collision 
    Intersector.intersectRectangles(player, babycrib, intersection); 
    if(intersection.x > player.x) 
     //Intersects with right side 
    if(intersection.y > player.y) 
      //Intersects with top side 
    if(intersection.x + intersection.width < player.x + player.width) 
       //Intersects with left side 
    if(intersection.y + intersection.height < player.y + player.height) 
        //Intersects with bottom side 
    Intersector.overlaps(player,babycrib); 

Voici le code complet

public class GameScreen implements Screen ,InputProcessor { 

final MyGdxGame game; 
// Constant rows and columns of the sprite sheet 
private static final int FRAME_COLS = 8, FRAME_ROWS = 4; 
private boolean peripheralAvailable; 
// Objects used 
Animation<TextureRegion> walkAnimation; // Must declare frame type (TextureRegion) 
Texture left_paw,right_paw; 

Texture baby,cat; 
SpriteBatch spriteBatch; 
Sprite catsprite,sprite_baby; 
ImageButton moveBackward,moveForward; 
Viewport viewport; 
private Stage stage; 
private static float fade; 
// A variable for tracking elapsed time for the animation 
float stateTime; 
private TextureRegion myTextureRegion; 
TextureRegion textureRegion; 
private TextureRegionDrawable myTexRegionDrawable; 

OrthographicCamera camera; 
Rectangle player = new Rectangle(); 
Rectangle babycrib = new Rectangle(); 
Rectangle intersection = new Rectangle(); 


float deltaTime; 
int progressKnobX = 18; 
Float fadeTime = 1f; 

public GameScreen(final MyGdxGame game) { 
    this.game = game; 
    stage = new Stage(new StretchViewport(720, 1280)); 
    camera = new OrthographicCamera(1280 ,720); 
    Gdx.input.setCatchBackKey(true); 
    camera.update(); 
    Gdx.graphics.setContinuousRendering(true); 
    Gdx.graphics.requestRendering(); 
    camera.setToOrtho(false, 720, 1280); 
    Gdx.input.setInputProcessor(stage); 
    spriteBatch = new SpriteBatch(); 
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 
    Gdx.input.setInputProcessor(this); 
    peripheralAvailable = Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer); 
    viewport = new ScreenViewport(); 

    baby = new Texture(Gdx.files.internal("equip/baby.png")); 
    sprite_baby = new Sprite(baby); 
    babycrib = new Rectangle(); 
    sprite_baby.setPosition(180,4000); 


    // Load the sprite sheet as a texture 
    cat = new Texture(Gdx.files.internal("spriteCatsheet.png")); 
    catsprite = new Sprite(cat); 
    player = new Rectangle(); 
    player.x = Gdx.graphics.getWidth() - player.width - 350; //250; //550 // 410 
    // Use the split utility method to create a 2D array of TextureRegions. This is 
    // possible because this sprite sheet contains frames of equal size and they are 
    // all aligned. 
    TextureRegion[][] tmp = TextureRegion.split(cat, cat.getWidth()/ FRAME_COLS , cat.getHeight()/ FRAME_ROWS); 
    // Place the regions into a 1D array in the correct order, starting from the top 
    // left, going across first. The Animation constructor requires a 1D array. 
    TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS]; 
    int index = 0; 
    for (int i = 0; i < FRAME_ROWS; i++) { 
     for (int j = 0; j < FRAME_COLS; j++) { 
      walkFrames[index++] = tmp[i][j]; 
     } 
    } 
    // Initialize the Animation with the frame interval and array of frames 
    walkAnimation = new Animation<TextureRegion>(0.099f, walkFrames); 
    // Instantiate a SpriteBatch for drawing and reset the elapsed animation 
    // time to 0 
    spriteBatch = new SpriteBatch(); 
    stateTime = 0f; 
    //left_control 
    left_paw = new Texture(Gdx.files.internal("left_paw.png")); 
    myTextureRegion = new TextureRegion(left_paw); 
    myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion); 
    moveBackward = new ImageButton(myTexRegionDrawable); //Set the button up 
    moveBackward.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("left_paw.png")))); 
    //the hover 
    moveBackward.getStyle().imageDown = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("left_paw_hover.png")))); 
    moveBackward.setPosition(10,25); 
    stage.addActor(moveBackward); //Add the button to the stage to perform rendering and take input. 
    Gdx.input.setInputProcessor(stage); 
    moveBackward.addListener(new InputListener(){ 
     @Override 
     public void touchUp (InputEvent event, float x, float y, int pointer, int button) { 
      System.out.println("Left Button Pressed"); 
      //Start Animation 
      progressKnobX = progressKnobX - 4; 
      Gdx.graphics.setContinuousRendering(true); 
      motionState=MotionState.NONE; 
     } 
     @Override 
     public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { 
      System.out.print("Released"); 
      Gdx.graphics.setContinuousRendering(false); 
      motionState=MotionState.DOWN; 
      return true; 
     } 
    }); 
    stage.addActor(moveBackward); 
    //right_control 
    right_paw = new Texture(Gdx.files.internal("right_paw.png")); 
    myTextureRegion = new TextureRegion(right_paw); 
    myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion); 
    moveForward = new ImageButton(myTexRegionDrawable); //Set the button up 
    moveForward.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("right_paw.png")))); 
    //the hover 
    moveForward.getStyle().imageDown = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("right_paw-hover.png")))); 
    moveForward.setPosition(517,25); 
    stage.addActor(moveForward); //Add the button to the stage to perform rendering and take input. 
    Gdx.input.setInputProcessor(stage); 
    moveForward.addListener(new InputListener(){ 
     @Override 
     public void touchUp (InputEvent event, float x, float y, int pointer, int button) { 
      System.out.println("Right Button Pressed"); 
      progressKnobX = progressKnobX + 4; 

      motionState=MotionState.NONE; 
     } 
     @Override 
     public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { 
      motionState=MotionState.UP; 
      return true; 
     } 
    }); 
    stage.addActor(moveForward); 
} 
public enum State 
{ 
    PAUSE, 
    RUN, 
    RESUME, 
    STOPPED 
} 
private State state = State.RUN; 

MotionState motionState=MotionState.NONE; 
enum MotionState { 
    NONE { 
     @Override 
     public boolean update(Rectangle player) { 
      return true; 
     } 
    }, 

    UP { 
     @Override 
     public boolean update(Rectangle player) { 
      player.y += 300 * Gdx.graphics.getDeltaTime(); 
      return false; 
     } 
    }, 
    DOWN{ 
     @Override 
     public boolean update(Rectangle player) { 
      player.y -= 300 * Gdx.graphics.getDeltaTime(); 
      return false; 
     } 
    }, 
    LEFT{ 
     @Override 
     public boolean update(Rectangle player) { 
      player.x -= 100 * Gdx.graphics.getDeltaTime(); 
      return false; 
     } 
    }, 
    RIGHT{ 
     @Override 
     public boolean update(Rectangle player) { 
      player.x += 100 * Gdx.graphics.getDeltaTime(); 
      return false; 
     } 
    }; 
    public abstract boolean update(Rectangle player); 
} 

@Override 
public void show() { 
} 
public void update(){ 
deltaTime = Gdx.graphics.getDeltaTime(); 
camera.position.x += 10; 
camera.position.y += 10; 
camera.update(); 
} 
@Override 
public void render(float delta) { 
    // clear previous frame 
    Gdx.gl.glClearColor(0.294f, 0.294f, 0.294f, 1f); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear screen 
    stateTime += Gdx.graphics.getDeltaTime(); // Accumulate elapsed animation time 
    camera.update(); 
    update(); 
    spriteBatch.begin(); 
    stateTime += Gdx.graphics.getDeltaTime(); 
    TextureRegion currentFrame = walkAnimation.getKeyFrame(stateTime, true); 
    camera.position.x = player.getX() + 100; //190 
    camera.position.y = player.getY() + 180; 
    camera.position.x = 350; 
    update(); 
    spriteBatch.setProjectionMatrix(camera.combined); 
    spriteBatch.draw(currentFrame,player.x, player.y); 
    sprite_baby.draw(spriteBatch); 


    if(Gdx.input.isKeyPressed(Input.Keys.DOWN)) motionState = MotionState.DOWN; 
    if(Gdx.input.isKeyPressed(Input.Keys.UP)) motionState=MotionState.UP; 
    if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) motionState=MotionState.LEFT; 
    if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) motionState=MotionState.RIGHT; 

    if(motionState.update(player)) motionState=MotionState.NONE; 


    // check collision 
    Intersector.intersectRectangles(player, babycrib, intersection); 
    if(intersection.x > player.x) 
     //Intersects with right side 
     if(intersection.y > player.y) 
      //Intersects with top side 
      if(intersection.x + intersection.width < player.x + player.width) 
       //Intersects with left side 
       if(intersection.y + intersection.height < player.y + player.height) 
        //Intersects with bottom side 
        Intersector.overlaps(player,babycrib); 
    //Intersects with bottom side 


    if(!player.overlaps(babycrib)){ 
     Gdx.app.log("babycrib overlaps", "yes"); 
    } 

    //Mobile acceleration 
    if (Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer)) { 
     player.x -= Gdx.input.getAccelerometerX(); 
    } 
    if (player.x < 0) { 
     player.x = 0; 
     player.x += Gdx.graphics.getDeltaTime() *20 *delta; 
    } 
    if (player.x > Gdx.graphics.getWidth()-player.getWidth() -150) { 
     player.x = Gdx.graphics.getWidth()-player.getWidth() -150; 
    } 
    if(this.state==State.RESUME) { 
     switch (state) { 
      case RUN: 
       //do suff here 
       break; 
      case PAUSE: 
       break; 
      case RESUME: 
       break; 
      default: 
       break; 
     } 
    } 
    spriteBatch.end(); 
    stage.act(); //acting a stage to calculate positions of actors etc 
    stage.draw(); //drawing it to render all 
} 


@Override 
public void resize(int width, int height) { 
    viewport.update(width, height); 
    camera.position.set(camera.viewportWidth/2, camera.viewportHeight/2, 0); 
    camera.update(); 
} 
@Override 
public void pause() { 

} 
@Override 
public void resume() { 

} 

@Override 
public boolean keyDown(int keycode) { 
    return false; 
} 

@Override 
public boolean keyUp(int keycode) { 
    return false; 
} 
@Override 
public boolean keyTyped(char character) { 
    return false; 
} 
@Override 
public boolean touchDown(int screenX, int screenY, int pointer, int button) { 
    return false; 
} 
@Override 
public boolean touchUp(int screenX, int screenY, int pointer, int button) { 
    return false; 
} 
@Override 
public boolean touchDragged(int screenX, int screenY, int pointer) { 
    return false; 
} 
@Override 
public boolean mouseMoved(int screenX, int screenY) { 
    return false; 
} 
@Override 
public boolean scrolled(int amount) { 
    return false; 
} 
@Override 
public void hide() { 
} 
@Override 
public void dispose() { // SpriteBatches and Textures must always be disposed 
} 
} 

Quelqu'un peut-il jusqu'à moi quelle est la bonne implémentation de la détection de collision rectangle il n'y a pas de chevauchement se produire, je suis nouveau à ce cadre. Merci et avance :)

+1

position de mise à jour de 'player' et 'babycrib' et vérifiez qu'il n'y a pas de collision entre ces deux quand' if (! player.overlaps (babycrib)) {} ' – Aryan

+0

J'ai déjà fait monsieur @AbhishekAryan et ne peux toujours pas entrer en collision avec le babycrib. – jaZzZ

+0

comment vous mettez à jour la position de 'player' et' babycrib', veuillez ajouter ce code – Aryan

Répondre

1

Vous pouvez utiliser pour la détection de collision régulière. Vous pouvez également utiliser Intersector pour cela, mais cela calcule également la zone de chevauchement qui se produit.

if (Intersector.intersectRectangles(player, babycrib, intersection)) 
{ 
    //player and babycrib are intersecting... 
    if (intersection.contains(babyRoom)) 
    { 
    //Collision happened in baby room. 
    } 
} 
+0

Je vais essayer ce Sir @Madmenyo :) – jaZzZ

+0

Bonne journée Sir @Madmenyo J'essaie vos codes cela fonctionne, mais comment puis-je croiser les deux côtés du rectangle? la gauche et la droite du babycrib. – jaZzZ

+0

Vous voulez dire où il entre en collision? Voici une réponse à cette question: https://gamedev.stackexchange.com/a/29796/20075 – Madmenyo

0

largeur définie et la hauteur de vos rectangles, par exemple:

player = new Rectangle(); 
player.setWidth(catsprite.getWidth());  
player.setHeight(catsprite.getHeight()); 

babycrib = new Rectangle(); 
babycrib.setWidth(babysprite.getWidth()); 
babycrib.setHeight(babysprite.getHeight()); 
1

Votre code est en quelque sorte en désordre, peut-être parce que vous êtes nouveau pour ce cadre, je ne peux pas dire exactement ce qui se passe faux.

Mais au point de détection de collision, il semble que vous êtes seulement la mise à jour de la position de player rectangle qui ayant zéro largeur et hauteur. Aussi ne change pas la position du rectangle babycrib qui a aussi la taille zéro.


Vous utilisez:

babycribRectangle < ----- POUR ------->sprite_babySprite
playerRectangle < ----- POUR ---- ->catspriteSprite

ne pas créer de nouvelles Rectangle pour Sprite, ayant Sprite propre bounds membre de type de type Rectangle donc utilisez des limites au lieu du nouveau rectangle.

Chaque fois que vous souhaitez accéder sprite_baby rectangle utilisez sprite_baby.getBoundingRectangle() et quand vous voulez rectangle de catsprite utiliser catsprite.getBoundingRectangle().

Si vous ne voulez pas changer plus dans votre code, garder référence du rectangle de Sprite à votre variable Rectangle comme,

sprite_baby = new Sprite(baby); 
babycrib = sprite_baby.getBoundingRectangle(); 

Et

catsprite = new Sprite(cat); 
player = catsprite.getBoundingRectangle(); 
+0

Merci Monsieur quand j'essaie' catsprite = nouveau Sprite (cat); ' ' player = catsprite.getBoundingRectangle(); 'Mon lecteur ne s'affiche pas sur mon écran. – jaZzZ

+0

Position d'impression du lecteur sur la console, vérifiez s'il est à l'écran ou non. Vous changez également la position de la caméra, alors assurez-vous que le joueur est dans la fenêtre de la caméra. – Aryan