Qbasicnews.com

Full Version: 3D engine math and a LOT of stuff
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
I want to do a 3d engine.

-No wait! Don't go away for that, now read here.
The engine will be based on three major things:

A "floor".

Static or moving objects.

A camera.

The floor wil be created by a two-dimensional matrix of vertices
(the dimensions in the matrix is x and z values of the matrix
(you multiplicate it with n) and the value stored in one of the
"boxes" in the matrix is the y value of the vertex) like this:
Code:
*-*-*
|\|\|
*-*-*
|\|\|
*-*-*

* vertex

|\- edge of polygon(s)

This wil create the floor.

Static objects should be trees, bushes, maybe even simple houses,
etc. moving objects will be players etc. (My dream is to create a lan-based 3d game but I will not start with 500 polygons for each player and I will not start with the game but the engine with VERY low-poly objects like first: a floor, second: a floor and a box, ...)

The camera is the player and it can be moved in x,y,z directions
turned 360 degrees and looking up and down =rotating around the
y axis and z axis (?)

The part that involves you is that I want to know the formulas
for this rotating (I know there's a lot of 3D tuts "learn rotating your
own box" but I think it's better to talk to you face to face (lol? anyone?))

Also: What method should I use to know what polygon to draw
before I draw that one?

To end: Big project? h*ll yeah, but I really want to learn this
and I will let it take its time and see it as "education" instead
of a game project that must be finished soon.

Thanks in advance!

PS: I will code this in C++ probably with some opengl lib, for example:
GLUT
Quote: (My dream is to create a lan-based 3d game...)

Mine too. I think there's ENORMOUS potential for a high speed Sonic Hedgehog like net game. Imagine CTF on a huge map like BF1942 where every player moves at the speed of the jeep.

Of course, sometime in the future we may see true voxel-based terrain. [Image: drool2.gif]

Quote:PS: I will code this in C++ probably with some opengl lib

Same. But I just don't really have the energy or mathematical skills to get anywhere serious with it. I'm going to wait until the end of high-school
voxel? oh dear, level files on 500mb
Quote:voxel? oh dear, level files on 500mb

Well - like with bitmaps, you can do "cubic tiles".
Well, some thoughts:

vertex_x_camera=vertex_x_world*cos(camera_xz_angle)

vertex_z_camera=vertex_z_world*sin(camera_xz_angle)

vertex_y_camera=vertex_y_world*cos(camera_y_angle)


This is not some really working math but it may show in wich
direction I think. I do have a feeling for how I think it should be
done but when I sit down to write it dow my brain goes just bonk.
Well here's a 3D tutorial, but not about polygons and such.
But it might be a good idea to learn it anyway.
http://www.qbasicnews.com/tutorials.php?...=view&id=9
Quote:voxel? oh dear, level files on 500mb

Worms 3 is using a combo engine... so the terrain is fully destructible like in Armaggeddon, but still manageable. Check the screenies, they're pretty sweet. (Big voxels, but they have it working...)
I'm thinking of using the equations in the tutorial blue_keyboard
showed me, but I'm planning to add some sort of
planes/spaseships (like in BF1942) (sooner or later) (and also other
vechiles) and in a plane, when it turns it also rotates around a Z axis
so since the math in the tut only covers rotations around the X and Y axis
and I don't have the mathematical knoledge to add a Z axis rotation
equation part to the existing equation so I ask you to help me out and
Code:
X2 = -x * s!(theta) + y * c!(theta)
Y2 = -x * c!(theta) * s!(phi) - y * s!(theta) * s!(phi) - Z * c!(phi) + p

and somewhere on this equation something like

*s!(gamma)
*c!(gamma)

should be added I think...
Here.I managed to get your equations working. The P somehow Y-relocates your Center.

And the only problem I have with your equation is you CAN'T rotate the X and Y axes independentlyof each other.




Code:
DECLARE SUB LoadSphere (SphereRadius%, Slices%, Bands%, ModelArray() AS ANY)

DEFINT A-Z

TYPE Point3d                                        'type for each 3D point
        X       AS INTEGER                          'x coord (horizontal)
        Y       AS INTEGER                          'y coord (vertical)
        Z       AS INTEGER                          'z coord (into the screen)
        P AS INTEGER                                'dist from center of object
END TYPE



REDIM SHARED Sphere(1) AS Point3d      'Model

CONST PI = 3.141592
CONST Xcenter = 160, Ycenter = 100, Zcenter = 256

SphereRadius = 80
Slices = 10
Bands = 20

LoadSphere SphereRadius, Slices, Bands, Sphere()


SCREEN 7
CLS


XYang = 0: Zang = 0
XYangleRot = 1: ZangleRot = 1

DO

   SCREEN 7, 0, 2, 0: PCOPY 1, 2


  FOR I = 1 TO UBOUND(Sphere)

    ox = Sphere(I).X            'Load original coords  
    oy = Sphere(I).Y
    oz = Sphere(I).Z
    P = 1                       'Rho???
    Theta! = XYang * PI / 180   'Convert to Radians
    Phi! = Zang * PI / 180

    Xrot = -ox * SIN(Theta!) + oy * COS(Theta!)
    Yrot = -ox * COS(Theta!) * SIN(Phi!) - oy * SIN(Theta!) * SIN(Phi!) - oz * COS(Phi!) + P
    Zrot = -ox * COS(Theta!) * COS(Phi!) - oy * SIN(Theta!) * COS(Phi!) + oz * SIN(Phi!)
  
     IF (Zrot + Zcenter) <> 0 THEN      'Division by 0 error check
       FlatX = 256 * (Xrot / (Zrot + Zcenter)) + Xcenter
       FlatY = 256 * (Yrot / (Zrot + Zcenter)) + Ycenter
       PSET (FlatX, FlatY), I AND 15
     END IF


  NEXT I

  ' Increment the Angles
  XYang = (XYang + XYangleRot) MOD 360
  Zang = (Zang + ZangleRot) MOD 360

        SCREEN 7, 0, 0, 0: PCOPY 2, 0: PCOPY 1, 2


LOOP UNTIL INKEY$ <> ""

CLS
SCREEN 0

END

SUB LoadSphere (SphereRadius, Slices, Bands, ModelArray() AS Point3d)

REDIM ModelArray((Slices * Bands))  AS Point3d

I = 0
  FOR SliceLoop = 1 TO Slices
      Phi! = PI / Slices * SliceLoop
      FOR PPSLoop = 1 TO Bands
          Theta! = 2 * PI / Bands * PPSLoop
          ModelArray(I).X = INT(SphereRadius * SIN(Phi!) * SIN(Theta!))
          ModelArray(I).Y = INT(SphereRadius * SIN(Phi!) * COS(Theta!))
          ModelArray(I).Z = INT(SphereRadius * COS(Phi!))
          I = I + 1
      NEXT PPSLoop
NEXT SliceLoop

END SUB
Pages: 1 2 3