I’ve been programming a 3D game where a ship avoids spikes on a terrain of triangle sectors. I’ve been having two major problems with the game so far. 1) the triangle sector terrain seems to be moving slowly downwards and eventually off-screen, but it seems I’m not changing a z value anywhere. Second, I cannot properly track collisions between the ship and the spikes. I’m not sure if this is because of translations or how I drew the spikes. Code:
int cols, rows;
int scl = 20;
int w = 2000;
int h = 1600;
Ship ship;
ArrayList<Spikes> spikes;
color g;
color s;
float flying = 0;
float score = 0;
float[][] terrain;
boolean gameStart;
boolean gameOver;
void setup() {
size(600, 600, P3D);
cols = w / scl;
rows = h/ scl;
terrain = new float[cols][rows];
g = color(156,111,81);
s = color(0);
ship = new Ship(1000,1100,50);
spikes = new ArrayList<Spikes>();
gameStart = true;
gameOver = false;
}
void draw() {
if(gameOver == true){
background(255);
text("GAME OVER",width/2,height/2);
}
if( gameStart == true){
flying -= 0.1;
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
terrain[x][y] = flying;
}
}
background(0);
textSize(50);
fill(255);
text(frameCount, 50,50);
stroke(s);
fill(g);
score += 0.01;
translate(width/2, height/2+50);
rotateX(PI/3);
translate(-w/2, -h/2);
for (int y = 0; y < rows-1; y++) {
beginShape(TRIANGLE_STRIP);
for (int x = 0; x < cols; x++) {
vertex(x*scl, y*scl, terrain[x][y]);
vertex(x*scl, (y+1)*scl, terrain[x][y+1]);
}
endShape();
}
for( int i = 0; i < 6; i++){
spikes.add(new Spikes(random(300,1500),500,50));
spikes.get(i).render();
spikes.get(i).move();
}
ship.render();
ship.move();
}
}
class Ship{
float x;
float y;
float z;
Ship(float _x, float _y, float _z){
x = _x;
y = _y;
z = _z;
}
void render(){
translate(x,y,z);
fill(160);
stroke(0);
beginShape(TRIANGLES);
scale(0.6);
vertex(-50,0,25);
vertex(-150,100,25);
vertex(0,100,25);
vertex(50,0,25);
vertex(150,100,25);
vertex(0,100,25);
vertex(-50,0,-25);
vertex(-150,100,-25);
vertex(0,100,-25);
vertex(50,0,-25);
vertex(150,100,-25);
vertex(0,100,-25);
vertex(-50,0,25);
vertex(50,0,25);
vertex(0,-300,25);
vertex(-50,0,25);
vertex(50,0,25);
vertex(0,100,25);
vertex(-150,100,25);
vertex(150,100,25);
vertex(-150,100,-25);
vertex(150,100,25);
vertex(-150,100,-25);
vertex(150,100,-25);
vertex(-150,100,25);
vertex(150,100,25);
vertex(150,100,-25);
vertex(50,0,25);
vertex(50,0,-25);
vertex(0,-300,-25);
vertex(50,0,25);
vertex(0,-300,25);
vertex(0,-300,-25);
vertex(-50,0,25);
vertex(-50,0,-25);
vertex(0,-300,-25);
vertex(-50,0,25);
vertex(0,-300,25);
vertex(0,-300,-25);
vertex(-50,0,25);
vertex(-50,0,-25);
vertex(-150,100,25);
vertex(50,0,25);
vertex(50,0,-25);
vertex(150,100,25);
endShape();
}
void move(){
if(keyPressed && keyCode == LEFT){
x-=3;
}
if(keyPressed && keyCode == RIGHT){
x+=3;
}
if(keyPressed && keyCode == UP){
y-=3;
}
if(keyPressed && keyCode == DOWN){
y+=3;
}
}
}
class Spikes{
float x;
float y;
float z;
Spikes(float _x, float _y, float _z){
x = _x;
x = random(300,1500);
y = _y;
z = _z;
}
void render(){
translate(x,y,z);
fill(0);
stroke(255);
beginShape(TRIANGLES);
vertex(100,-300,-100);
vertex(150,-350,-100);
vertex(100,-300,200);
vertex(100,-300,-100);
vertex(50,-350,-100);
vertex(100,-300,200);
endShape();
translate(-x,-y,-z);
}
void move(){
y+=4;
if(y>1810){
x = random(300,1500);
y = 600;
}
if(x + 5 >= ship.x && x - 5 <= ship.x && y + 5 >= ship.y && y - 5 <= ship.y){
gameStart = false;
gameOver = true;
}
}
}