Author Topic: Coding ninjas, I needs your halp !  (Read 11158 times)

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Coding ninjas, I needs your halp !
« on: June 14, 2011, 11:49:32 PM »
I can't get my head around the maths for this.

This function is passed a vertex ID.  It is supposed to check each face in turn and figure out if the vertex is inside it or not, as described with extensive diagrams here.  If it's inside the face, the vertex does not get rendered (because it is "hidden" by the face).

If the vertex is found to be inside a face, it's also supposed to check if the vertex is further away than the face.  That part isn't implemented yet, so the expected behaviour should be for the engine to hide any vertices which are inside a face they are not themselves part of.

It doesn't work - the vertices behave strangely depending on what exactly I've changed... sometimes they all hide all the time, other times they reappear for a fraction of a second if the camera is in _just_ the right position...  I can't figure out what I'm doing wrong.  Any halp? :>

Code: [Select]
function VertexToPlaneCheck(vertexID)
-- (vertexID is the ID the of the vertex being checked to see if it's behind this plane)

for faceID = 0,numberoffaces do
-- detect numberoffedges
numberofedgesinthisface = -1
for i = 0,100 do
if face[faceID][i] ~= nil then
numberofedgesinthisface = numberofedgesinthisface + 1
else
break
end
end



passededges = 0
-- this counter is used to check how many edges are passed through to the left of this point
for faceEdgeNumber = 0,numberofedgesinthisface do
-- for each edge in this face
edgeID = face[faceID][faceEdgeNumber]
-- use the ID of the selected edge
v1 = edgefrom[edgeID]
v2 = edgeto[edgeID]
-- set v1 and v2 equal to the vertices that join this edge
if v1 ~= vertexID and v2 ~= vertexID then
-- make sure the point passed to us in this function is not the same point mentioned in v1 or v2...
slope = (vertex2dX[v2] - vertex2dX[v1]) / (vertex2dY[v2] - vertex2dY[v1])
-- first calculate the slope of the line formed by the two coordinates

-- we need to find the X-value where the line drawn between v1 and v2 crosses the axis of vertexID
-- b = -mx / y



Yintercept = (-slope * vertex2dX[v1]) + vertex2dY[v1]
-- this is the Y-intercept

intercept = (-Yintercept / slope)
-- this is the x-intercept

vertexXintercept = (vertex2dY[vertexID] - Yintercept) / slope
-- ah fuk knows!

-- intercept is equal to the X-value where the line crosses the P line

if vertex2dX[v2] <= vertex2dX[v1] then
minv = vertex2dX[v2]
maxv = vertex2dX[v1]
else
minv = vertex2dX[v1]
maxv = vertex2dX[v2]
end

if vertex2dX[vertexID] > vertexXintercept then --and vertexXintercept >= minv and vertexXintercept <= maxv then
passededges = passededges + 1

end
end
end


if math.floor(passededges / 2) == math.ceil(passededges / 2) then
-- the vertex is inside a face

-- is this vertex behind the face?
draw = false

end

end

end

Level file attached also.

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Coding ninjas, I needs your halp !
« Reply #1 on: June 15, 2011, 01:10:52 AM »
You've managed to declare faces now then?

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Coding ninjas, I needs your halp !
« Reply #2 on: June 15, 2011, 02:02:47 AM »
Yes, check the Cube function in the file attached to the original post and you'll see how they are laid out.

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Coding ninjas, I needs your halp !
« Reply #3 on: June 15, 2011, 05:54:54 PM »
Oh wait... I think I might know what's wrong...

The problem lies in the fact that vertex2dX[ i] is treated as a leveldraw object, but compared against vertices with screendraw coordinates.

Perhaps, to fix it, I must use different variable names for the screendraw coordinates...
Maybe the function was working fine after all.  Though I may have screwed the maths for it now by fiddling around with it so much..

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Coding ninjas, I needs your halp !
« Reply #4 on: June 15, 2011, 06:57:51 PM »
I think that is working, but the maths still is not right.

Here's my current problem:


annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Coding ninjas, I needs your halp !
« Reply #5 on: June 15, 2011, 07:49:12 PM »
x = (y - b) / m

Where M and B are known, and Y = vertex2dY[vertexID]
Is that correct??

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Coding ninjas, I needs your halp !
« Reply #6 on: June 15, 2011, 08:43:10 PM »
Ok to expand on that a little..

Slope/point form for a line is:
y = mx + c


I want to find the slope (m) of a line with coordinates v1 and v2.
I use this formula:

slope = (vertex2dX[v2] - vertex2dX[v1]) / (vertex2dY[v2] - vertex2dY[v1])


Then I want to find the Y-intercept (c) of the that same line.
I use:

Yintercept = (-slope * vertex2dX[v1]) + vertex2dY[v1]


Finally I wish to know at what X-coordinate the line will be at when it passes through a specific value of Y.  The specific value of Y is referred to as vertex2dY[vertexID].  The desired X-coordinate shall be called vertexXintercept.
So,

vertexXintercept = (vertex2dY[vertexID] - Yintercept) / slope



Is that correct?

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Coding ninjas, I needs your halp !
« Reply #7 on: June 15, 2011, 10:13:51 PM »
Moreover, is the resulting X-coordinate somewhere between the From and To x-coordinates of this edge?
If it is, then the intersection occurs somewhere along the length of the line (rather than a theoretical intersection that happens further down the line than either of the given vertices)

That would mean that the passededges variable should be incremented.


Here's another diagram illustrating this functionality:


Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Coding ninjas, I needs your halp !
« Reply #8 on: June 16, 2011, 12:51:28 AM »
Your gradient equation is wrong. It should be
slope = (vertex2dY[v2] - vertex2dY[v1]) / (vertex2dX[v2] - vertex2dX[v1])

Note that the X and Y parts are swapped. That is probably what is causing your problem.

Ideally, Eq2 should be

Yintercept = vertex2dY[v1] - (slope * vertex2dX[v1])

Although yours says exactly the same, that's kinda...tidier.

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Coding ninjas, I needs your halp !
« Reply #9 on: June 16, 2011, 12:59:37 AM »
Sir Pilchard, you are a genius.  Working! :D


Attached.  :>

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Coding ninjas, I needs your halp !
« Reply #10 on: June 16, 2011, 01:09:22 AM »
Incedentally,

-(X1-X2)/(Y1-Y2) gives you the gradient of the line that is perpendicular to the line with gradient (Y1-Y2)/(X1-X2)    (This is usually called the normal). The minus sign is deliberate.

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Coding ninjas, I needs your halp !
« Reply #11 on: June 16, 2011, 02:06:00 AM »
Ok there is still a problem somewhere.
One vertex, the one at the front at the top-right, does not display.
It's not specific to that vertex, it happens on all the vertices when they reach a certain rotation.

Hmm, wonder what that could be...

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Coding ninjas, I needs your halp !
« Reply #12 on: June 16, 2011, 02:07:35 AM »
Wait no... it doesn't _always_ happen.
Weird.
I need to sit and watch this for a while to figure out what is going on...

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Coding ninjas, I needs your halp !
« Reply #13 on: June 16, 2011, 03:41:54 AM »
Ugh... I wish I was of help, but I'm just sitting and wondering what is happening with my eyes when I watch the damn complicated code D:

*Clearing my mind to try understand better*

EDIT: Nope, impossible to understand it...
« Last Edit: June 16, 2011, 03:52:54 AM by Aino »

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Coding ninjas, I needs your halp !
« Reply #14 on: June 16, 2011, 05:00:29 AM »
Aino, do you understand what vertices, edges and faces are?  It's necessary to understand those concepts in order to have any hope of understanding what the code does, and what it's supposed to do.  :>

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Coding ninjas, I needs your halp !
« Reply #15 on: June 16, 2011, 05:04:06 AM »
Verticles are the points in the 3D objext, Edges are the lines stretching from the erticles as the editor chooses and the faces are... Uhhm, need help here :/'

EDIT: I saw that Faces is "2d planes"(mind explaining that please :)) which obstructs anything behind it, just like the stars on the asteroids, but annikk, I think the coding is more expensive than just drawing them in cycles... But you learn a few things though :P

