Advertisement

movement in 3-space

Started by April 26, 2003 05:41 PM
5 comments, last by noRulez43 21 years, 10 months ago
I have a simple little spaceship I drew with a sphere and a disc, and I want to follow this spaceship around in a 3rd person sort of way. I have just one question. If I want my ship to start moving left...how do I do this? I tried to just rotate it, but it just rotates and moves forward still.
Try this or this. OpenGl doesn't rotate movement, it only rotates what you draw. To rotate use math as explianed in the two tutorials.

[edited by - xiros on April 26, 2003 10:01:40 PM]
Advertisement
okay, thanks...I got that nailed down. This is how I''m calculating the new camera position:

camera_x_position -= sin(camera_y_angle*(3.1415/180))*move_speed;
camera_z_position -= cos(camera_y_angle*(3.1415/180))*move_speed;

Now, how would I go about calculating coordinates for my ship which I always want to be directly in front of my camera?
off the top of my head, try this:

ship_x_position = camera_x_position + 10 * cos(camerayangle)
ship_y_position = camera_x_position + 10 * sin(camerayangle)

in other words, you know the camera's position and the camera's angle, so just imagine a line of length, say, 10 "sticking out" in front of the camera, and then use basic trig to find out the ship's coordinates based on the camera's coordinates.

I (and a lot of people!) do the same kind of thing with skyboxes - in a 3rdperson walkthrough world you always want the skybox centered around the person (or camera), so you center it around where the person is.

You might need to experiment a bit, I've been working with OpenGL for months and the paradigms of rotating the world around the camera still confuse the snot out of me.

[edited by - DalTXColtsFan on April 27, 2003 1:08:32 AM]
Another thing you can play with is combining glOrtho mode with glPerspective mode - just draw the ship in 2D coordinates - they''ll be the same coordinates no matter where the camera is.

Here is a sample of combining the two modes:

http://home.att.net/~tennisman2/TryingToCombine.zip

It''s Visual C++ 6.0 code.
I guess I''m confused why I can''t just do a glRotate on it with the same angle as the camera is at. I tried those equations, and I''m getting some odd results. I''m very confused, but I''ll keep trying.
Advertisement
Hey, whoever you are that asked this question I hope you''re still around .

I think I got it to work. I don''t have time to explain in detail right now, but really the important things to understand are:

1. By default in OpenGL, you don''t move the *camera* around the *world*. You move the *world* around the camera. If you have an object at 10, 12 and you want to put the camera at 8, 12, you first translate the world''s origin to the point -8, -12. and then draw the shape at 10, 12 relative *to the origin that was just moved*. This creates the *illusion* that you''ve moved the camera.

2. Remember that you different result if you rotate then translate vs. if you translate then rotate.

What I did in my city code is, I declared variables called personx, personz and rotangle that contain the person''s "real" coordinates and angle. I then translate the world to the point -personx, -personz and draw everything.

My code is at http://home.att.net/~tennisman2/CityCode.zip. The relevant routines are DrawRobots, WinMain, and Render in main.cpp, and DrawRobot in robot.cpp (note that I don''t make the same concession for the robots that I do for the person - I probably should have.)

I apologize, it''s AWFUL code, looks like it''s just been slapped together, but it puts a robot in front of the "camera".

If you''re curious as to what it looks like with images, they''re at http://home.swbell.net/joe2002/OpenGL.htm.

Hope this helps.

This topic is closed to new replies.

Advertisement