Loading...
Logo
Processing Forum
Hey all,
I'm trying to figure out how to use mouse movement and 3d rendering to create a world in which the user can use the mouse to look around as you would in any 1st person game. Eventually I want to get the keyboard working on moving you around too, but I want to get this part right first. So I've set up a class that defines where you, the person, exists in the world. You have x,y,z (cartesian) coordinates that define where in the world you are. You also have two angles that define which way you are looking (azimuth/horizontal and elevation/incline/vertical). I'm definitely a bit rusty on my spherical coordinates, but I've got my program at least kind of working. On each frame I record the distance of the mouse from the center of the screen and then reset the mouse position to the center using the java Robot class. I then use these distances to change the azimuth and elevation angles. So this works, but the mouse movement is not very smooth. Sometimes it seems smooth but then it *randomly* gets very choppy. Help very much appreciated 


The important code happens in this class:
Copy code
  1. class FirstPerson {
  2.   float dx, dy;
  3.   PVector pos;
  4.   float elevation, azimuth;
  5.   
  6.   FirstPerson() {
  7.     reset();
  8.   }
  9.   
  10.   void reset() {
  11.     dx = 0;
  12.     dy = 0;
  13.     pos = new PVector(0,0,0);
  14.     // we start looking straight down the Y-axis
  15.     elevation = 0;
  16.     azimuth = PI/2;
  17.   }
  18.   
  19.   void start() {
  20.     //noCursor();
  21.     int frameX = frame.getX()+width/2; // center of screen
  22.     int frameY = frame.getY()+height/2;
  23.     robot.mouseMove(frameX, frameY);
  24.   }
  25.   
  26.   void drawReticle() {
  27.     stroke(255,150);
  28.     strokeWeight(1);
  29.     line(width/2, height/2-5, width/2, height/2+5);
  30.     line(width/2-5, height/2, width/2+5, height/2);
  31.   }
  32.   
  33.   void update() {
  34.     
  35.     
  36.     // update differential mouse movement this frame
  37.     int frameX = frame.getX()+width/2; // center of screen
  38.     int frameY = frame.getY()+height/2;
  39.     dx = mouseX - width/2;
  40.     dy = mouseY - height/2;
  41.     if(!frame.isUndecorated()){
  42.       dx += 3;
  43.       dy += 25;
  44.     }
  45.     
  46.     // reset mouse to center
  47.     robot.mouseMove(frameX, frameY);
  48.     
  49.     // update view angles based on mouse movement
  50.     if (dy > 0)
  51.       elevation += sqrt(abs(dy)/float(height))/2.0;
  52.     else
  53.       elevation -= sqrt(abs(dy)/float(height))/2.0;
  54.     if (dx > 0)
  55.       azimuth -= sqrt(abs(dx)/float(height))/2.0;
  56.     else
  57.       azimuth += sqrt(abs(dx)/float(height))/2.0;
  58.     
  59.     elevation = constrain(elevation, -PI/2+0.0001, PI/2-0.0001);
  60.     
  61.     // change camera position
  62.     float lookDist = 1000;
  63.     float lookX, lookY, lookZ;
  64.     lookX = pos.x + lookDist * cos(elevation) * cos(azimuth);
  65.     lookY = pos.y + lookDist * cos(elevation) * sin(azimuth);
  66.     lookZ = pos.z + sin(elevation) * lookDist;
  67.     println("elv: " + elevation + " az: " + azimuth);
  68.     println("x: " + lookX + " y: " + lookY + " z: " + lookZ);
  69.     camera(pos.x, pos.y, pos.z,
  70.            lookX, lookY, lookZ,
  71.            0,     0,     1    );
  72.     
  73.     println(dx + " " + dy);
  74.     
  75.     
  76.   }
  77.   
  78.   void stop() {
  79.     cursor();
  80.   }
  81.   
  82. }

Replies(14)

hello,

 

you might want to look at http://www.openprocessing.org/visuals/?visualID=25255 and

http://www.openprocessing.org/visuals/?visualID=25245

 

Both make use of ROBOT as well, so they don't run in the browser.

 

