Collision Mapping
in
Programming Questions
•
11 months ago
Hey people. So I'm working on a Missile Command game. And right now I need to work on Collision mapping.... probably the hardest part.
So I'm wondering if anybody knows how I would add collisions for the line you create when you click, and the lines (Missiles) which come from the top of the screen.
Something with would add an event when this happens would be helpful. Code below. All help is appreciated.
So I'm wondering if anybody knows how I would add collisions for the line you create when you click, and the lines (Missiles) which come from the top of the screen.
Something with would add an event when this happens would be helpful. Code below. All help is appreciated.
- class Missile {
float endX; // End X position
float endY; // End Y position
float endvarX; // Line gradient X
float endvarY; // Line gradient Y
float startX; // Start of missile line
float startY; // start of missile line
color c;
Missile(float init_endvarX, float init_endvarY, float init_startX, float init_startY,
color init_c)
{
endX = init_startX;
endY = init_startY;
endvarX = init_endvarX;
endvarY = init_endvarY;
startX = init_startX;
startY = init_startY;
c = init_c;
}
void render() {
{
smooth();
stroke(c);
line(startX, startY, endX, endY);
}
}
void update() {
endY = endY + endvarY;
endX = endX + endvarX;
}
} // class Missile
- int framestore; // frame last missile was created on
int Mdelay; //delay between next fire time
int posX;
int posY;
int fireX;
int fireY;
Missile randomMissile() {
return new Missile(random(-2.0, 2.0), random(0.5, 2.5),
random (200, 600), 0,
color(random(0, 256),
random(0, 256),
random(0, 256))
);
}
ArrayList<Missile> missilelist; // list in null initially
void setup() {
size(700, 500);
framestore = 0;
Mdelay = (int)random(60, 150);
missilelist = new ArrayList<Missile>(); // creates and initially empty ArrayList of missile objects
frameRate(30);
}
void draw() {
background(255);
if (Mdelay < (frameCount - framestore) ) {
for (int i = 0; i < (int)random(3); i = i+1) { // gives the initial value of 0
// then randomized between 3 maximum values. This means there can only be between 1 and
// 3 missiles active
// then adds +1 to the value of i, but cannot be more than 3.
//Above code courtesy of Jacques
missilelist.add(randomMissile());
}
framestore = frameCount;
}
// render all the missiles.
for (Missile b : missilelist) {
b.update();
b.render();
}
fill(255, 80, 80);
triangle(350, 480, 335, 500, 365, 500);
}
void mousePressed() {
fireX = 350;
fireY = 480;
posX = mouseX;
posY = mouseY;
smooth();
//strokeWeight(2.5);
stroke(255,0,0);
line(fireX, fireY, posX, posY);
}
1