So, the most annoying thing is happening to me!
my Collision detection detects the floor so that I do not fall through but the ‘Wall/Collide’ detection does not work.
I am doing a 2D Skyview y - z game and its my first ever committed game.
So here is my scenario.
I have my Character, when he leaves the Bounds of the screen he shrinks, as there is no tile there. I want that,
But what I don’t want is to actually be able to LEAVE the spawn/screen bounds, Here is my class containing the detection.
Everything is working BUT the wall detection, if necessary I shall post a video.
Please don’t judge, I am a noob.
(Sometimes it throws a out of bounds exception)
Character.java
package net.tehepicford.LostSouls;
import java.awt.Graphics;
import java.awt.Point;
public class Character extends DoubleRectangle {
public double fallingSpeed = 1.3;
public double xmovementSpeed = 1;
public double ymovementSpeed = 1;
public static boolean canMove = false;
public Character(int width, int height) {
setBounds((Component.pixel.width / 2) - (width / 2), (Component.pixel.height / 2) - (height / 2), width, height);
}
private int f = (int) fallingSpeed;
public void tick() {
if(Component.isMovingx || Component.isMovingy) {
if(Component.dirx == xmovementSpeed || Component.diry == ymovementSpeed) {
canMove = isCollidingWithBlock(new Point(), new Point());
} else if(Component.dirx == -xmovementSpeed || Component.diry == -ymovementSpeed) {
canMove = isCollidingWithBlock(new Point((int) x, (int) y), new Point((int) x, (int) (y + height)));
}
if(!isCollidingWithBlock(new Point((int) x, (int) (y + (int) height)), new Point((int) (x + (int) width), (int) (y + (int) height)))) {
if(!canMove) {
if (f <= 19) f++;
}
}
}
if(Component.isMovingx) {
x += Component.dirx;
}
if(Component.isMovingy) {
y += Component.diry;
}
}
public boolean isCollidingWithBlock(Point pt1, Point pt2) {
for(int x = (int) (this.x/Tile.tileSize); x < (int) (this.x/Tile.tileSize + 3); x++)
for(int y = (int) (this.y/Tile.tileSize); y < (int) (this.y/Tile.tileSize + 3); y++){
if( x >= 0 && y >= 0 && x < Component.level.block.length && y < Component.level.block[0].length) {
if(Component.level.block[x][y].id != Tile.air) {
if(Component.level.block[x][y].contains(pt1) || Component.level.block[x][y].contains(pt2)) { x = 20;
return true;
}
}
}
}
return false;
}
public void render(Graphics g) {
g.drawImage(Tile.tileset_terrain, (int) x, (int) y, (int) (x + width - f), (int) (y + height -f), Tile.character[0] * Tile.tileSize, Tile.character[1] * Tile.tileSize, Tile.character[0] * Tile.tileSize + (int) width, Tile.character[1] * Tile.tileSize + (int) height, null);
}
}
And level.java (As I am not sure if I declared a wall or anything, please do tell me how if that is the case)
package net.tehepicford.LostSouls;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Level {
public Block[][] block = new Block[50][50];
public Level() {
for(int x = 0; x < block.length; x++) {
for(int y = 0; y < block[0].length; y++) {
block[x][y] = new Block(new Rectangle(x * Tile.tileSize, y * Tile.tileSize, Tile.tileSize, Tile.tileSize), Tile.earth);
}
}
generateLevel();
}
public void generateLevel() {
for(int x = 0; x < block.length; x++) {
for(int y = 0; y < block[0].length; y++) {
if(x == 0 || y == 0|| x == block.length - 1 || y == block[0].length - 1) {
block[x][y].id = Tile.earth;
}
}
}
}
public void tick() {
}
public void render(Graphics g) {
for(int x = 0; x < block.length; x++) {
for(int y = 0; y < block[0].length; y++) {
block[x][y].render(g);
}
}
}
}
If any more info is necessary, that I am sure is not just tell me