Greetings, Chrisir

Hey, thanks for the examples. I see you are doing less math in yours (fewer sin's and cos's). Maybe this is why yours is smoother? I also see you were planning a look up table, which doesn't seem like a bad idea.

Is doing 10 or so sin/cos commands per frame just too much strain on the computer?
Update:
I just added wrapping for the azimuth angle, and all of a sudden the movement seems to be a lot smoother. Interesting. I would really like to try the lookup table idea though.
joedoku, if you get the basics figured out.  I'd love it if you'd consider adding the functionality to my PeasyCam fork or working with me to add it.  I've been wanting to add a first person mode, but wasn't quite sure how to do it.
I would be glad to help once I've got a secure footing with things. Do you intend to have this same first person interaction with the mouse and keyboard (ala fps games)?

Hello!

very interesting ideas here

@joedoku: My experiences with a look up table have been bad so I abandonded this idea for now. Reason was that it was getting very less smooth with a look up table. I used a look up table from 0 to 360 degrees, that's far too rough.

So maybe one could make a look up table with one 10th or one 100th of degrees. Thus the look up table would be an array of 0 to 36000 with 0 being 0 degree, 1 being 1/100th degrees, 2 being 2/100th degrees etc.

@jeff_g: peasyCam is very worthy. It would be great to enhance it further.

You also might want to look at

1st Person Flight    : http://www.openprocessing.org/visuals/?visualID=28947

3rd Person Game - OpenProcessing: http://www.openprocessing.org/visuals/?visualID=25245

1st Person Game - OpenProcessing: http://www.openprocessing.org/visuals/?visualID=25255

 

Thanks!

Greetings, Chrisir

 

 

joedoku, sure I'm open to adding keyMapping functions to Peasy.  I believe it can be done now with the handlers, but we could add in some defaults and a way to turn it on/off.
So I think I've run into a wall of processing capability. As is, my 3D program works smoothly. However I've now added a 'sun' that revolves around the map. With this extra computation, you can tell the program really slows down when also trying to look around with the mouse. The keyboard movement doesn't seem to be affected so much. Like I said, if I remove the sun, it runs pretty smoothly (you can do this by removing the sun.update() line in the draw() method). Obviously I want my program to be more than a static 3D world. So what are my options? Multi-threading? Sin/cos lookup tables? Should I not even be bothering trying to do this in processing?

Latest version downloadable at - joekur.com/downloads/move3D.zip
Downloaded that version, but I don't see sun.update().  Since the scene is not dynamic, consider using a Display List.  That would store it all on the video card and you would just call the list - much much faster.  VBO could also work.
Apologies, sun.update() is within runMovement(), in move3D.pde.

By storing a non-dynamic scene on a video card you mean creating a "movie" of the world and looping it? I really do want to do dynamic 3D stuff eventually though. Can you also explain what VBO is for?

Link doesn't work.

better look at:

http://www.joekur.com/downloads/move3D.zip

No sun there in draw...

Thanks!



Sorry, it's within runMovement()  (below the draw function).
It's easy enough to update the scene, but if your drawing the same thing for more than few frames, then it makes sense to send all the vertex data to the video card one time, then just tell the video card to draw that again, and again, and again.

Display Lists are fairly easy.  I put one up on the Wiki as a quick example.
http://wiki.processing.org/w/Display_List

VBO (Vertex Buffer Objects) are a little harder to do. 
http://wiki.processing.org/w/Vertex_Buffer_Object_%28VBO%29

Vertex Arrays are another method.
http://wiki.processing.org/w/1,000,000_points_in_OpenGL_using_Vertex_Arrays

Hello joedoku,

when you are not interested in programming itself but rather want to make the Game without bothering with the technicalities, I would recommend to use a Game Editor.

E.g. unity: http://unity3d.com/
Indie-Version is free but with a Watermark.
http://www.heise.de/software/download/unity/41308

see
http://en.wikipedia.org/wiki/List_of_game_engines
http://en.wikipedia.org/wiki/Game_engine

With a Game Engine comes a Game development program.
There, you already have editors and lots of tools.

Greetings,

Chrisir