Page 1 of 1

I need help with math.. :D

PostPosted: Mon Jun 04, 2012 4:01 pm
by lcl
Hello I hope someone can help me with this.

I am trying to make a room which the player will be able to turn full 360 degrees.
Something like the box that Bee-Ant made but one the player is inside, not outside.
Well what I can't figure out is how to make the room turn. I have the coordinates of the
corners but what do I need to do with them to make them rotate? It could be easier without the
perspective messing it up. If I just could get the corners to rotate correctly I am quite sure I could
make the whole thing to work.

Here is picture that shows what corner I should for example to get rotated (with all others of course :lol: )
Oh and that pacman is currently in the middle of the room. (The room continues a little out of the screen. :D )
room.png


EDIT: The problem is that the corners don't settle to a circle but ellipse. It makes it much harder (for me at least)...


Thanks in advance!

Re: I need help with math.. :D

PostPosted: Tue Jun 05, 2012 9:07 am
by Bee-Ant
Something like this?
http://dl.dropbox.com/u/3997355/Demo/3D ... lipses.ged
The control is arrow keys
I have made this over a year ago as experiment :)

Re: I need help with math.. :D

PostPosted: Wed Jun 06, 2012 7:47 am
by lcl
Well, something like that..
Your example just doesn't have the perspective in it and the
shape of the room is a little skewed..

The room won't appear like this in reality:
room1.png

The roof and the floor should be a little flatter.
room2.png

And if this had perspective the corners behind should be closer to each other than the
ones in front of.

And well I still can't understand the math.
I guess that's where I need the most help. What do I even need to calculate to get
the corners to fit right on the ellipse? It is very difficult for me to understand where I should start
from..

But thank you anyway, Bee! :)

Re: I need help with math.. :D

PostPosted: Sat Jun 09, 2012 1:30 pm
by Bee-Ant
Sorry, I just posted that sucked demo without doing any modification first.

Re: I need help with math.. :D

PostPosted: Sat Jun 09, 2012 11:23 pm
by lcl
No need to be sorry :)
It just seems that what I'm trying to do is very hard to achieve.. :P

Re: I need help with math.. :D

PostPosted: Tue Jun 19, 2012 9:34 pm
by jimmynewguy
Well in your first post it says that the main problem for you is that the room is an ellipse instead of a circle. Is this the only problem you have with this? If it is, then you should just do the room as a circle but use the trig of an ellipse.

http://en.wikipedia.org/wiki/Ellipse#Ge ... etric_form

If not then well, maybe that helps somehow?

Re: I need help with math.. :D

PostPosted: Thu Jun 21, 2012 10:21 pm
by makslane
This is a code of an old program of mine (Visual3D, 1993).
I think this can help you.

Code: Select all
double d = 100; //Perspective distance (you must adjust this value)
double scale = 1; //Object size
int center_x = 500, center_y = 500; //the center of the canvas/screen (view.width / 2, view.height / 2, for example)
double teta_x= 0.8 , teta_y = 0.8; //Vision angle (radians)

void line3d(double x1, double y1, double z1, double x2, double y2, double z2)
{
   double x3d_x1,y3d_y1,z3d_z1,x3d_x2,y3d_y2,z3d_z2;
   double vector_y3d_x1,vector_y3d_y1,vector_y3d_x2,vector_y3d_y2;
   double p1,p2,pers1,pers2;
   LONG point_x_x1,point_y_y1,point_x_x2,point_y_y2;
   
   
   x3d_x1 = x1*scale;
   y3d_y1 = y1*scale;
   z3d_z1 = z1*scale;
   
   x3d_x2 = x2*scale;
   y3d_y2 = y2*scale;
   z3d_z2 = z2*scale;
   
   /* Perspective !!! */
   
   
   p1 = z3d_z1*sin(teta_y)*cos(teta_x)+y3d_y1*sin(teta_x)*cos(teta_y)+x3d_x1*cos(teta_x)*cos(teta_y);
   p2 = z3d_z2*sin(teta_y)*cos(teta_x)+y3d_y2*sin(teta_x)*cos(teta_y)+x3d_x2*cos(teta_x)*cos(teta_y);
   
   p1 = (d+p1)*((d+p1)/d);
   p2 = (d+p2)*((d+p2)/d);
   
   pers1 = ((d+p1)/d)/2;
   pers2 = ((d+p2)/d)/2;
   
   vector_y3d_x1 = center_x+pers1*y3d_y1*scale_x*cos(teta_x);
   vector_y3d_y1 = center_y+pers1*y3d_y1*scale_y*sin(teta_x)*sin(teta_y);
   
   vector_y3d_x2 = center_x+pers2*y3d_y2*scale_x*cos(teta_x);
   vector_y3d_y2 = center_y+pers2*y3d_y2*scale_y*sin(teta_x)*sin(teta_y);
   
   point_x_x1 = vector_y3d_x1-pers1*x3d_x1*scale_x*sin(teta_x);
   point_y_y1 = (vector_y3d_y1+pers1*x3d_x1*scale_y*sin(teta_y)*cos(teta_x))-pers1*z3d_z1*scale_y*cos(teta_y);
   
   point_x_x2 = vector_y3d_x2-pers2*x3d_x2*scale_x*sin(teta_x);
   point_y_y2 = (vector_y3d_y2+pers2*x3d_x2*scale_y*sin(teta_y)*cos(teta_x))-pers2*z3d_z2*scale_y*cos(teta_y);
   
   //Some draw function
   draw(point_x_x2,point_y_y2);
}

