Hello
I want to move my tiledMap depending on my characters position. But when I simply set the maps position to the characters position the collision detection goes wild. Any solutions?’
What I tried was map.draw(position.x,0);
Ptmx map;
PImage image;
boolean left, right, jump, crouch;
PVector position, velocity, acceleration, origin;
void setup() {
size(1200, 480);
map = new Ptmx(this, "map.tmx");
image = loadImage("MarioStanding.png");
position = new PVector(0, 0);
velocity = new PVector(0, 0);
acceleration = new PVector(0.32, 0.48);
}
void draw() {
if (!alive(position)) {
if (!collision_detected(position, "down")) {
velocity.add(0, acceleration.y);
}
position.add(0, velocity.y);
if (collision_detected(position, "down")) {
image = loadImage("MarioStanding.png");
velocity.set(velocity.x, 0);
}
if (collision_detected(position, "up")) {
velocity.set(velocity.x, 4);
}
if (jump == true && collision_detected(position, "down") && !collision_detected(position, "up")) {
image = loadImage("MarioMoving2.png");
velocity.set(velocity.x, -10);
}
if (left == true && !collision_detected(position, "left")) {
if (velocity.x < 4) {
velocity.add(acceleration.x, 0);
}
position.sub(velocity.x, 0);
}
if (crouch == true && !collision_detected(position, "down")) {
position.add(0, velocity.y);
}
if (right == true && !collision_detected(position, "right")) {
if (velocity.x < 4) {
velocity.add(acceleration.x, 0);
}
position.add(velocity.x, 0);
}
background(map.getBackgroundColor());
map.draw(0, 0);
image(image, position.x, position.y);
} else {
}
}
boolean alive(PVector pPosition) {
int tile_id = map.getTileIndex(0, (int)(pPosition.x / 16), (int)(pPosition.y / 16));
if (tile_id == -2) {
return true;
} else {
return false;
}
}
boolean collision_detected(PVector pPosition, String pDirection) {
int x = 0;
int y = 0;
if ((pPosition.x + image.width) % 16 == 0) {
x = 1;
}
if ((pPosition.y + image.height) % 16 == 0) {
y = 1;
}
int character_left = (int)(pPosition.x / 16);
int character_right = (int)((pPosition.x + image.width) / 16 - x);
int character_top = (int)(pPosition.y / 16);
int character_bottom = (int)((pPosition.y + image.height) / 16 - y);
boolean any_collision = false;
int a = 0, b = 0;
if (pDirection.equals("left") || pDirection.equals("right")) {
a = 0;
b = 1;
} else if (pDirection.equals("up") || pDirection.equals("down")) {
a = 1;
b = 0;
}
int i = character_left - b;
int k = character_right + b;
if (pDirection.equals("right")) {
i = character_right + b; // set i to character_right because only right columns need to be checked for collision
} else if (pDirection.equals("left")) {
k = character_left - b;
}
for (; i <= k; i++) {
int j = character_top - a;
int l = character_bottom + a;
if (pDirection.equals("down")) {
j = character_bottom + a; // set j to character_bottom because only bottom rows need to be checked for collision
} else if (pDirection.equals("up")) {
l = character_top - a;
}
for (; j <= l; j++) {
int tile_id = map.getTileIndex(0, i, j);
switch(tile_id) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
any_collision = true;
break;
default:
break;
}
}
}
return any_collision;
}
void keyReleased() {
if (key == 'w') {
jump = false;
}
if (key == 'a') {
left = false;
velocity.set(0, velocity.y);
}
if (key == 's') {
crouch = false;
}
if (key == 'd') {
right = false;
velocity.set(0, velocity.y);
}
}
void keyPressed() {
if (key == 'w') {
jump = true;
}
if (key == 'a') {
left = true;
}
if (key == 's') {
crouch = true;
}
if (key == 'd') {
right = true;
}
}