### Author Topic: Limitations of the 3D Starfield Engine  (Read 3339 times)

0 Members and 1 Guest are viewing this topic.

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### Limitations of the 3D Starfield Engine
« on: June 01, 2011, 02:16:45 PM »
Hey, it's time for one of these again.

I've been thinking about the 3D Starfield Engine a bit lately.  To call it "3D" is not entirely true.  For example, you couldn't render a rotating 3D box using it.  It is limited to rendering points with a radius.  That's ideal for stars, but not much else.

In order to render more complex objects made out of points, lines, and ultimately surfaces, it becomes necessary to add something else.  We must create the idea of "objects" that are essentially a series of points linked together with lines.  The lines always maintain the same relative angular relationship with each other and never change length.

Then, to rotate the object (for example), you perform a "transform" on it.

Today I've been looking at a very simple example of a transform, and trying to figure out what the calculations ought to be.

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### Re: Limitations of the 3D Starfield Engine
« Reply #1 on: June 01, 2011, 02:33:07 PM »
Oooh, there's some good stuff on the road ahead.  Wikipedia says   :D

#### Aino

• Ent
• Thank You
• -Given: 1
• Posts: 1,492
• They'll eat you next!
• Eufloria: Yes
##### Re: Limitations of the 3D Starfield Engine
« Reply #2 on: June 01, 2011, 02:39:34 PM »
Use sin/cos for the rotation, just do the angle in degree or what you want, divide it by the max(in degrees it's 360) and you get a number between 0 and 1, now you multiply with pi*2 and you get a number between 0 and pi*2. Now put that number in the sin/cos.

This is how to make the angle you want to start with, now you can do sin(original angle + new angle) to get the y axis of the rotation, do this to cos too...

I've been coding this before, heres a snippet:

Code: [Select]
`DrawX = xpos + math.cos(ModelRotation+Rotation)*(MAPIModelPartDist[i]*size)DrawY = ypos + math.sin(ModelRotation+Rotation)*(MAPIModelPartDist[i]*size)`
Where ModelRotation is the base rotation and rotation is the new.

This system might seem confusing or unuseful for your purpose though, but it's a starter

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### Re: Limitations of the 3D Starfield Engine
« Reply #3 on: June 01, 2011, 03:13:06 PM »
Thanks dude, that's a pretty useful explanation.  I think I'm going to try to do mine using a matrix, with two "for" loops to represent rows and columns.  That will make things easier in the long run because if I carry out a transform on an extremely large and complicated object, EG with 500 points and a similar number of lines connecting them, it will keep everything nice and compact.

However, I intend to spend a long time studying the maths and concepts behind it before I even attempt to start programming anything.  :>

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### Re: Limitations of the 3D Starfield Engine
« Reply #4 on: June 01, 2011, 03:53:09 PM »
Perhaps what I will do is create a matrix called "lines".  This will store a numbered list of points, and a list of other points they connect to with lines.  So points[ i][ j] would refer to point number "i", as having a connection to whatever point number is being stored in [ j].

Take a square.  That's 4 points connected with 4 lines.
The matrix would look like this:

Code: [Select]
` { 0 3 1 } { 1 0 2 } { 2 1 3 } { 3 2 0 }`

Code: [Select]
` -- store data about a square -- point data pointX[0] = 25 pointY[0] = 25 pointX[1] = 25 pointY[1] = -25 pointX[2] = -25 pointY[2] = -25 pointX[3] = -25 pointY[3] = 25 numberofpoints = 3 -- line data lines[0][0] = 3 lines[0][1] = 1 lines[1][0] = 0 lines[1][1] = 2 lines[2][0] = 1 lines[2][1] = 3 lines[3][0] = 2 lines[3][1] = 0`

Finally, to draw the object, I simply do one Draw command per declared line:

Code: [Select]
` -- for each point, draw all its lines for i = 0,numberofpoints do -- we need to calculate how many lines this point is part of linenumber = -1 for numberoflinescheck = 0,100 do if lines[i][linenumber] ~= nil then linenumber = linenumber + 1 else break end end -- we now know this point is connected to "linenumber" other points by lines for j = 0,linenumber do -- now we take each of the other points... destinationpoint = lines[i][j] -- and retrieve the coordinates of that point destX = pointX[destinationpoint] destY = pointY[destinationpoint] -- then we draw the line from the current point to the stored point. DrawLine(pointX[i], pointY[i], destX, destY, 1,1,1,1,1,1,1,1,10) end end`

One major problem of this approach is that many lines will be drawn twice.
Consider 2 points connected by a line.  Under this system, first it would draw a line from Point A to Point B.  Then, it would draw a line from Point B to Point A.  That's the same line!!  This is wasteful.  Wonder how I can optimise that..

By the way this doesn't count as programming anything yet.  :P

#### Aino

• Ent
• Thank You
• -Given: 1
• Posts: 1,492
• They'll eat you next!
• Eufloria: Yes
##### Re: Limitations of the 3D Starfield Engine
« Reply #5 on: June 01, 2011, 03:59:34 PM »

Drawing a line to the shape of a box ain't this hard, just have four points, draw from point 1 to point 2 and so on, when you reach the last point, jsut make it draw to the starting point.

But his might be wrong, as I got no idea what you're thinking of doing :/

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### Re: Limitations of the 3D Starfield Engine
« Reply #6 on: June 01, 2011, 04:03:01 PM »
Quote
Drawing a line to the shape of a box ain't this hard, just have four points, draw from point 1 to point 2 and so on

The "and so on" part is what I am doing here.
I need to have a way to record which lines connect which points.  I need to do that not for just one object, but for all possible objects.

I am developing this system with a view to using it in a 3D coordinate system (at the moment I'm just working in 2D).

#### Aino

• Ent
• Thank You
• -Given: 1
• Posts: 1,492
• They'll eat you next!
• Eufloria: Yes
##### Re: Limitations of the 3D Starfield Engine
« Reply #7 on: June 01, 2011, 04:07:29 PM »
So you're going to have a cube, and then you need to draw behind every other point too?

Well, there is no real solution I can think of... *thinking like a train*

#### Aino

• Ent
• Thank You
• -Given: 1
• Posts: 1,492
• They'll eat you next!
• Eufloria: Yes
##### Re: Limitations of the 3D Starfield Engine
« Reply #8 on: June 01, 2011, 04:12:22 PM »
By the way, why do you want to have a fully 3D starfield? Making a starmap are we?

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### Re: Limitations of the 3D Starfield Engine
« Reply #9 on: June 01, 2011, 04:18:06 PM »
Ultimately I want to draw "Fluffy" on the side of a cube, and rotate the cube.

The system I am planning will be wireframe only, though.  Surfaces will have to come much later, that's another massive leap onward once I've got wireframe down.  For surfaces, you have to calculate which surface is in front.  For surfaces that are partially behind, you have to calculate which parts of the surface are visible and which are not.  It's going to get a bit insane really..

But for now, wireframe is a good step toward a "true" 3D engine.  :>

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### Re: Limitations of the 3D Starfield Engine
« Reply #10 on: June 01, 2011, 04:30:30 PM »
If you're still not sure what I'm talking about, imagine drawing a 3D cube.

How many points are in a cube?  (ie, how many corners?)
How many lines are in a cube? (how many straight edges are there linking the corners?)

(click to show/hide)

Not all the points connect to each other though.  Each point connects to just 3 others.  If you want to store the data about such an object, you are going to have to store a few things:

1) The number of points
2) The coordinates of those points
3) For each point, which other points does it connect to with lines?

