Reputation: 21
Ok, so I am working on a Java2D game, and I've gotten close to getting the collisions right. I have a player class, a block class, and xvel, yvel, etc. for the player. The y collisions work right, but when I try to get X collisions to work, it just is completely messed up. Also, even though the Y collisions work right, the player is just bobbing up and down very slightly over the block. I have setup a hitbox for my player for collisions. What am I doing wrong? Here is my code (I know it is not the best):
EDIT: Ok, so I have managed to stop the player from bobbing up and down and I have changed the way the collisions are checked. I removed the player's hitbox and am just using the .png sprite, and I resized it so that is working well. So, the problem is, the "if (player.xvel < 0) { }" and the "if (player.xvel > 0) { }" are still being executed even though the player hasn't run into the block horizontally. How can I make it so the code is executed when the player runs/walks into the block horizontally?
Block.java:
import java.awt.*;
import javax.imageio.ImageIO;
import java.io.*;
import java.awt.Rectangle;
class Block {
Image block_sprite;
Image dirt;
int x;
int y;
int height;
int width;
int i = 0;
public Block(int X, int Y, int Width, int Height, int block_type) {
x = X * 32;
y = Y * 32;
width = Width;
height = Height;
try {
dirt = ImageIO.read(new File("img/dirt.png"));
} catch(IOException e) {
e.printStackTrace();
}
if (block_type == 1) {
block_sprite = dirt;
}
}
void draw(Graphics g) {
g.fillRect(x + Display.graphics_panel.WorldX, y, width, height);
}
void collide(Player player) {
Rectangle player_hb = new Rectangle((int) player.x, (int) player.y, (int) player.width, (int) player.height);
Rectangle block_hb = new Rectangle(x + Display.graphics_panel.WorldX, y, width, height);
if (player_hb.intersects(block_hb)) {
Rectangle intersection = (Rectangle) player_hb.createIntersection(block_hb);
if (player.yvel > 0) {
player.jumping = false;
player.y -= intersection.getHeight();
}
if (player.yvel < 0) {
player.y += intersection.getHeight();
}
if (player.xvel > 0) {
System.out.println("You shouldn't see this unless you run into a block!" + i++);
}
if (player.xvel < 0) {
System.out.println("You shouldn't see this unless you run into a block!" + i--);
}
}
}
}
Player.java:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.imageio.*;
import java.io.*;
class Player extends Entity {
float x = Display.WINDOW_WIDTH / 2;
float y = 430;
float width = 48;
float height = 103;
float xvel = 0;
float yvel = 0;
float gravity = 0.4f;
float xvel_max = 2;
float yvel_max = 8;
float xvel_min = -2;
float yvel_min = -8;
String dir = "left";
float sword_offsetX = 49;
float sword_offsetY = 32;
Boolean jumping = false;
Image sprite;
Image sword_sprite;
Image sprite_r;
Image sprite_r_a1;
Image sprite_r_a2;
Image sprite_l;
Image sprite_l_a1;
Image sprite_l_a2;
Image ssprite_r;
Image ssprite_l;
Player() {
try {
sprite_r = ImageIO.read(new File("img/player_r.png"));
sprite_r_a1 = ImageIO.read(new File("img/player_r_a1.png"));
sprite_r_a2 = ImageIO.read(new File("img/player_r_a2.png"));
sprite_l = ImageIO.read(new File("img/player_l.png"));
sprite_l_a1 = ImageIO.read(new File("img/player_l_a1.png"));
sprite_l_a2 = ImageIO.read(new File("img/player_l_a2.png"));
ssprite_r = ImageIO.read(new File("img/sword_r.png"));
ssprite_l = ImageIO.read(new File("img/sword_l.png"));
if (dir == "left") {
sprite = sprite_r;
}
sword_sprite = ssprite_r;
} catch(IOException e) {
e.printStackTrace();
}
}
Graphics2D g2d;
void draw(Graphics g) {
g2d = (Graphics2D) g;
g2d.drawImage(sprite, (int) x, (int) y, null);
g2d.drawImage(sword_sprite, (int) x + (int) sword_offsetX, (int) y + (int) sword_offsetY, null);
}
void update() {
jumping = true;
yvel += gravity;
Display.graphics_panel.WorldX += xvel;
y += yvel;
for (int i = 0; i < Display.graphics_panel.world.blocks.length; i++) {
Display.graphics_panel.world.blocks[i].collide(this);
}
if (Display.keys[65] == 65 || Display.keys[68] == 68) {
if (dir == "left") {
sprite = sprite_r;
sword_sprite = ssprite_r;
sword_offsetX = 49;
} else if (dir == "right") {
sprite = sprite_l;
sword_sprite = ssprite_l;
sword_offsetX = -18;
}
if (xvel < 0) {
if (Display.graphics_panel.frameCount / 10 % 2 == 0) {
sprite = sprite_r_a1;
} else {
sprite = sprite_r_a2;
}
}
if (xvel > 0) {
if (Display.graphics_panel.frameCount / 10 % 2 == 0) {
sprite = sprite_l_a1;
} else {
sprite = sprite_l_a2;
}
}
}
if (xvel == 0 && dir == "left") {
sprite = sprite_r;
}
if (xvel == 0 && dir == "right") {
sprite = sprite_l;
}
if (Display.keys[65] == 65) {
xvel += 0.25;
dir = "right";
} else if (Display.keys[68] == 68) {
xvel -= 0.25;
dir = "left";
} else {
xvel = 0;
}
if (Display.keys[87] == 87 && jumping == false) {
jumping = true;
yvel = -8;
}
if (xvel > xvel_max) {
xvel = xvel_max;
} else if (xvel < xvel_min) {
xvel = xvel_min;
}
if (yvel > yvel_max) {
yvel = yvel_max;
} else if (yvel < yvel_min) {
yvel = yvel_min;
}
}
}
Upvotes: 0
Views: 272
Reputation: 11171
If you only want to check collisions between rects, here is a rectangle class :
class Rect {
public Point[] points=new Point[4];
public Point start;
public Point dim;
public Rect(double x,double y,double w,double h) {
start=new Point((int)x,(int)y);
dim=new Point((int)w,(int)h);
points[0]=new Point((int)x,(int)y);
points[1]=new Point((int)x+(int)w,(int)y);
points[2]=new Point((int)x+(int)w,(int)y+(int)h);
points[3]=new Point((int)y,(int)y+(int)h);
}
public boolean collidepoint(Point p) {
if (p.x >= start.x && p.x <= start.x+dim.x && p.y >= start.y && p.y <= start.y+dim.y) {
return true;
}
else {
return false;
}
}
public boolean colliderect(Rect rect2) {
boolean retvar=false;
for (Point p:rect2.points) {
if (collidepoint(p)==true) {
retvar=true;
}
}
if (retvar==false) {
for (Point p:points) {
if (rect2.collidepoint(p)==true) {
retvar=true;
}
}
}
if (retvar==false) {
if (rect2.start.y >= start.y && rect2.start.y <= start.y+dim.y || rect2.start.y+rect2.dim.y >= start.y && rect2.start.y+rect2.dim.y <= start.y+dim.y) {
if (rect2.start.x >= start.x && rect2.start.x <= start.x+dim.x || rect2.start.x+rect2.dim.x >= start.x && rect2.start.x+rect2.dim.x <= start.x+dim.x) {
retvar=true;
}
}
}
if (retvar==false) {
if (start.y >= rect2.start.y && start.y <= rect2.start.y+rect2.dim.y || start.y+dim.y >= rect2.start.y && start.y+dim.y <= rect2.start.y+rect2.dim.y) {
if (start.x >= rect2.start.x && start.x <= rect2.start.x+rect2.dim.x || start.x+dim.x >= rect2.start.x && start.x+dim.x <= rect2.start.x+rect2.dim.x) {
retvar=true;
}
}
}
return retvar;
}
}
Hope it helps you.
Upvotes: 0
Reputation: 11171
So, I actually made me a little platformer with hitbox. The part that you probably need is to know collisions before they happen. Probably this example from my game helps you.
level.material=array of points from the level
w=width of hitbox
h=height of hitbox
x=upper left x
y=upper left y
onground=false;
double maxy=1000;
for (Point p:level.material) {
if (p.x >= (int)x && p.x <= (int)x+w) { //Is the player over the point ? (is the point between player x and player x+w)
if (p.y >= (int)(y+h) && (int)(y+h+yvel) >= p.y && p.y-h-1 < maxy) { //Would he go through a platform if the yvel would be added ? And is this the earliest point where this happens ?
maxy=p.y-p2.y-1d; //Set new earliest point
onground=true; //He is onground
}
}
}
}
if (onground==true) {
y=maxy; //Dont fall through the floor ! Stop at the earliest point !
yvel=0; //There isnt any velocity downwards anymore.
xvel=xvel-xvel/4; //Friction
}
You should be able to easily copy this for all other alignments(left,right,under). This is "onground"(over). So the player is able to stand on platforms.
Upvotes: 1