Collision Detection
in
Programming Questions
•
10 months ago
Can't seem to make Collision detection work for this code
PVector pos_player;
Bullet aBullet;
Zombie zombie;
void setup() {
size(1200, 800);
smooth();
pos_player = new PVector( -100, -100, 0);
aBullet = new Bullet();
pos_player = new PVector( 200, 400, 0 );
zombie = new Zombie();
}
void draw() {
background(128);
stroke(0);
fill(0,255,0);
rect( pos_player.x, pos_player.y, 50, 50);
aBullet.simulate();
aBullet.draw();
zombie.simulate();
zombie.draw();
if( key == CODED ){
if( keyCode == UP && keyPressed ){
pos_player.y--;
}
if( keyCode == DOWN && keyPressed ){
pos_player.y++;
}
}
}
void keyPressed(){
if( key == ' ' || key == 'f' ){
aBullet = new Bullet();
}
}
class Bullet{
PVector velocity;
PVector p,v;
Bullet(){
p = new PVector( pos_player.x+50, pos_player.y+25, 0 );
v = new PVector(4,0,0);
}
void simulate(){
p.x+=v.x;
p.y+=v.y;
}
void draw(){
pushStyle();
fill(255,0,0);
rectMode(CENTER);
rect( p.x, p.y, 25, 25);
popStyle();
}
}
class Zombie {
PVector p,v;
Zombie(){
p = new PVector( 1000,random(800),0);
v = new PVector(-1,0,0);
}
void simulate(){
p.x+=v.x;
p.y+=v.y;
}
void draw(){
pushStyle();
fill(0,0,255);
rectMode(CENTER);
rect( p.x, p.y, 50, 50);
popStyle();
}
}
Basically I want to be able to tell if the zombie touches any bullets the player fires. I also cant figure out how to make it so that when the bullet touches a zombie it kills it. Any help?
p.s zombie is blue bullet is red
PVector pos_player;
Bullet aBullet;
Zombie zombie;
void setup() {
size(1200, 800);
smooth();
pos_player = new PVector( -100, -100, 0);
aBullet = new Bullet();
pos_player = new PVector( 200, 400, 0 );
zombie = new Zombie();
}
void draw() {
background(128);
stroke(0);
fill(0,255,0);
rect( pos_player.x, pos_player.y, 50, 50);
aBullet.simulate();
aBullet.draw();
zombie.simulate();
zombie.draw();
if( key == CODED ){
if( keyCode == UP && keyPressed ){
pos_player.y--;
}
if( keyCode == DOWN && keyPressed ){
pos_player.y++;
}
}
}
void keyPressed(){
if( key == ' ' || key == 'f' ){
aBullet = new Bullet();
}
}
class Bullet{
PVector velocity;
PVector p,v;
Bullet(){
p = new PVector( pos_player.x+50, pos_player.y+25, 0 );
v = new PVector(4,0,0);
}
void simulate(){
p.x+=v.x;
p.y+=v.y;
}
void draw(){
pushStyle();
fill(255,0,0);
rectMode(CENTER);
rect( p.x, p.y, 25, 25);
popStyle();
}
}
class Zombie {
PVector p,v;
Zombie(){
p = new PVector( 1000,random(800),0);
v = new PVector(-1,0,0);
}
void simulate(){
p.x+=v.x;
p.y+=v.y;
}
void draw(){
pushStyle();
fill(0,0,255);
rectMode(CENTER);
rect( p.x, p.y, 50, 50);
popStyle();
}
}
Basically I want to be able to tell if the zombie touches any bullets the player fires. I also cant figure out how to make it so that when the bullet touches a zombie it kills it. Any help?
p.s zombie is blue bullet is red
1