The system above is my attempt to store that information in a programmatically accessible way.

If it's STILL not clear, let me know and I will try to draw a diagram that illustrates it.  :>

#### Aino

• Ent
• Thank You
• -Given: 1
• Posts: 1,492
• They'll eat you next!
• Eufloria: Yes
##### Re: Limitations of the 3D Starfield Engine
« Reply #11 on: June 01, 2011, 04:31:06 PM »
And I think I've got the idea how to draw it...

Code: [Select]
`Init:LineX1 = {} --X1 of the lineLineY1 = {}LineX2 = {}LineY2 = {}LinesInSide = {} --How many lines are there in the squareLinesInSideIDs = {}LineDrawn = {}`
That was the initialization for the line problem, now to the code:

Code: [Select]
`draw code:function BlahBlah() LineDrawn = {} for square = 0,numsquares do -- or the amount of boxes!!! for side = 0,NumSides[square] do for line = 0,LinesInSide[side] do if LineDrawn[LinesInSideIDs[line]] ~= true then DrawLine(LineX1[LinesInSideIDs[line]],LineY1[LinesInSideIDs[line]],LineX2[LinesInSideIDs[line]],LineY2[LinesInSideIDs[line]],..(do the rest of the variables!)) LineDrawn[LinesInSideIDs[line]] end end end endend`
Hope it was clear and easily understandable

It might not work or it might, I've not tested it :/
« Last Edit: June 01, 2011, 04:34:37 PM by Aino »

#### Pilchard123

• Tester
• Old Oak
• Thank You
• -Given: 0
• Posts: 904
• Eufloria: Yes
##### Re: Limitations of the 3D Starfield Engine
« Reply #12 on: June 01, 2011, 04:51:48 PM »
What might be useful is the fact that ANY polyhedron (3-dimensional shape) has edges, vertices (corners/points) and faces linked by the equation VERTICES + FACES = EDGES + 2

Any polyGON (2D) has sides equal to the number of points.

Also: http://en.wikipedia.org/wiki/Vertex_arrangement

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### Re: Limitations of the 3D Starfield Engine
« Reply #13 on: June 01, 2011, 06:40:54 PM »
Hmm, yes... that might prove very useful..   Thanks ! :>

• Sapling
• Thank You
• -Given: 0