October 24th, 2013, 12:12 AM
I managed to create a 3d starfield effect using c++ and sdl. It wasn't that hard to do although I admit I just followed the code from a website where someone did the same effect in html 5.
Typically all it seems you have to do is create an array of 3d coordinates and decrease the value of z, so the formula that you see all the time is something like:
screen.x = x / z
screen.y = y / z
Now while this is all well and good I would like to have a better understanding of why this works and I was wondering if someone could explain what is actually happening here.
Specifically I want to know why if you divide x by z and y by z you get the 3d coordinates in 2d. Why does this happen?
October 24th, 2013, 03:57 AM
It is not very complex matter, but it is not easy to explain shortly if you are not acquainted with analytic geometry.
Shortly speaking, if we define some points (or objects as set of points) in 3D space, we can then perform some manipulations with them:
- rotation around arbitrary axis;
- movement in arbitrary direction;
- shrinking and stretching;
All these are performed with simple enough math operations on the set of points.
Besides them there are also operation of projection (for example 3D space to 2D).
So what happens to star field is the movement in z direction and projection to the imaginary plane between user and the stars - this imaginary plane is then simply rendered to screen.
Dividing x and y by z is not the completely correct transformation, but it is good enough approximation for effect of flying through stars.
If you really want to dive deeper in this topic, it would be good to lay your hands on some book or tutorial on basics of analytic geometry in 2D and then in 3D (they can often be found hand by hand with books on linear algebra) and do some practical tasks - like rotating squares and other objects on the screen etc.
Do not be afraid of horrible titles of these branches - they really involve only arithmetic and bit of trigonometry.
November 18th, 2013, 10:33 PM
You wouldn't happen to know the titles of any of these books would you?
Having said that I think I would prefer online tutorials to purchasing books since I'm not looking to get too deep into this topic, just enough to know the basics really.
Just so you know this is where I got the code from:
All I did was convert it to c++ and sdl but unfortunately I don't think the writer gives a great explanation for what is actually happening. For example the number 128 just appears from nowhere and there is no attempt to explain what its purpose is or how it is derived.