EDIT 2: Wildly guessing planes are a filled area(like a trinagle, or commonly 2 triangles forming s square(I guess thats it...)) which tells what points are obstructed...
« Last Edit: June 16, 2011, 05:07:40 AM by Aino »

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Coding ninjas, I needs your halp !
« Reply #16 on: June 16, 2011, 05:32:15 AM »
Imagine a cube:

Edges are...well, the edges.
Vertices are the corners.
Faces are the sides - the flat bits.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Coding ninjas, I needs your halp !
« Reply #17 on: June 16, 2011, 06:00:55 AM »
Imagine a cube:

Edges are...well, the edges.
Vertices are the corners.
Faces are the sides - the flat bits.

As I thought, thanks for assuring me :)

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Coding ninjas, I needs your halp !
« Reply #18 on: June 16, 2011, 06:23:25 AM »
Yes exactly that.

I will talk a bit about what the objective here is, and also how these pieces of data are stored.

Imagine you know all the coordinates to draw the lines to/from in order to make a nice looking 3D cube for a given camera position.
If this were a real box, we wouldn't be able to see the lines of the back of the cube, because they are hidden away behind it.  The thing that blocks our view is the closest Face of the cube - it is solid and so we cannot see through it.

The objective here is to create that look, as if the sides of the cube are solid.  The first step thing that we need to do, then, is to figure out a way to say where the faces are.
The way I chose to do this is to take a bunch of edges - at least 3 - and say "these edges form a face".  

