How the camera move in 3D ? And how to rotate a Object in 2D ?

Started by
3 comments, last by tksuoran 18 years, 10 months ago
I have a great doubt about how the camera move in 3D games, I need help to clear my doubt, I assumed 2 hypotheses, which of them is the correct ? 1 - The camera moves in X,Y,Z and the world remains stoped or 2 - The world moves in X,Y,Z and the camera remains stoped like in 2D games Which of them is the correct ? or no one of them is correct ? I have another doubt, how I rotate a object, somebody has a mathematic formula ? Usually I use SDL or Allegro to study, I know allegro have a function that do this for me, but I want to create my own function that rotate images and geometric forms, somebody has some tip in how to make this ? I thank the help and sorry for my english
Advertisement
Ummm... actually it depends on how you implement it. I've had 2d worlds where the camera moves and the world is stationary and vica verse.

When you REALLY think about it, what does it really matter? Einstein said (and he was right) that you can never tell what's moving and what's not. It all depends on your point of view.

Now, for how it's programmed... it depends... bwahahahaha. The way I see it, OpenGL has me moving the world constantly. But if I decided to write an engine, I'd have my interface seem like I'm just moving a camera throughout the world.

I don't want to ramble at all, so I'll finish by saying this question is a matter of your Point of View and how you want to see the problem.

As for rotating an object in 2d, just find a formula to rotate an single point around a center point. (that'll be easy to find or even just figure out, just simple trigonometry). Then apply that formula to every pixel in your 2d image.

(Thinks the above method would look really ugly)

Or you could try one of the methods describe in this file.

Good luck.
----------------------------------------------------------No matter how eloquently you state your argument, the fact remains that the toilet seat is a bistable device. Therefore it's natural position is no more down than it is up.[SDL Smooth Tile Scrolling]
Everything in the world consists of vertices, which in the end get transformed usually by a matrix. The camera is implemented by plugging it into *that* matrix (called model view transformation).

Static world geometry may be in 'world coordinates', that is, it does not have own transformation matrix, but in general all 3D objects have their own frame. A frame should provide two transformations: local to world, and world to local. A simple implementation might be just a single matrix, local_to_world, the other matrix can be computed by taking inverse of the other. In other words, frame.local_to_world * frame.world_to_local = I, I is identity matrix which transforms vector to itself. I * A = A where A is any matrix. Also A * I = A, but in general A * B != B * A.

Now we get to the camera. Camera also has a frame, and the model to view transformation matrix would be object.frame.local_to_world * camera.frame*world_to_local (or the other way, depending on which way your matrix multiplication works). That transformation will transform any object vertex to cameras local space. That is where you want them to be before they get projected.

Note that the camera is optional. You can simply modify the transformation of each individual frame - but, it will be easier to modify camera transformation and then compute concatenated object and camera transformation which is then used as model view transformation.

To make your own code to transform (translate, rotate. scale etc) things, you really should use own matrix code instead of glTranslate() and glRotate() etc. The important thing is to maintain the transformations for each frame, and then implement the camera by rendering each object using the combined object and camera matrix as above.

Initially (1) you should set both camera transformation and object transformation to identity matrix and try to draw some geometry which contains vertices so that the should appear in the view. glBegin(GL_POINTS); glVertex3f(0,0,10); glEnd(); is good for debugging. Notice that you need to setup the projection matrix properly to get something on screen.

In the above case object.frame.local_to_world * camera.frame*world_to_local would be I * I.

Then (2) you can see what happens if you move the object around, so make the drawing test code to use glVertex3f(0,0,0) and set up the object local_to_world to a transformation which translates z by 10. This should effectively get you visually the same result.

In the above case object.frame.local_to_world * camera.frame*world_to_local would be object.frame.local_to_world * I = object.frame.local_to_world.

Then (3) you can put back identity matrix to object frame and instead set up the camera local_to_world matrix so that it translates z by -10. This also should give the same result. Notice that when you modify local_to_world, you should also update world_to_local at some point before it gets used. It gets used when the model view transformation is computed.

Now in the above case object.frame.local_to_world * camera.frame*world_to_local would be I * camera.frame.local_to_world = camera.frame.local_to_world.

And finally notice that object.frame.local_to_world from case (2) == camera.frame.local_to_world from case (3).

Some basic linear algebra - which deals with matrices and vertices - will help. But with some trying it is also possible to first learn 3D transformations and then you already know something about linear algebra :)

[Edited by - tksuoran on June 7, 2005 3:03:18 PM]


Thank you wasted_druid, I have downloaded the file you indicated, there teach very well how to use the matrix to rotate and thanks for the tips

I read the file, and this part of text called my attention

"" For a 2D rotate about the origin, multiply all coordinates by this matrix: ""

( x y ) ( )
( cos(r) sin(r) )
( )
( -sin(r) cos(r) )
( )



Thank you very much tksuoran, you almost wrote a tutorial explaining how to use a camera and rotate the things, with your help now I know a little more. You said ""Everything in the world consists of vertices"" now this make sense for me, because I am studying a game engine 3D, and I use very much the 3 vertices X,Y,Z to move the things, I am really a beginner, thank you very much

This is a little game that I made some time ago, was used SDL, here is the link http://geocities.yahoo.com.br/binhoeu/ to download click in ""Download do jogo"" still have many bugs to fix, stay at will to play

I am from Brasil, and again sorry for my english

If somebody more want to give tips stay at will
Hey that jogo game was actually quite neat. I can imagine where you could plug in rotating 2D images. But the game is also nice as it is. I did not finish even the first level, but I wanted to try several times.

You can also do 2D by using 3D API like OpenGL. You can use glOrtho() to setup perspective matrix and draw textured quads like glBegin(GL_QUADS); glVertex2f(0,0); glVertex(1,0); glVertex(1,1); glVertex(0,1); glEnd(); and setup object matrix with translation, rotation and scaling, or even use glTranslate(position); glRotate(orientation); glScale(size);. Correction: you need to set up matrices always before you draw anything.

OpenGL uses backface culling which may cause things not to show up, when drawing 2D you might want to disable it. To draw objects with transparency you can use alpha blending, additive blending, or alpha test. For alpha blending and alpha test you need alpha in your texture. Additive blending does not need alpha, but it looks different, so you might want to use it in effects like weapons and explosions instead of ships. For alpha blending you may want to try different blending functions, and for alpha testing you may need to set up alpha test treshold.

This topic is closed to new replies.

Advertisement