stealth bug in grid plotting code for nms clone
Blitz3D Forums/Blitz3D Programming/stealth bug in grid plotting code for nms clone
| ||
Hi citizen of blitztopia Last time I had a small bug that the eagle shoot down fast, I have new one I can't fathom what's happening. I'm still trying to make a poor's version of no man sky planet generation. I'm trying to find in which tile I'm currently hovering, I made a bunch of prototype I'm not putting together to move to the next step. Except it don't behave as intended for no reason in a code that supposedly works correctly when extracted :| I feel like the code is trolling me. Include "function library.bb" .setup Graphics3D 800,600,16,2 SetBuffer BackBuffer() ;------------------------------------------- cam = CreateCamera () PositionEntity cam, 0,0,-5 l1 = CreateLight (1) RotateEntity l1, 45,45,0 tex=LoadTexture( "testure.jpg" ) plane = CreatePlane () PositionEntity plane, 0,-8,0 EntityColor plane, 64,32,16 EntityTexture plane,tex center = CreateSphere (32) PositionEntity center, 0,-0,0 ScaleEntity center, .5,.5,.5 EntityColor center, 255,0,0 ;HideEntity center h = CreateSphere (8) PositionEntity h, 0,-0,0 ScaleEntity h, .1,.1,.1 EntityColor h, 0,255,0 ;HideEntity h ;------------------------------------------- ;put set up code here .loop hoveredface$ = " " div = 3 div2 = div*div di = div +1 Dim g( di*di ) For i = 0 To ( di*di )-1 g(i) = CreateSphere (2) ;FreeEntity g(i) Next While Not KeyDown(1) ;put runtime code and runtime 3d here controlentity(cam); function from the library, move object freely using keyboard or gamepad (reference x360), look at the library for reference PointEntity cam, center ;------------------------------------------- brush = CreateBrush() BrushFX brush, 16 entitypoint.point3d = New point3d entitypoint\x = EntityX (cam) entitypoint\y = EntityY (cam) entitypoint\z = EntityZ (cam) hoveredface = cubeface(entitypoint\x, entitypoint\y, entitypoint\z ) Select hoveredface Case "side1" : axis = 2 Case "side2" : axis = 5 Case "top" : axis = 0 Case "bottom" : axis = 3 Case "back" : axis = 1 Case "front" : axis = 4 End Select If poscell = 0 Then poscell = CreateMesh () possurface = CreateSurface(poscell) v1 = AddVertex (possurface, 0,0,0, 0,0,0) v2 = AddVertex (possurface, 1,0,0, 0,0,0) v3 = AddVertex (possurface, 1,1,0, 0,0,0) v4 = AddVertex (possurface, 0,1,0, 0,0,0) t1 = AddTriangle (possurface, v1,v3,v2) t2 = AddTriangle (possurface, v1,v4,v3) PaintSurface possurface, brush ;If hoveredface = "side2" Or hoveredface = "bottom" Or hoveredface = "back" Then FlipMesh poscell EndIf UpdateNormals poscell detectplanettile(entitypoint,poscell,axis, div,1) c0.point3d= normalizepoint3d(entitypoint); PositionEntity h, c0\x,c0\y,c0\z Delete Each point3d ;------------------------------------------- ;refresh UpdateWorld 1 RenderWorld 1 ; put 2d drawing code here showjoystate(100); 2d text display of joystick state Text 10,10, cubeface(EntityX (cam), EntityY (cam), EntityZ (cam) ) VWait:Flip False Wend ;put exit clean up code here ClearWorld So this is the main code, nothing fancy, just a bunch of visualization. - g is a structure to hold visualization of the tiled enumeration - hoveredface hold which face of a cube centered on 0,0,0 I'm hovering - poscell is supposed to visualize as a mesh the surface of tile on the surface of the sphere - h entity is simply the normalized, to the surface, position of the "ships" - Cubeface() is the custom function that detect face of the cube I'm hovering, works as intended - -- - detectPlanetTile() is where things broke down ... Function detectplanettile(entitypoint.point3d, poscell, axis, division#=10,size#=1) division = division +1 d2# = division*division S# = size/(division-1);step size ;center = 0 -> corner, center = 1 -> center offset center = 0 offset# = 0: If center = 1 Then offset = - (size) / 2 surface = GetSurface (poscell,1) close# = Abs( 2^30 ) ;detect side ;enumerate cell division ;find corner ;compute centroid ;project to sphere ;detect rightcell ;store cell within radius ;find current cell ;if not max depth -> enumerate cell division ;for each face relative to current face hovered p1.point3d = New point3d p2.point3d = New point3d p3.point3d = New point3d p4.point3d = New point3d centroid.point3d = New point3d rightcell.point3d = Null s# = 1 ;-------------------------------------------------------------- For i=0 To d2-1:;Stop; ;generate position, depend on axis xz=0, xy=1, zy=2 w1 = i w2 = i+1 w3 = i+division +1 w4 = i+division p1 = pointtile(w1, offset, division, s, axis) p2 = pointtile(w2, offset, division, s, axis) p3 = pointtile(w3, offset, division, s, axis) p4 = pointtile(w4, offset, division, s, axis) If (i Mod division) = (division -1) Then Goto skip ;If i => d2 - division Then Goto skip centroid\x = (p1\x + p2\x + p3\x + p4\x)/4 centroid\y = (p1\y + p2\y + p3\y + p4\y)/4 centroid\z = (p1\z + p2\z + p3\z + p4\z)/4 .skip p1c.point3d = p1;catspherepoint(p1) p2c.point3d = p2;catspherepoint(p2) p3c.point3d = p3;catspherepoint(p3) p4c.point3d = p4;catspherepoint(p4) centroidcat.point3d = centroid ;catspherepoint( centroid ) ; PositionEntity g(i), centroidcat\x, centroidcat\y, centroidcat\z PositionEntity g(i), p1c\x, p1c\y, p1c\z ; PositionEntity g(i), p2\x, p2\y, p2\z ; PositionEntity g(i), p3\x, p3\y, p3\z ; PositionEntity g(i), p4\x, p4\y, p4\z ScaleEntity g(i), .05,.05,.05 EntityColor g(i), 0,0,255 ;;;;;;;;;;;;; ;select right cell ; if cell = null, this cell = right, check distance to normalize ship, if distance to centroid less, then cell is new cell If rightcell = Null Then rightcell = centroidcat; VertexCoords surface, v1, p1c\x, p1c\y, p1c\z VertexCoords surface, v2, p2c\x, p2c\y, p2c\z VertexCoords surface, v3, p3c\x, p3c\y, p3c\z VertexCoords surface, v4, p4c\x, p4c\y, p4c\z Else n.point3d = normalizepoint3d(entitypoint); m# = magnitude3d(n\x - centroidcat\x, n\y - centroidcat\y, n\z - centroidcat\z) Text 0,20, m If m < close Then close# = m rightcell = centroidcat VertexCoords surface, v1, p1c\x, p1c\y, p1c\z VertexCoords surface, v2, p2c\x, p2c\y, p2c\z VertexCoords surface, v3, p3c\x, p3c\y, p3c\z VertexCoords surface, v4, p4c\x, p4c\y, p4c\z EndIf EndIf UpdateNormals poscell Next End Function I have already start to isolate where bogus data flowed, so the function catsphere() is not operational here, it's responsible for projecting the grid face toward sphere surface, so the result of this function should be a neatly align grid .... which I'm not having, I'm having a slanted grid for no reason. The responsible for enumerated the grid is PointTile(), which I visualize with the g(i) structure that hold some entity. Function pointtile.point3d(i, offset#, division#, s#, axis=0);:Stop a# = offset + ( i Mod division )*s b# = offset + ( i / division )*s Select axis ;__________________________________________ Case 0;xz floor (top) x# = a z# = b y# = s ;__________________________________________ Case 1;xy forward wall (front) x# = a y# = b z# = s ;__________________________________________ Case 2;zy side1 wall z# = a y# = b x# = s ;__________________________________________ Case 3;xz floor (bottom) x# = a z# = b y# = -s ;__________________________________________ Case 4;xy forward wall (back) x# = a y# = b z# = -s ;__________________________________________ Case 5;zy side2 wall z# = a y# = b x# = -s ;__________________________________________ End Select p.point3d = New point3d p\x = x p\y = y p\z = z Return p ;return x,y,z End Function The catch is that if I extract this function and throw it into it's own project, it works perfectly, WTF WHAT DID I MISS?: division = 3 size = 10 division = division +1 d2# = division*division S# = size/(division-1);step size ;center = 0 -> corner, center = 1 -> center offset center = 0 offset# = 0: If center = 1 Then offset = - (size) / 2 For i = 0 To d2-1 axis=1 a# = offset + ( i Mod division )*s b# = offset + ( i / division )*s Select axis ;__________________________________________ Case 0;xz floor (top) x# = a z# = b y# = s ;__________________________________________ Case 1;xy forward wall (front) x# = a y# = b z# = s ;__________________________________________ Case 2;zy side1 wall z# = a y# = b x# = s ;__________________________________________ Case 3;xz floor (bottom) x# = a z# = b y# = -s ;__________________________________________ Case 4;xy forward wall (back) x# = a y# = b z# = -s ;__________________________________________ Case 5;zy side2 wall z# = a y# = b x# = -s ;__________________________________________ End Select Plot x+10,y+10 Next WaitKey() |
| ||
Hold on, trial and error win again, I had to turn division from float to int :/ |