So in my array... well, actually it's a matrix..  here is how I am organising things:


Code: [Select]
face[2][0] = firstedge + 3
face[2][1] = firstedge + 4
face[2][2] = firstedge + 5

This is a typical face entry.  "face" is the name of the matrix.  The "2" in the first set of square brackets means that this is a declaration for Face ID 2.  We're numbering the faces, yea..?  Ok, and the 0, 1, and 2 in the second square brackets, that is simply the ID numbers used to refer to the different slots within that face... it's like an ID number for an edge's membership to a particular face.
The values that we are storing in each "slot" of the face matrix, correspond to the ID numbers of the Edges that are participating in each face ID.

So we have ID numbers for each of these:

Vertex
vertex2dX[ID]

Edge
edgeFrom[ID]
edgeTo[ID]

Face
face[ID][0]

Edge membership to a face
face[0][ID]


Let me know if there is anything else specific that you're having trouble grasping :>
« Last Edit: June 16, 2011, 06:31:15 AM by annikk.exe »

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Coding ninjas, I needs your halp !
« Reply #19 on: June 16, 2011, 06:41:23 AM »
The current burning topic (for me anyway) is the vertex visibility bug.

The bug means that vertices 1, 2, 5 and 6 erroneously do not render when they are at a certain rotation - roughly a 90 degree chunk of the east.  So when those vertices are facing east, they disappear - even though they are not inside a face.

It only affects those vertices.  It always happens regardless of rotation.  And it's always at the same positions.


Now, I have discovered it is something with this line:

Code: [Select]
if vertex2dX[vertexID] > vertexXintercept and vertexXintercept >= minv and vertexXintercept <= maxv then
Changing the test to "less than" instead of "more than", means that the vertices erroneously disappear in the west, not the east!
Strange...  I hope there is not some massive conceptual shortcoming here that I had not thought of...

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Coding ninjas, I needs your halp !
« Reply #20 on: June 16, 2011, 06:59:55 AM »
Fixed.

Changed this:
Code: [Select]
else
partoftheface = true
end

to this:
Code: [Select]
else
partoftheface = true
break
end


All faces are active and it's working with expected behaviour ! :>
Take a look at the attached.

Understand what you are seeing here; at the moment, it doesn't check whether a vertex is further away than the face... it just checks whether the vertex is "inside" the face.  So it doesn't look correct yet.... but it's a major start to proper vertex visibility.

Time to go research plane geometry !