How do I create a 3D like environment from 2D?
in
Programming Questions
•
4 months ago
Hi there, Right now I am trying to figure out how I can make the ball bounce off the point where
the environment is. I am new to processing so this may be easy but I have asked around and it seems to be
a rather large problem.
Also the if mousePressed to change stage doesn't seem to work properly in the main menu the instructions and exit(); will not activate and if you press start game the ball moves without re-clicking.
Any help would be gratefully taken!
import processing.opengl.*;
PImage bg;
PImage startscreen;
PFont titlefont;
PFont menufont;
boolean start = false;
boolean inside;
int stage;
PVector ballPos, ballVel;
int score = 0;
int multiplier = 0;
int scoreincrement = 100;
int level = 1;
// Maybe change boxes to objects and them call them in to check for intersections?
void setup() {
ballPos = new PVector(275, 187, -30);
ballVel = new PVector(); // no speed
stage = 1;
size(550, 375, OPENGL, P3D);
rectMode(CENTER);
smooth();
titlefont = loadFont("Game-Over-250.vlw");
menufont = loadFont("Harabara-80.vlw");
bg = loadImage("leo.jpg");
startscreen = loadImage("start.jpg");
}
void boxes (int j, int k) {
rect(width/2, height/2, width -j, height-k);
}
void draw() {
if (stage==1) {
mainMenuStage();
}
if (stage==2) {
background(bg);
noStroke();
lights();
textFont(menufont, 18);
fill(0, 255, 255);
text("Score:", 125, 50);
text(score, 185, 50);
text("Level:", 350, 50);
text(level, 410, 50);
noFill();
strokeWeight(0.75);
stroke(50, 205, 50);
quad(195, 137.5, 195, 237.5, 50, 350, 50, 25);
quad(355, 137.5, 355, 237.5, 500, 350, 500, 25);
stroke(50, 180, 205);
boxes( 100, 50); //Outer box
stroke(50, 205, 50);
boxes( 190, 120);
boxes( 252, 170);
boxes( 308, 210);
boxes( 345, 240);
boxes( 375, 262);
stroke(205, 50, 50);
boxes( 390, 275); // Inner Box
stroke(50, 205, 50);
strokeWeight(0.75);
fill(255);
noStroke();
pushMatrix();
translate(ballPos.x, ballPos.y, ballPos.z);
sphere(28);
popMatrix();
noFill();
stroke(50, 50, 205);
strokeWeight(2);
rect(mouseX, mouseY, 125, 80);
// is ball hitting the paddle?
if ((ballPos.x > mouseX-63)
&& (ballPos.x < mouseX +63)
&& (ballPos.y > mouseY - 40)
&& (ballPos.z < mouseY + 40)) {
inside = true;
} else {
inside = false;
}
if (stage==3) {
background(startscreen);
text("Instructions", 200, 300);
text("The objective of the game is to hit the ball with your paddle and get the highest score possible!", 275, 300);
}
println(ballPos.z);
//////////////////////////////////////////////////////////////////////////////////////////Problem 1//////////////////////////////
if ((start) && (stage==2)) {
ballPos.x += ballVel.x;
ballPos.y += ballVel.y;
ballPos.z += ballVel.z;
// Ball bouncing off random points and returning.
if ( ballPos.x < 50 && ballVel.x < 0 )
{
// bounce off left side
ballVel.x = -ballVel.x;
}
if (ballPos.z <= -750) {
// did enemy hit the ball?
ballVel.z = -ballVel.z;
}
else if (ballPos.z >= -30 && inside) {
// we hit the ball
ballVel.z = -ballVel.z * 1.01;
ballVel.x =random(-10, 10);
// here you can now change the direction
int scoreup = scoreincrement + multiplier;
score += scoreup;
multiplier += 10;
}
else if (ballPos.z >= -30) {
ballPos.z = -30;
// swicth to a game-over stage here
textFont(titlefont, 200);
fill(255,0,0);
text("GAME OVER", 275, 200);
}
}
}
void mainMenuStage()
{
fill(255, 0, 0);
noFill();
background(startscreen);
textFont(titlefont, 200);
textAlign(CENTER);
text("3D Pong", 275, 100);
textFont(menufont, 30);
text(" X Start Game", 275, 200);
text(" X Instructions", 275, 250);
text("X Quit Game", 275, 300);
//////////////////////////////////////////////////////////////////////////////////////// Problem 2 //////////////////////////////////////
// move this to mouseClicked and use if (stage == ...)
if (mousePressed == true
&& mouseX > 185
&& mouseX < 185 + 180
&& mouseY > 170
&& mouseY < 170 + 30)
{
stage = 2;
}
else if (mousePressed == true
&& mouseX > 185
&& mouseX < 185 + 230
&& mouseY > 170
&& mouseY < 170 + 30
) {
stage = 3;
}
else if (mousePressed == true
&& mouseX > 185
&& mouseX < 185 + 280
&& mouseY > 170
&& mouseY < 170 + 30
) {
exit();
}
}
void mouseClicked() {
start = true;
if ( stage == 2 && (ballVel.z == 0) )
{
ballVel.z = -14;
}
}
2