Hello,
I like the idea of a 3D pong game a lot. Looks great.
Did you read
http://www.processing.org/learning/p3d/ ?
In processing, the x goes to the right, the y down your screen
and -z (this is minus z) into the screen, away from you.
A 3D scene
To make a proper 3D scene, you place your paddles as follows:
Imagine a flat table on which you play your game of pong. Your table extends from left to right (x) and forward and backward (z) and has a height within in your room (y).
Here the paddles have a X and a Z position. The left paddle has a x of maybe 100, the right of maybe 700 on your table. When they move, you change the z-position only (moving on the table as you play). Thus they move into (minus z) and out of (plus z) the screen (like in the game). So change Z in order the get the paddles going up and down (forward and backward really).
Anyway, the visual height of the paddle is determined by the y-value (they float above the table or lie on it). You don't want to change this. Since 0 is at the top of the screen (way above the table), you want Y be something like 700.
The camera
The camera has 9 values:
- the first three values being position xyz (where is the camera?),
- next three values being look at xyz (Where does the camera look at? Center of your game.) and
- last three not important (rotation).
Not knowng your code this should work:
- // ==================================================
- // camera
- camera(
- width/2.0, 10, 700,
- width/2.0, height/2.0, 0,
- 0, 1, 0);
- // ==================================================
- This camera is fixed at a spot (line 4) which x-position is centered, the y-pos being 10 which is pretty high above the scene and z being 700 which is far out of the screen towards you.
- It is also looking at a spot (line 5) at the center of the x-axis, the center of y and with a Z-position being 0 which is the screen's position.
- It is not rotated (straight up) (line 6).
What does it mean?
Well, when you look to a house and run towards it, the house gets bigger. Here the camera position (your eyes) would change (you run towards the house) but the look at would stay put (you always look at the house). Find a small example with cursor movement below (Camera wanders, look at is fixed).
When you stand on a spot and look around, we got another situation. Here the camera position (your eyes) would stay fixed, whereas the look at would change (you turn your head to the house, then to its garden and then to the street). The scenery seems to wander.
Of course you can run on the street past the house looking at the house next to you or even run past it, first looking at it and then looking straight ahead again, thus combining changing of the camera position and the look at.
Later see an example here:
http://www.openprocessing.org/sketch/47022
and here (works not in browser)
http://www.openprocessing.org/sketch/25255
The Lights
Oh, and remember to use lights();
Looks much better.
Hope this helps.
Greetings, Chrisir
- float CameraX;
- float CameraY;
- void setup()
- {
- size( 800, 800, P3D );
- CameraX = width / 2.0;
- CameraY = 50;
- } // setup
- void draw()
- {
- background(0);
- stroke(111, 110, 110);
- fill(111, 110, 110);
- // ==================================================
- // draw floor
- float floorY=600;
- line ( 20, floorY, -200,
- width-20, floorY, -200);
- line ( 20, floorY, -20,
- width-20, floorY, -20);
- line ( 20, floorY, -200,
- 20, floorY, -20);
- line ( width-20, floorY, -200,
- width-20, floorY, -20);
- // ==================================================
- // camera
- camera(
- CameraX, CameraY, 700,
- width/2.0, height/2.0, 0,
- 0, 1, 0);
- // ==================================================
- // paint boxes
- MyBox ( 310, 20, 22, 530, 210, 3, 14, color (255, 0, 0));
- MyBox ( 110, 440, 44, 52, 10, 222, 14, color (2, 0, 255));
- } // draw
- void keyPressed() {
- if (key == CODED) {
- if (keyCode == UP) {
- CameraY-=10;
- }
- else if (keyCode == DOWN) {
- CameraY+=10;
- }
- else if (keyCode == RIGHT) {
- CameraX+=10;
- }
- else if (keyCode == LEFT) {
- CameraX-=10;
- }
- else {
- // nothing
- }
- }
- else {
- // not key == CODED
- //
- }
- }
- void MyBox(float x1, float y1, float z1, float x2, float y2, float z2, float weight, color strokeColour)
- // was called drawLine; programmed by James Carruthers
- // see http://processing.org/discourse/yabb2/YaBB.pl?num=1262458611/0#9
- {
- PVector p1 = new PVector(x1, y1, z1);
- PVector p2 = new PVector(x2, y2, z2);
- PVector v1 = new PVector(x2-x1, y2-y1, z2-z1);
- float rho = sqrt(pow(v1.x, 2)+pow(v1.y, 2)+pow(v1.z, 2));
- float phi = acos(v1.z/rho);
- float the = atan2(v1.y, v1.x);
- v1.mult(0.5);
- pushMatrix();
- translate(x1, y1, z1);
- translate(v1.x, v1.y, v1.z);
- rotateZ(the);
- rotateY(phi);
- noStroke();
- fill(strokeColour);
- box(weight, weight, p1.dist(p2)*1.2);
- popMatrix();
- }