Java - Detection collision in 2D tile game - java

I'm having fun to build a 2D tile game (without Slick2D of libgdx) and I'm working on collision since more than 15 hours. I think it's time to ask question. I searched a lot but none really responds what I searched for.
So, this is what it look like : 2D game capture (The dark tile are were the player can't walking in) and Another 2D game capture
The rectangle are were collision can happen. There, when they collide, it's still moving just enough to be lock in the tile. (can't do nothing anymore)
So, my class is called by the input handler, the update and the draw. Theses three by the "play" class. Who just update stuff. (Player.MoveUp(), Player.draw, ...)
The "WorldManager" is where the tiles are managed (the only 4 of them and the getType() is to look up if it's blocked (0 valid and 1 blocked)
The "LevelGeneration" is where level, position of tiles and their ID is
mapped (with a int[][]) (he is ok' it create map with ID and when I
show then they are all good).
The AssetManager is where I cut sprites sheets to transform then in BufferedImage
The tilesize is 50 and the player is 41x36.
I have a problem since I want to have a liberty of mouvements (4 sides to make 8 mouvements sides ) and the anchor point seem to be at upperleft side
So here is my class (without imports and packages):
public class Player {
public static double SPEED;
public static int WIDTH, HEIGHT;
public static float XPos, YPos;
public static float destXPos, destYPos;
public static BufferedImage presentImage;
public static int[] position;
public static boolean moving;
public static boolean movementlock;
public static int side; // 0 up 1 right 2 down 3 left
public static Rectangle p = null;
public static Rectangle e = null;
private static boolean mouvementlock;
private static boolean collision;
public static void CreateHero(){
// Valeurs de test
position = new int[2];
SPEED = 1;
XPos = destXPos = 100;
YPos = destYPos = 100;
presentImage = AssetManager.Player[0][0];
moving = false;
movementlock = false;
collision = false;
position[0] = (int) (XPos/WorldManager.TileSize);
position[1] = (int) (YPos/WorldManager.TileSize);
}
public static void MoveUp(){
side = 0;
destYPos -= SPEED;
moving = ValidateNextPosition();
}
public static void MoveRight(){
side = 1;
destXPos += SPEED;
moving = ValidateNextPosition();
}
public static void MoveDown(){
side = 2;
destYPos += SPEED;
moving = ValidateNextPosition();
}
public static void MoveLeft(){
side = 3;
destXPos -= SPEED;
moving = ValidateNextPosition();
}
static void updateMovement(){
if(moving == true && mouvementlock == false){
mouvementlock = true;
System.out.println("Mouvement");
System.out.println("X : " + XPos);
System.out.println("Y : " + YPos);
XPos = destXPos;
YPos = destYPos;
System.out.println("Xdest : " + destXPos);
System.out.println("Ydest : " + destYPos);
System.out.println("" + moving);
moving = false;
mouvementlock = false;}
else{
destXPos = XPos;
destYPos = YPos;
}
}
public static boolean ValidateNextPosition(){
if(moving) return false;
if(collision) return false;
p = new Rectangle((int) XPos, (int) YPos, presentImage.getWidth(null),presentImage.getHeight(null));
int tileid = 0;
boolean validate = true;
int destpos[] = new int[2];
double TileCollision[] = new double[2];
destpos[0] = (int) destXPos/WorldManager.TileSize;
destpos[1] = (int) destYPos/WorldManager.TileSize;
TileCollision[0] = 0;
TileCollision[1] = 0;
if(side == 0 && collision != true){
if(destpos[1] - 1 > 0){
tileid = LevelGenerator.getIdmap()[destpos[0]][destpos[1] - 1];
if(destYPos + 1 < 0 || WorldManager.Tuiles[tileid].getType() == 1 ){
destpos[1] -= 1;collision = true;}}
}
if(side == 1 && collision != true){
tileid = LevelGenerator.getIdmap()[destpos[0] + 1][destpos[1]];
if(destXPos + 1 < 0 || WorldManager.Tuiles[tileid].getType() == 1 ){
destpos[0] += 1;collision = true;}
}
if(side == 2 && collision != true){
tileid = LevelGenerator.getIdmap()[destpos[0]][destpos[1] + 1];
if(destYPos - 1 < 0 || WorldManager.Tuiles[tileid].getType() == 1 ){
destpos[1] += 1;collision = true;}
}
if(side == 3 && collision != true){
tileid = LevelGenerator.getIdmap()[destpos[0]][destpos[1]];
if(destXPos - 1 < 0 || WorldManager.Tuiles[tileid].getType() == 1 ){
/*destpos[0] -= 1*/;collision = true;}
}
if (collision == true){
TileCollision[0] = LevelGenerator.getPosmap()[destpos[0]][destpos[1]][0];
TileCollision[1] = LevelGenerator.getPosmap()[destpos[0]][destpos[1]][1];
e = new Rectangle((int) TileCollision[0], (int) TileCollision[1],WorldManager.TileSize,WorldManager.TileSize);}
if(e != null && p.intersects(e)){
validate = false;
System.out.println("collision");}
return validate;
}
public static void update() {
updateMovement();
}
public static void draw(Graphics2D g) {
g.drawImage(presentImage,(int) XPos,(int) YPos,presentImage.getWidth(null),presentImage.getWidth(null),null);
if(p != null){
g.setColor(Color.BLACK);
g.drawRect(p.x, p.y, p.width, p.height);}
if(e != null){
g.setColor(Color.BLACK);
g.drawRect(e.x, e.y, e.width, e.height);}
}
}
To resume,
The collision happen too late and the player get stuck in the blocked block.
Sometimes it pass through it since the (I suppose) the top left don't hit the block.
It can go out of the map without problems and then it crashed, but everything I tried keep crashing the game
Thank you very much and have a nice day
Hell'no
(P.S. : English is my third language, so ask me if you don't completely understand)

When collision gets set to true and your ValidateNextPosition method returns false, collision (and by extension your moving variable) can never be toggled again because of your if(collision) return false statement at the top.

Related

How do I create a restart button that shows in the button left corner while the game is running?

Our restart button is only appearing by itself. Our two tanks disappear when we run the game. We haven't coded the function of the button, we're just trying to figure out how to make the button appear in the lower left corner
We moved the code into the game object class, but it does not change anything
This is our game object class. The button is at the bottom.
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GameObject {
// Every moving object in our game is a separate GameObject. However, since there are different types of moving objects (eg. players and
// projectiles), each with their own variables and functions, we don't want to instantiate GameObjects (otherwise, we wouldn't be able
// to distinguish between a player and a projectile). Instead, we want to create derived (also known as child) classes to implement the
// separate functionality, and instantiate the derived/child classes instead. The purpose of this GameObject "parent" class is to
// provide the derived/child classes with a set of useful functions for handling physics interactions (eg. collisions with the terrain
// or the walls of the arena) between different GameObjects (eg. player/player, player/projectile, or even projectile/projectile). Note:
// depending on the features you implement, you may not end up using all of the functions provided in this class.
// Every GameObject contains its own velocity, position, size, and mass information (necessary for moving and handling physics
// interactions). We store this information in the GameObject class instead of the derived/child classes because every type of
// GameObject has the same set of velocity, position, size, and mass variables.
public double vX; // Velocity in the x direction. Positive if moving right. Negative if moving left.
public double vY; // Velocity in the y direction. Positive if moving down. Negative if moving up. Notice how it's flipped from the usual
// coordinate system in math!
public double posX; // Position along the x direction. Ranges from 0 (left edge) to maximumX (see below).
public double posY; // Position along the y direction. Ranges from 0 (top edge) to maximumY (see below).
public int width; // Width of the bounding box of the GameObject.
public int height; // Height of the bounding box of the GameObject.
public int mass; // Used in realistic physics collision calculations. Ignore it if you don't want to implement that feature.
public double radius; // Used for circular GameObjects only.
public int maximumX; // Maximum x position for a GameObject, equal to the arena width subtracted by the game object's width.
public int maximumY; // Maximum y position for a GameObject, equal to the arena height subtracted by the game object's height.
// Constructor. All derived (ie. child) classes call this constructor in their own constructors. The resulting derived/child class
// object can then call the other functions in this class.
public GameObject(int arenaWidth, int arenaHeight, double vX, double vY, double posX, double posY, int width, int height, int mass) {
this.vX = vX;
this.vY = vY;
this.posX = posX;
this.posY = posY;
this.width = width;
this.height = height;
this.mass = mass;
this.maximumX = arenaWidth - width;
this.maximumY = arenaHeight - height;
radius = Math.min(width, height) / 2.0;
}
// Note: No need to change this function since we're going to override it in the the child classes.
public boolean move(Map map, double translateX, double translateY) {
return false;
}
// Check if the calling GameObject currently intersects with the obj GameObject.
public boolean currentlyIntersects(GameObject obj) {
return (posX + width >= obj.posX && posY + height >= obj.posY && obj.posX + obj.width >= posX && obj.posY + obj.height >= posY);
}
// Check if the calling GameObject will intersect with the obj GameObject, after both have moved according to their velocities. A note
// of caution: what might go wrong if either player moves too fast?
public boolean willIntersect(GameObject obj) {
double nextX = posX + vX;
double nextY = posY + vY;
double nextObjX = obj.posX + obj.vX;
double nextObjY = obj.posY + obj.vY;
return (nextX + width >= nextObjX && nextY + height >= nextObjY && nextObjX + obj.width >= nextX && nextObjY + obj.height >= nextY);
}
// Clip the calling GameObject to within the arena's x bounds, if it has moved outside the arena along the x direction.
public boolean xClip() {
if (posX < 0) {
posX = 0;
return true;
} else if (posX > maximumX) {
posX = maximumX;
return true;
}
return false;
}
// Clip the calling GameObject to within the arena's y bounds, if it has moved outside the arena along the y direction.
public boolean yClip() {
if (posY < 0) {
posY = 0;
return true;
} else if (posY > maximumY) {
posY = maximumY;
return true;
}
return false;
}
// If the calling GameObject will move outside the arena along either direction (after moving according to its velocity), this function
// tells you which of the four edges of the arena it hit. If the calling GameObject will stay within the bounds of the arena, this
// function returns null.
public Direction hitEdgeDirection() {
if (posX + vX < 0) {
return Direction.LEFT;
} else if (posX + vX > maximumX) {
return Direction.RIGHT;
}
if (posY + vY < 0) {
return Direction.UP;
} else if (posY + vY > maximumY) {
return Direction.DOWN;
} else {
return null;
}
}
// If the calling GameObject will intersect with the "other" GameObject (after both move according to their velocities), this function
// tells you which of the four sides of the calling GameObject that the "other" GameObject hit. If the calling GameObject will not
// intersect with the "other" GameObject, this function returns null. Note: this function is great for figuring out when and where two
// rectangles intersect, but is it a good choice for handling circle/rectangle or circle/circle intersections?
public Direction hitObjectDirection(GameObject other) {
if (this.willIntersect(other)) {
double dx = other.posX + other.width / 2.0 + other.vX - (posX + width / 2.0 + vX);
double dy = other.posY + other.height / 2.0 + other.vY - (posY + height / 2.0 + vY);
double theta = Math.acos(dx / (Math.sqrt(dx * dx + dy * dy)));
double diagTheta = Math.atan2(height / 2.0, width / 2.0);
if (theta <= diagTheta) {
return Direction.RIGHT;
} else if (theta <= Math.PI - diagTheta) {
if (dy > 0) {
return Direction.DOWN;
} else {
return Direction.UP;
}
} else {
return Direction.LEFT;
}
} else {
return null;
}
}
// Change the calling GameObject's velocity (to simulate a "bouncing" effect) based on which direction it intersected another GameObject
// or the edge of the arena. If the passed in direction is null, this function does nothing (why is this a good idea?). This function is
// best used with the hitEdgeDirection and hitObjectDirection functions above.
public void bounce(Direction d) {
if (d == null) {
return;
}
// Note: We probably should use a "switch" statement here instead. But for pedagogical purposes it's left as a simple if/else
// conditional.
if (d == Direction.UP) {
vY = Math.abs(vY);
} else if (d == Direction.DOWN) {
vY = -Math.abs(vY);
} else if (d == Direction.LEFT) {
vX = Math.abs(vX);
} else if (d == Direction.RIGHT) {
vX = -Math.abs(vX);
}
}
// TODO: (Challenge!) If you want to implement realistic sphere-sphere collisions that take into account the laws of physics, do so in
// the function below.
public boolean bounceWith(GameObject otherObj, Map map, long frames, double[] actualVelocities) {
return false;
}
// Calculate the distance from (pointX, pointY)---perhaps representing the center of a circle---to the closest point on a rectangle
// bounded by minX (left), maxX (right), minY (top), and maxY (bottom). If the point is inside the rectangle, this function returns 0.
public double pointToRectSqrDist(double minX, double maxX, double minY, double maxY, double pointX, double pointY) {
double dx = Math.max(Math.max(minX - pointX, 0), pointX - maxX);
double dy = Math.max(Math.max(minY - pointY, 0), pointY - maxY);
return dx * dx + dy * dy;
}
// Rotate the point (x, y) "degrees" degrees around (centerX, centerY) in counterclockwise fashion, and return the resulting point in an
// array of length 2. If the returned array is "result", then (result[0], result[1]) is the final point.
public double[] rotatePoint(double centerX, double centerY, double degrees, double x, double y) {
double s = Math.sin(Math.toRadians(degrees));
double c = Math.cos(Math.toRadians(degrees));
x -= centerX;
y -= centerY;
double xNew = x * c - y * s;
double yNew = x * s + y * c;
double[] result = new double[2];
result[0] = xNew + centerX;
result[1] = yNew + centerY;
return result;
}
// Note: No need to change this function since we're going to override it in the the child classes.
public void draw(Graphics g) {
}
public static void main(String []args){
JButton b= new JButton("Reset");
JFrame f = new JFrame();
f.setSize(1200,800);
f.setVisible(true);
f.getDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
f.add(p);
p.add(b);
b.setSize(50,50);
b.setVisible(true);
b.setLocation(50, 50);
}
}
This is our arena class:
// TODO: Feel free to import any other libraries that you need.
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
#SuppressWarnings("serial")
public class Arena extends JPanel {
public int arenaWidth;
public int arenaHeight;
public Player player1;
public Player player2;
Timer timer;
public static int INTERVAL = 35;
public long lastTick;
// TODO: Add other variables to keep track of the game state or other game objects (eg. the map) that will be in your game. Don't forget
// to instantiate them in reset()!
// Constructor. Called inside Game.java for setting up the Arena on game start.
public Arena() {
// Create a timer that calls the tick() function every INTERVAL milliseconds. Every call of the tick() function is a "frame".
timer = new Timer(INTERVAL, new ActionListener() {
public void actionPerformed(ActionEvent e) {
tick();
}
});
lastTick = System.currentTimeMillis();
timer.start();
setFocusable(true);
// TODO: To recognize key presses, you need to fill in the following.
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent butthole) {
if (butthole.getKeyCode() == KeyEvent.VK_W) {
player1.isWPressed = true;
}
if (butthole.getKeyCode() == KeyEvent.VK_S) {
player1.isSPressed = true;
}
if (butthole.getKeyCode() == KeyEvent.VK_A) {
player1.isAPressed = true;
}
if (butthole.getKeyCode() == KeyEvent.VK_D) {
player1.isDPressed = true;
}
if (butthole.getKeyCode() == KeyEvent.VK_SPACE) {
player1.isSpacePressed = true;
}
if (butthole.getKeyCode() == KeyEvent.VK_UP) {
player2.isUpPressed = true;
}
if (butthole.getKeyCode() == KeyEvent.VK_DOWN) {
player2.isDownPressed = true;
}
if (butthole.getKeyCode() == KeyEvent.VK_LEFT) {
player2.isLeftPressed = true;
}
if (butthole.getKeyCode() == KeyEvent.VK_RIGHT) {
player2.isRightPressed = true;
}
if (butthole.getKeyCode() == KeyEvent.VK_ENTER) {
player2.isEnterPressed = true;
}
}
public void keyReleased(KeyEvent butthole) {
if (butthole.getKeyCode() == KeyEvent.VK_W) {
player1.isWPressed = false;
}
if (butthole.getKeyCode() == KeyEvent.VK_S) {
player1.isSPressed = false;
}
if (butthole.getKeyCode() == KeyEvent.VK_A) {
player1.isAPressed = false;
}
if (butthole.getKeyCode() == KeyEvent.VK_D) {
player1.isDPressed = false;
}
if (butthole.getKeyCode() == KeyEvent.VK_SPACE) {
player1.isSpacePressed = false;
}
if (butthole.getKeyCode() == KeyEvent.VK_UP) {
player2.isUpPressed = false;
}
if (butthole.getKeyCode() == KeyEvent.VK_DOWN) {
player2.isDownPressed = false;
}
if (butthole.getKeyCode() == KeyEvent.VK_LEFT) {
player2.isLeftPressed = false;
}
if (butthole.getKeyCode() == KeyEvent.VK_RIGHT) {
player2.isRightPressed = false;
}
if (butthole.getKeyCode() == KeyEvent.VK_ENTER) {
player2.isEnterPressed = false;
}
}
});
}
// Resets the game to its initial state.
public void reset() {
this.removeAll();
this.setBackground(Color.WHITE);
this.setOpaque(true);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
arenaWidth = (int) screenSize.getWidth();
arenaHeight = (int) screenSize.getHeight();
player1 = new Player(arenaWidth, arenaHeight, 100, 100, 1);
player2 = new Player(arenaWidth, arenaHeight, arenaWidth - Player.INIT_SIZE - 100, arenaHeight - Player.INIT_SIZE - 100, 2);
requestFocusInWindow();
}
// Function called once per "frame".
void tick() {
// While tick() should be called once every INTERVAL amount of time, there's no guarantee of that, particularly if you have a lot
// of background apps running. Thus, we need to calculate the time difference (timeDelta) between every two calls of the tick()
// function. Note: 1 divided by this difference is commonly known as the "frames per second", or fps.
long currentTime = System.currentTimeMillis();
long timeDelta = (currentTime - lastTick)/35;
lastTick = currentTime;
if ((player1.isWPressed && player1.isSPressed) ||
(!player1.isWPressed && !player1.isSPressed)) {
player1.vX = 0;
player1.vY = 0;
} else if (player1.isWPressed) {
//System.out.println("Up");
player1.vX = Math.cos(player1.RotationDegree*Math.PI/180)*player1.speed * timeDelta;
player1.vY = Math.sin(player1.RotationDegree*Math.PI/180)*player1.speed * timeDelta;
//player1.posY -= player1.speed*timeDelta;
// MOVE FORWARD
} else if (player1.isSPressed) {
//System.out.println("Down");
player1.vX = -Math.cos(player1.RotationDegree*Math.PI/180)*player1.speed * timeDelta;
player1.vY = -Math.sin(player1.RotationDegree*Math.PI/180)*player1.speed * timeDelta;
//player1.posY += player1.speed*timeDelta;
// MOVE BACKWARD;
}
if (player1.isAPressed) {
//System.out.println("Left");
player1.RotationDegree -= player1.rotateSpeed* timeDelta;
// MOVE BACKWARD;
}
if (player1.isDPressed) {
//System.out.parintln("Right");
player1.RotationDegree += player1.rotateSpeed* timeDelta;
// MOVE BACKWARD;
}
if(player1.RotationDegree > 360) {
player1.RotationDegree -= 360;
}
else if(player1.RotationDegree < 0) {
player1.RotationDegree += 360;
}
player1.move(null, player1.vX, player1.vY);
if ((player2.isUpPressed && player2.isDownPressed) ||
(!player2.isUpPressed && !player2.isDownPressed)) {
player2.vX = 0;
player2.vY = 0; }
else if (player2.isUpPressed) {
//System.out.println("Up");
player2.vX = Math.cos(player2.RotationDegree*Math.PI/180)*player2.speed* timeDelta;
player2.vY = Math.sin(player2.RotationDegree*Math.PI/180)*player2.speed* timeDelta;
//player2.posY -= player2.speed*timeDelta;
// MOVE FORWARD
}
else if (player2.isDownPressed) {
//System.out.println("Down");
player2.vX = -Math.cos(player2.RotationDegree*Math.PI/180)*player2.speed * timeDelta;
player2.vY = -Math.sin(player2.RotationDegree*Math.PI/180)*player2.speed * timeDelta;
//player2.posY += player2.speed*timeDelta;
// MOVE BACKWARD;
}
if (player2.isLeftPressed) {
//System.out.println("Left");
player2.RotationDegree -= player2.rotateSpeed*timeDelta;
// MOVE BACKWARD;
}
if (player2.isRightPressed) {
//System.out.println("Right");
player2.RotationDegree += player2.rotateSpeed*timeDelta;
// MOVE BACKWARD;
}
if(player2.RotationDegree > 360) {
player2.RotationDegree -= 360;
}
else if(player2.RotationDegree < 0) {
player2.RotationDegree += 360;
}
player2.move(null, player2.vX, player2.vY);
player1.currentReload -= timeDelta;
if (player1.currentReload <= 0)
;
{
if (player1.isSpacePressed) {
// create bullet and fire
BasicWeapon newBullet = new BasicWeapon(arenaWidth, arenaHeight, player1.posX + player1.radius, player1.posY + player1.radius, player1);
player1.bullets.add(newBullet);
player1.currentReload = player1.MaxReload;
}
}
ArrayList<PlayerProjectile> bulletsToDelete1 = new ArrayList<PlayerProjectile>();
for (int i = 0; i < player1.bullets.size(); i++) {
PlayerProjectile bulletToChange = player1.bullets.get(i);
bulletsToDelete1.add(bulletToChange);
}
player1.bullets.removeAll(bulletsToDelete1);
// TODO: Update the game state each frame. This can be broken into the following steps:
// Step 1: Handle the keys pressed during the last frame by both players and calculate their resulting velocities/orientations.
// Step 2: Move the players and detect/handle player/player collisions and player/terrain collisions.
// Step 3: Decide whether a bullet should be fired for each player and create new bullet(s) if so. Also, handle reload mechanics.
// Step 4: Move all bullets via their calculated velocities (up to bullet range). Handle bullet/player & bullet/terrain collisions.
// Step 5: Decide whether the game has ended. If so, stop the timer and print a message to the screen indicating who's the winner.
// Note: If you implement other features (eg. weapon swapping, damage counters...etc.), you might also need to add more steps above.
// Update the display: this function calls paintComponent as part of its execution.
repaint(); }
// TODO: Draw all of the objects in your game.
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
//AffineTransform at = AffineTransoform.getTranslateInstance();
player1.draw(g);
player2.draw(g);
}
// Returns the dimensions of the Arena (for properly resizing the JPanel on the screen).
#Override
public Dimension getPreferredSize() {
return new Dimension(arenaWidth, arenaHeight);
}
}
THIS IS OUR PLAYER CLASS
// TODO: Feel free to import any other libraries that you need.
//import java.*;
import java.awt.*;
import java.util.ArrayList;
//import javax.swing.*;
//import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Player extends GameObject {
public static void main(String []args){
}
// TODO: Set these values as you see fit. However, which variables should you not set as negative numbers? Which variables should you
// not set as zero? Which v'ariables should you not set as a very large positive number? Why?
public static final int INIT_SIZE = 30;
public static final int INIT_MASS = 0;
public static final int INIT_DAMAGE = 0;
public static final double INIT_SPEED = 10;
public static final double INIT_ROTATE_SPEED = 10;
public static final int INIT_HEALTH = 100;
// Member variables of the player that you can change over the course of the game.
public int damage = INIT_DAMAGE;
public double speed = INIT_SPEED;
public double rotateSpeed = INIT_ROTATE_SPEED;
public int health = INIT_HEALTH;
public double orientation = 0;
public int id;
// TODO: You may need to set up extra variables to store the projectiles fired by this player, the reload status/time of this player,
// the key press/release status of this player, and any other player-related features you decide to implement. Make sure to update the
// constructor appropriately as well!
long currentReload = 0;
long MaxReload = BasicWeapon.INIT_RELOAD;
ArrayList<PlayerProjectile> bullets = new ArrayList<PlayerProjectile>();
double RotationDegree = 0;
boolean isWPressed = false;
boolean isSPressed = false;
boolean isAPressed = false;
boolean isDPressed = false;
boolean isSpacePressed = false;
boolean isEnterPressed = false;
boolean isUpPressed = false;
boolean isDownPressed = false;
boolean isLeftPressed = false;
boolean isRightPressed = false;
boolean isLeftCLickPressed = false;
// Constructor that calls the super (ie. parent) class's constructor and instantiates any other player-specific variables.
public Player(int arenaWidth, int arenaHeight, double startPosX, double startPosY, int id) {
super(arenaWidth, arenaHeight, 0, 0, startPosX, startPosY, INIT_SIZE, INIT_SIZE, INIT_MASS);
this.id = id;
}
// TODO: This function should move the player and handle any player-terrain interactions.
#Override
public boolean move(Map map, double translateX, double translateY) {
posX += translateX;
posY += translateY;
xClip();
yClip();
return false;
}
//UPDATE PLAYER POSTION HERE
//}
public void draw(Graphics g) {
// TODO: Draw the barrel(s) for the player here
double xChords[] = new double[4];
double YChords[] = new double[4];
xChords[0] = posX + 0.6 * width;
xChords[1] = posX + 0.6 * width;
xChords[2] = posX + 1.5 * width;
xChords[3] = posX + 1.5 * width;
YChords[0] = posY + 0.4 * height;
YChords[1] = posY + 0.5 * height;
YChords[2] = posY + 0.5 * height;
YChords[3] = posY + 0.4 * height;
double[] point0 = rotatePoint(posX + 0.5 * width, posY + 0.5 * height, RotationDegree, xChords[0], YChords[0]);
double[] point1 = rotatePoint(posX + 0.5 * width, posY + 0.5 * height, RotationDegree, xChords[1], YChords[1]);
double[] point2 = rotatePoint(posX + 0.5 * width, posY + 0.5 * height, RotationDegree, xChords[2], YChords[2]);
double[] point3 = rotatePoint(posX + 0.5 * width, posY + 0.5 * height, RotationDegree, xChords[3], YChords[3]);
int rotatedPointsX[] = new int[4];
int rotatedPointsY[] = new int[4];
rotatedPointsX[0] = (int)Math.round(point0[0]);
rotatedPointsX[1] = (int)Math.round(point1[0]);
rotatedPointsX[2] = (int)Math.round(point2[0]);
rotatedPointsX[3] = (int)Math.round(point3[0]);
rotatedPointsY[0] = (int)Math.round(point0[1]);
rotatedPointsY[1] = (int)Math.round(point1[1]);
rotatedPointsY[2] = (int)Math.round(point2[1]);
rotatedPointsY[3] = (int)Math.round(point3[1]);
g.drawPolygon(rotatedPointsX, rotatedPointsY, 4);
g.setColor(Color.BLACK);
g.fillPolygon(rotatedPointsX, rotatedPointsY, 4);
if (id == 1) {
g.setColor(new Color(255, 215, 0));
} else if (id == 2) {
g.setColor(Color.RED);
}
// Body
g.fillOval((int) posX, (int) posY, width, height);
g.setColor(Color.BLACK);
g.drawOval((int) posX, (int) posY, width, height);
// TODO: Draw the health bar for the player here.
}
}
Only the button appears, and player1 and player2 don't.
I think what you are trying to achieve is something like :
public static void main(String []args){
JButton button= new JButton("Reset");
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.add(button);
frame.add(panel, BorderLayout.NORTH);
frame.add(new Arena(), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
For future question please do not post so much code. See: don't just copy in your entire program!

Block Collision Code Troubleshooting

I'm finishing up a coding assignment that requires me to set an array of blocks in motion, bouncing off of the window and each other, but unfortunately I'm totally lost as to where to go next. Any help would be appreciated (still getting the hang of coding, so I'm looking for all the help possible). Specifically, I need the rectangles to appear first, and then troubleshoot movement, and finally help with collision detection.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Driver implements ActionListener
{
private JFrame window;
private Timer timer;
private ChaseBlock[] blocks = new ChaseBlock[15];
// constants for graphics
private final int windowSize = 500;
private final int blockSize = 20;
/**
* Simple initiating main().
*
* #param args Not used.
*/
public static void main( String[] args )
{
Driver d = new Driver();
d.createWindow();
}
/**
* Set up the basic graphical objects.
*/
private void createWindow()
{
// create the window
window = new JFrame( "The Great Chase" );
window.setVisible( true );
window.setLayout( null );
window.getContentPane().setBackground( Color.white );
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setLocation( 50, 50 );
window.setSize(
windowSize + window.getInsets().left + window.getInsets().right,
windowSize + window.getInsets().top + window.getInsets().bottom );
window.setResizable( false );
window.repaint();
timer = new Timer(10, this);
timer.start();
blocks[1].setBackground(Color.blue);
window.repaint();
addBlocks();
}
private void addBlocks() {
for (int i = 0; i < blocks.length; i++) {
int x = i * blockSize + 10 * (i +1);
blocks[i] = new ChaseBlock(x, windowSize / x - blockSize / 2, blockSize, windowSize);
}
for (int i = 0; i < blocks.length; i++) {
window.add(blocks[i]);
}
}
private void animate() {
for (int i = 0; i < blocks.length; i++) {
blocks[i].move();
for (int b1 = 0; b1 < blocks.length; b1++)
for (int b2 = 0; b2 < blocks.length; b2++)
if (b1 != b2)
blocks[b1].checkCollision(blocks[b2]);
}
}
public void actionPerformed (ActionEvent e) {
animate();
}
}
This is the driver class that we use, with a Rectangle class also. Near to the bottom, the goal is to add the rectangles and make them move. My problem here is that the rectangles do not show up whatsoever or move.
import java.awt.Color;
public class ChaseBlock extends Rectangle {
private int dX, dY;
private int windowWidth = 500;
private int windowHeight = 500;
public ChaseBlock(int x, int y, int w, int h) {
super(x, y, w, h );
if (Math.random() < 0.5) {
dX = -1;
dY = -1;
setBackground(Color.green);
} else
dX = 1;
dY = 1;
setBackground(Color.blue);
}
public void move() {
setLocation(getX(), getY() + 5);
if(getX() < 0 || getY() + getWidth() >= windowWidth) {
dX = dX * -1;
}
if (getY() < 0 || getY() + getHeight() >= windowHeight) {
dY = dY * -1;
}
}
public void checkCollision(ChaseBlock blocks) {
boolean up = false;
boolean down = false;
boolean left = false;
boolean right = false;
boolean hit = false;
}
}
This is my class to define the movement and everything else. My problems here are that I need to use the checkCollision method to manage the collisions between the blocks themselves and the window, and in addition set colors for all the blocks.
First of all, the first condition for collision checks with window edges is incorrect. It should be
if(getX() < 0 || getX() + getWidth() >= windowWidth)
To detect collisions between two Axis Aligned Bounding Boxes (which is what you have as Rectangles), you just have to check whether the first's min and max points (x,y and x+h, y+h) are inside the second as follows:
if(a.x + a.h < b.x or a.x > b.x + b.h) return false;
if(a.y + a.h < b.y or a.y > b.y + b.h) return false;
return true;
If you want to find out which face (or which direction) the collision takes place on, you'll have to use the more long and slightly more complicated Separating Axis Theorem.

Program is not checking the second condition

When I run my game it works fine on the bottom portion of the object collision but, it will not distguish that one and the other if. It is not reading the second one. I took a picture and you can see the (x, y) coords and it meets the condition but, I can still move.
IMG: http://i.stack.imgur.com/PjcHB.png
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
int playerWeight = 115;
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(){
System.out.println(x + ", " + y);
x = x + xa;
y = y + ya;
if(x + xa < 0) // Left Bounds
xa = 0;
if (x + xa > game.getWidth() - WIDTH) // Right Bounds
xa = 0;
if (y + ya < 0) // Top Bounds
ya = 0;
if(y + ya > game.getHeight() - WIDTH)
ya = 0;
if (collision()) // Tile bounds
y = y - 4;
if (collision2()){
if(x > 370 && x < 531 && y < 286){ // Check for 3 values
y = y + 4;
System.out.println("-x");
/*
* Use the gravity() method to determine player fall rate
*/
}
else if(x > 370 && x < 531 && y > 223){ // Check for 3 values
ya = 0;
System.out.println("+x");
/*
* Use the gravity() method to determine player fall rate
*/
}
}
}
// Method to find where player is located
public int[] Playerposition(){
x = 1;
y = 300;
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());
}
}
Change:
else if(x > 370 && x < 531 && y > 223){ // Check for 3 values
ya = 0;
System.out.println("+x");
/*
* Use the gravity() method to determine player fall rate
*/
}
to:
if(x > 370 && x < 531 && y < 286){ // Check for 3 values
y = y + 4;
System.out.println("-x");
/*
* Use the gravity() method to determine player fall rate
*/
}
if(x > 370 && x < 531 && y > 223){ // Check for 3 values
ya = 0;
System.out.println("+x");
/*
* Use the gravity() method to determine player fall rate
*/
}
Reason being is that the keyword "else" will only execute code within it's block if the above "if" statement's condition fails/requirements are not met. For example:
boolean anInteger = 9;
if (anInteger == 1999) {
// ...
} else {
// If 'anInteger' is not equal to 9, then execute the following code (within this block)
// ...
}

Using WASD and arrow keys simultaneously

I am developing a two player game in Processing (running on Java). One user will control his character using the WASD keys and the other will control movement using the arrow keys. The problem I am having is that using keyPressed negates WASD when arrows are pressed and vice versa. I have been messing around with it for a really long time. Does anyone know a work around or notice something that I am doing wrong?
//global variables
int wide = 600; //canvas width
int tall = 600; //canvas height
int s = 50; //player size
float speed = 2.5; //player movement speed
//colors
int redColor = #CB4646; //player 1 color
int blueColor = #4652CB; //player 2 color
int backgroundColor = #DBE3B3; //background color
float player1X = 600/3-s; //HOW COME width/3 DOESN'T WORK??????????
float player2X = 600*2/3;
float playerY = 600/2-(s/2);
//players
Player player1 = new Player(player1X, playerY, s, speed, "wasd", redColor); //player 1
Player player2 = new Player(player2X, playerY, s, speed, "arrows", blueColor); //player 2
//setup
void setup(){
background(backgroundColor);
size(wide, tall);
smooth();
println(player2.controls);
}
//draw
void draw(){
background(backgroundColor);
player1.usePlayer();
player2.usePlayer();
}
class Player{
//class variables
float x; // x position
float y; // y position
int s; //size
float speed; //speed
String controls; //controls
int colors; //player color
char keyControls [] = new char [4];
//construct
Player(float tempX, float tempY, int tempS , float tempSpeed, String tempControls, int tempColors){
x = tempX;
y = tempY;
s = tempS;
speed = tempSpeed;
controls = tempControls;
colors = tempColors;
}
void usePlayer(){
// draw player
fill(colors);
rect(x, y, s, s);
//move player
keyPressed();
//wraparound
boundaries();
}
void keyPressed(){
//sets controls for wasd
if(controls == "wasd"){
if(key == 'w' || key == 'W'){
y -= speed; //move forwards
}
if(key == 's' || key == 'S'){
y += speed; //move backwards
}
if(key == 'd' || key == 'D'){
x += speed; //move right
}
if(key == 'a' || key == 'A'){
x -= speed; //move left
}
}
//sets controls for arrows
if(controls == "arrows"){
if(key == CODED){
if(keyCode == UP){
y -= speed; //move forwards
}
if(keyCode == DOWN){
y += speed; //move backwards
}
if(keyCode == RIGHT){
x += speed; //move right
}
if(keyCode == LEFT){
x -= speed; //move left
}
}
}
}
//pacman style wraparound
void boundaries(){
if(x == width) x = 2;
if(y == height) y = 2;
if(x == 0) x = width-s;
if(y == 0) y = height-s;
}
}
Track your keys independently, don't rely on the event globals.
boolean[] keys = new int[255];
void keyPressed() {
keys[keyCode] = true;
}
void keyReleased() {
keys[keyCode] = false;
}
void draw() {
updatePlayers();
drawStuff();
}
void updatePlayers() {
if(keys[LEFT]) { p1.move(-1,0); }
if(keys[RIGHT]) { p1.move(1,0); }
if(keys[UP]) { p1.move(0,-1); }
if(keys[DOWN]) { p1.move(0,1); }
if(keys['a']) { p2.move(-1,0); }
if(keys['d']) { p2.move(1,0); }
if(keys['w']) { p2.move(0,-1); }
if(keys['s']) { p2.move(0,1); }
}
Note this has to be a series of if statements, because you want to handle all pressed keys. If someone's holding left and right, p1 will move left, and right.
Also note that this example code doesn't filter for the higher-than-255 codes you get for special keys, so you probably want to put an "if(keyCode>255) return" at the start of the event handlers.
Is key a global variable? I don't see it getting passed to the Player. If it's global, then it can only hold one key at a time, which precludes controlling two players at once.
Here is my Arduino/Processing code that I used to handle simultaneous key presses (to move diagonally). It fixes the issue with boolean[] cannot be cast to int[] error as shown by #Brannon and uses keyCodes instead of key.
import processing.serial.*;
​
boolean[] keys = new boolean[255];
Serial port;
​
void setup() {
port = new Serial(this, Serial.list()[1], 9600);
​
}
​
void draw() {
// loop through boolean array and see which ones (index = keyCode)
// are true, then write to them.
for(int i = 0; i < 255; i++) {
if(keys[i]) {
if (i == 87) { port.write('w'); }
if (i == 65) { port.write('a'); }
if (i == 83) { port.write('s'); }
if (i == 68) { port.write('d'); }
}
}
}
​
void keyPressed() {
keys[keyCode] = true;
}
​
void keyReleased() {
keys[keyCode] = false;
}

How can I simplify this code? (Chess game obstruction testing)

I'm making a chess game in Java, and testing to make sure there are no pieces blocking the path of the piece being moved. The piece moves from (srcX,srcY) to (dstX,dstY).
I've written this code which checks if there are any obstructions for a rook:
if(dstY == srcY) {
// No change on Y axis, so moving east or west
if(dstX > srcX) {
// Moving east
// Test every cell the piece will pass over
for(int x = srcX+1; x < dstX; x++) {
// Is the cell set?
if(isPiece(x, srcY)) {
return true;
}
}
} else {
// Must be moving west
// Test every cell the piece will pass over
for(int x = srcX-1; x > dstX; x--) {
// Is the cell set?
if(isPiece(x, srcY)) {
return true;
}
}
}
} else if(dstX == srcX) {
// No change on X axis, so moving north or south
if(dstY > srcY) {
// Moving north
// Test every cell the piece will pass over
for(int y = srcY+1; y < dstY; y++) {
// Is the cell set?
if(isPiece(srcX, y)) {
return true;
}
}
} else {
// Must be moving south
// Test every cell the piece will pass over
for(int y = srcY-1; y > dstY; y--) {
// Is the cell set?
if(isPiece(srcX, y)) {
return true;
}
}
}
}
but it's a bit big and I'm sure it can be simplied.. any ideas?
ps, this is ONLY obstruction testing. I've already validated everything else.
Once you've tested for direction, you can set dx, dy values (e.g. dx=1, dy=0 for east). Then you can have a single for loop for all cases and just increment x and y by dx and dy respectively at each iteration.
You can then simplify the direction checking into the following:
if dstY == srcY: dy = 0
else: dy = (dstY - srcY) / abs(dstY - srcY)
if dstX == srcX: dx = 0
else: dx = (dstX - srcX) / abs(dstX - srcX)
Code:
int dx, dy;
if (dstY == srcY) dy = 0;
else dy = (dstY - srcY) / Math.abs(dstY - srcY);
if (dstX == srcX) dx = 0;
else dx = (dstX - srcX) / Math.abs(dstX - srcX);
while (srcX != dstX || srcY != dstY) {
srcX += dx; srcY += dy;
if (isPiece(srcX, srcY))
return true;
}
return false;
Also beware that this code (and yours) will fail if the move is not horizontal, vertical or diagonal.
You could do something along these lines (untested as I don't have a compiler to hand):
int dx = 0;
int dy = 0;
if (dstX != srcX) {
dx = (dstX > srcX) ? 1 : -1;
} else if (dstY != srcY) {
dy = (dstY > srcY) ? 1 : -1;
}
int x = srcX + dx;
int y = srcY + dy;
while (x != dstX || y != dstY) {
if (isPiece(x, y)) return true;
x += dx;
y += dy;
}
First, write tests. Lots and lots of tests. That way you can be confident that you're simplifying without changing the meaning of the code.
Refactoring without unit tests is like walking a high wire without a safety net.
Nearly the same, but with for loops:
// move along x axis
for (int x = 1; x < Math.abs(srcX - dstX); x++) {
int curX = (srcX - dstX) < 0 ? srcX - x : srcX + x;
if (isPiece(curX, srcY))
return true;
}
// move along y axis
for (int y = 1; y <= Math.abs(srcY - dstY); y++) {
int curY = (srcY - dstY) < 0 ? srcY - y : srcY + y;
if (isPiece(srcX, curY))
return true;
}
My soultion would be: introduce a direction class, and then do the check in this style:
isBlocked(startPossition, direction, numberOfFields)
I have done a little example, using 3 Classes.
Direction - an enum to represent the 8 directions (2 horizontal, 2 vertical, 4 diagonal)
Position - the x and y value of an position
LinarMove - represent one linear Move(startPossition, direction, numberOfFields) and contains the isBlockedMethod
The Enum:
public enum Direction {
UP(0, 1),
DOWN(0, -1),
LEFT(1, 0),
RIGHT(-1, 0),
UP_LEFT(UP, LEFT),
UP_RIGTH(UP, RIGHT),
DOWN_LEFT(DOWN, LEFT),
DOWN_RIGHT(
DOWN, RIGHT);
private final int incrementX;
private final int incrementY;
private Direction(int incrementX, int incrementY) {
this.incrementX = incrementX;
this.incrementY = incrementY;
}
private Direction(Direction sub1, Direction sub2) {
this.incrementX = sub1.incrementX + sub2.incrementX;
this.incrementY = sub1.incrementY + sub2.incrementY;
}
public Position oneField(Position start) {
return new Position(start.getX() + this.incrementX, start.getY()
+ this.incrementY);
}
}
The purpuse of second constructor is only that it alowes to write the diagonal moves in a more readable way.
public class Position {
private final int x;
private final int y;
public Position(int x, int y) {
super();
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
#Override
public String toString() {
return "x:" + x + ", y:" + y;
}
}
The Move contains the isBlocked Method -- you can see how small it get, and how readable it become. At least there is no single direction related if statement left.
The name LinareMove sugget that there is possible an other kind of move for the knight.
public class LinearMove {
private final Position start;
private final Direction direction;
/** Length of the move. */
private final int numberOfFields;
public LinearMove(Position start, Direction direction, int numberOfFields) {
super();
this.start = start;
this.direction = direction;
this.numberOfFields = numberOfFields;
}
boolean isBlocked() {
Position pos = this.start;
for (int i = 0; i < (this.numberOfFields - 1); i++) {
pos = this.direction.oneField(pos);
if (isPiece(pos)) {
return true;
}
}
return false;
}
boolean isPiece(Position pos) {
//Dummy;
System.out.println(pos);
return false;
}
}
And this is how it works:
public static void main(String[] args) {
new LinearMove(new Position(1, 1), Direction.RIGHT, 3).isBlocked();
}
You maybe noticed, that the knights move is some kind of probem. With this soultion you could model it in two ways:
- 4 special Directions
- an other kind of move class (this is the more cleaner way, because you could always return true, in the isBockedMethod)

Categories

Resources