user3113295
user3113295

Reputation: 21

OpenGL/C++ any way of moving an object in the direction the camera is facing?

I'm trying to make a 3D game of pool/billiards for a university project. I'm not a great programmer to start with so I'm struggling a lot.

Currently I have a basic table set up with all of the balls in place. The camera is positioned behind the white ball (sort of like a first person view looking down the table) and this can be rotated around the white ball (always looking at it) using the keyboard. Its sort of like an aiming system but the problem I have is that I need the ball to move in the direction the camera is facing (where the user is aiming the ball). I only know how to translate the ball in either the x,y or z direction but don't know how to specify the direction the camera is facing.

Upvotes: 0

Views: 1310

Answers (1)

Andon M. Coleman
Andon M. Coleman

Reputation: 43319

I would suggest you brush up on coordinate spaces.

If you want to move something in the "direction of the camera," you generally move Z-units in camera/eye/view-space (these are all synonyms for the same thing). Since this is OpenGL, view space is setup such that movement in the -z direction will move forward and +z will move backward. You ought to have the necessary matrices to transform in- and out- of view space, you just need to know how coordinate space transformations work to complete this puzzle.

With that said, you ought to be able to look at something like this and get a better idea of what is going on. There are plenty of other explanations, but I really like the color diagram it includes of the scene projected onto the image plane ;)

   

I would point out that the description of the transforms in that answer are conceptually backwards. You start out with points in object space, and then gradually work your way through to window space after projection.

Upvotes: 1

Related Questions