Re: I need help with math.. :D

PostPosted: Thu Jun 21, 2012 11:08 pm
by lcl
makslane wrote:This is a code of an old program of mine (Visual3D, 1993).
I think this can help you.

Code: Select all
double d = 100; //Perspective distance (you must adjust this value)
double scale = 1; //Object size
int center_x = 500, center_y = 500; //the center of the canvas/screen (view.width / 2, view.height / 2, for example)
double teta_x= 0.8 , teta_y = 0.8; //Vision angle (radians)

void line3d(double x1, double y1, double z1, double x2, double y2, double z2)
{
   double x3d_x1,y3d_y1,z3d_z1,x3d_x2,y3d_y2,z3d_z2;
   double vector_y3d_x1,vector_y3d_y1,vector_y3d_x2,vector_y3d_y2;
   double p1,p2,pers1,pers2;
   LONG point_x_x1,point_y_y1,point_x_x2,point_y_y2;
   
   
   x3d_x1 = x1*scale;
   y3d_y1 = y1*scale;
   z3d_z1 = z1*scale;
   
   x3d_x2 = x2*scale;
   y3d_y2 = y2*scale;
   z3d_z2 = z2*scale;
   
   /* Perspective !!! */
   
   
   p1 = z3d_z1*sin(teta_y)*cos(teta_x)+y3d_y1*sin(teta_x)*cos(teta_y)+x3d_x1*cos(teta_x)*cos(teta_y);
   p2 = z3d_z2*sin(teta_y)*cos(teta_x)+y3d_y2*sin(teta_x)*cos(teta_y)+x3d_x2*cos(teta_x)*cos(teta_y);
   
   p1 = (d+p1)*((d+p1)/d);
   p2 = (d+p2)*((d+p2)/d);
   
   pers1 = ((d+p1)/d)/2;
   pers2 = ((d+p2)/d)/2;
   
   vector_y3d_x1 = center_x+pers1*y3d_y1*scale_x*cos(teta_x);
   vector_y3d_y1 = center_y+pers1*y3d_y1*scale_y*sin(teta_x)*sin(teta_y);
   
   vector_y3d_x2 = center_x+pers2*y3d_y2*scale_x*cos(teta_x);
   vector_y3d_y2 = center_y+pers2*y3d_y2*scale_y*sin(teta_x)*sin(teta_y);
   
   point_x_x1 = vector_y3d_x1-pers1*x3d_x1*scale_x*sin(teta_x);
   point_y_y1 = (vector_y3d_y1+pers1*x3d_x1*scale_y*sin(teta_y)*cos(teta_x))-pers1*z3d_z1*scale_y*cos(teta_y);
   
   point_x_x2 = vector_y3d_x2-pers2*x3d_x2*scale_x*sin(teta_x);
   point_y_y2 = (vector_y3d_y2+pers2*x3d_x2*scale_y*sin(teta_y)*cos(teta_x))-pers2*z3d_z2*scale_y*cos(teta_y);
   
   //Some draw function
   draw(point_x_x2,point_y_y2);
}

Wow thank you Makslane! :)
I have to try this code! Thanks very much! :)