简体   繁体   中英

object collision detection, force player movement 0

I don't understand why when I collide into my objects my character freezes. Such as if I collide into collision() I can go left and right but, not up and if I collide into collision2() I freeze in all places but, if I collide into the bounds I made I can still go in all directions without problems. The xa and ya are still being set to 0.

package com.questkings.game;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

public class Player{

int x = 1; // Location of player
int y = 314; // location of player
int xa = 0; // Representation of where the player goes
int ya = 0; // Representation of where the player goes
private int speed = 2;
int[] playerPos = {x, y};
private static final int WIDTH = 30;
private static final int HEIGHT = 30;

private Game game;

public Player(Game game){
    this.game=game;
}

public void move(){
    if(x + xa < 0) // Left Bounds
        xa = 0;
    else if (x + xa > game.getWidth() - WIDTH) // Right Bounds
        xa = 0;
    else if (y + ya < 0) // Top Bounds
        ya = 0;
    else if(y + ya > game.getHeight() - WIDTH)
        ya = 0;
    else if (collision()) // Tile bounds
        ya = 0;
    else if (collision2()){
        ya = 0;
        xa = 0;
    }

    x = x + xa;
    y = y + ya;
}

// Method to find where player is located
public int[] Playerposition(){
    return playerPos;
}

public void Gravity(){

}

public void paint(Graphics2D g2d){
    //Draws player to screen
    g2d.drawImage(getPlayerImg(), x, y, null);
}

public Image getPlayerImg(){
    ImageIcon ic = new ImageIcon("C:/Users/AncientPandas/Desktop/KingsQuest/Misc/Images/Sprites/player.png");
    return ic.getImage();
}

public void keyReleased(KeyEvent e){
    xa = 0;
    ya = 0;
}

public void keyPressed(KeyEvent e){
    if (e.getKeyCode() == KeyEvent.VK_S)
        xa = -speed;
    if (e.getKeyCode() == KeyEvent.VK_F)
        xa = speed;
    if (e.getKeyCode() == KeyEvent.VK_E)
        ya = -speed;
    if (e.getKeyCode() == KeyEvent.VK_D)
        ya = speed;
}

public Rectangle getBoundsPlayer(){
    return new Rectangle(x, y, WIDTH, HEIGHT);
}

private boolean collision(){
    return game.maplayout.getBoundsBlock().intersects(getBoundsPlayer());
}

private boolean collision2(){
    return game.maplayout.getBoundsBlock2().intersects(getBoundsPlayer());
}


}

It seems to me that if collision() returns true, your Player won't be able to move, as ya and xa are set to zero. This would mean that once you hit something, you're stuck there until you're not hitting it, which is completely out of your control. If the thing you're colliding with is motionless, you will never get away.

What I would do to fix this is check for a collision after moving:

// handle hitting sides like normal
x = x + xa;
y = y + ya;
// now that you've moved, check if you're colliding
if (collision()){
  // if you have, go back to where you were
  x = x - xa;
  y = y - ya;
}

This way, you can move back to where you were before, which should be safe, since you somehow got there before without colliding.

I'm not sure if your collision logic can handle that, so the only other thing I can say is to try multiplying ya and xa by -1 if you're colliding, which will send you in the opposite direction and hopefully out of harm's way. The idea is that moving in your desired direction got you into trouble, so moving the opposite way should fix it. The problem with this is that if moving back doesn't stop you from colliding, then you'll reverse direction again and most likely end up right where you started, still colliding. This forms an infinite loop where your player will most likely look like they're having a seizure, but I've seen plenty of games where similar physics bugs occurred.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM