We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I've got the frame of a game working, but I wanted to add some collision between enemies, player and walls. Does anyone have any advice on how to achieve this, I've only added the code that seems essential for collections detection, since otherwise it would be 900 lines of code.
//detect forward movement.
if (key == 'w' || key == 'W' || keyCode == UP) {
if (currentScreen == 4) {
directiony1 = 1;
} else {
menu -= 1;
}
}
//detect backward movement.
if (key == 's' || key == 'S' || keyCode == DOWN) {
if (currentScreen == 4) {
directiony2 = 1;
} else {
menu += 1;
}
}
/detect left movement.
if (key == 'a' || key == 'A' || keyCode == DOWN) directionx1 = 1;
//detect right movement.
if (key == 'd' || key == 'D' || keyCode == UP) directionx2 = 1;
//detect if player wants to sprint.
if (keyCode == SHIFT) run = 1;
void keyReleased() {
//check player movement input
if (key == 'a' || key == 'A' || keyCode == DOWN) directionx1 = 0;
if (key == 'd' || key == 'D' || keyCode == UP) directionx2 = 0;
if (key == 'w' || key == 'W' || keyCode == DOWN) directiony1 = 0;
if (key == 's' || key == 'S' || keyCode == UP) directiony2 = 0;
if (keyCode == SHIFT) run = 0;
}
}
//calculate and place enemy
enemiescount = 0;
if (width/2 - playerx - enemies[enemiescount] > -enemies[enemiescount+9] && width/2 - playerx - enemies[enemiescount] < enemies[enemiescount+9] && height/2 - playery - enemies[enemiescount+1] > -enemies[enemiescount+9] && height/2 - playery - enemies[enemiescount+1] < enemies[enemiescount+9] ) {
if (width/2 - playerx - enemies[enemiescount] > -5 && width/2 - playerx - enemies[enemiescount] < 5 && height/2 - playery - enemies[enemiescount+1] > -5 && height/2 - playery - enemies[enemiescount+1] < 5 ) {
if (enemies[enemiescount+10] == 0) {
enemies[enemiescount+11] = millis();
enemies[enemiescount+10] = 1;
}
if (millis() - enemies[enemiescount+11] > 10) {
health = enemydamagetime * health - 0.5 * enemies[enemiescount+12] * damagemultiplyer;
enemies[enemiescount+10] = 0;
}
} else {
enemies[enemiescount] = enemymovementtime * int(cos(atan2(height/2-playery-enemies[enemiescount+1], width/2-playerx-enemies[enemiescount])) * enemies[enemiescount+8]*2 + enemies[enemiescount]);
enemies[enemiescount+1] = enemymovementtime * int(sin(atan2(height/2-playery-enemies[enemiescount+1], width/2-playerx-enemies[enemiescount])) * enemies[enemiescount+8]*2 + enemies[enemiescount+1]);
}
} else {
if (enemies[enemiescount+4] == 0) {
enemies[enemiescount+6] = int(random(-300, 300) + enemies[enemiescount]);
enemies[enemiescount+7] = int(random(-300, 300) + enemies[enemiescount+1]);
enemies[enemiescount+2] = int(random(1000, 2000));
enemies[enemiescount+3] = millis();
enemies[enemiescount+4] = 1;
}
if (enemies[enemiescount+5] == 0) {
enemies[enemiescount] = enemymovementtime * int(cos(atan2(enemies[enemiescount+7]-enemies[enemiescount+1], enemies[enemiescount+6]-enemies[enemiescount])) * enemies[enemiescount+8] + enemies[enemiescount]);
enemies[enemiescount+1] = enemymovementtime * int(sin(atan2(enemies[enemiescount+7]-enemies[enemiescount+1], enemies[enemiescount+6]-enemies[enemiescount])) * enemies[enemiescount+8] + enemies[enemiescount+1]);
}
if ( millis() - enemies[3] > enemies[2] ) {
enemies[4] = 0;
enemies[5] = 0;
}
if (enemies[enemiescount+6] - enemies[enemiescount] > -10 && enemies[enemiescount+6] - enemies[enemiescount] < 10 ) {
if (enemies[enemiescount+7] - enemies[enemiescount+1] > -10 && enemies[enemiescount+7] - enemies[enemiescount+1] < 10 ) {
enemies[enemiescount+5] = 1;
}
}
}
//move player
if (run == 0) playerspeed = 0;
if (directionx1 == 1 && directionx2 == 0 && directiony1 == 0 && directiony2 == 0) {
playerx += (10 * (1 + playerspeed)) * playermovementtime;
}
if (directionx2 == 1 && directionx1 == 0 && directiony1 == 0 && directiony2 == 0) playerx -= (10 * (1 + playerspeed)) * playermovementtime;
if (directiony1 == 1 && directiony2 == 0 && directionx1 == 0 && directionx2 == 0) playery += (10 * (1 + playerspeed)) * playermovementtime;
if (directiony2 == 1 && directiony1 == 0 && directionx1 == 0 && directionx2 == 0) playery -= (10 * (1 + playerspeed)) * playermovementtime;
if (directionx1 == 1 && directionx2 == 0 && directiony1 == 1 && directiony2 == 0) {
playerx += (5 * (1 + playerspeed)) * playermovementtime;
playery += (5 * (1 + playerspeed)) * playermovementtime;
}
if (directionx2 == 1 && directionx1 == 0 && directiony1 == 0 && directiony2 == 1) {
playerx -= (5 * (1 + playerspeed)) * playermovementtime;
playery -= (5 * (1 + playerspeed)) * playermovementtime;
}
if (directiony1 == 1 && directiony2 == 0 && directionx1 == 0 && directionx2 == 1) {
playery += (5 * (1 + playerspeed)) * playermovementtime;
playerx -= (5 * (1 + playerspeed)) * playermovementtime;
}
if (directiony2 == 1 && directiony1 == 0 && directionx1 == 1 && directionx2 == 0) {
playery -= (5 * (1 + playerspeed)) * playermovementtime;
playerx += (5 * (1 + playerspeed)) * playermovementtime;
}
//draw player
pushMatrix();
rectMode(CENTER);
noFill();
translate(width/2, height/2);
rect(0, 0, 20, 20);
popMatrix();
translate(width/2, height/2);
Answers
Check this: https://forum.processing.org/two/discussion/comment/89569/#Comment_89569
Kf
Thank you thats perfect! @kfrajer