Jump to content


Photo

Furthest point on grid


  • This topic is locked This topic is locked
5 replies to this topic

#1 lolslayer

lolslayer

    GMC Member

  • GMC Member
  • 242 posts
  • Version:GM:Studio

Posted 17 March 2016 - 01:57 PM

Hello everybody

 

I'm working on a voxel based fake 3D firstperson engine in Game Maker. To do this I've got one object that draws the voxels based on the angle compared to the camera angles and the distance to the camera. Also, the voxels are drawn on a grid

 

The only problem I've got so far is the fact that I can't set the depth in the draw event, so I've got to draw the voxel in the right order that the nearest voxels are drawn later on then the furthest points on the grid.

 

Is there a way that I can get the furthest X and Y on a grid, and then repeating this but every time I draw a voxel I get X and Y of the grid one spot nearer as the last one I got?

 

 

Here's a sexy pic if you want to see the engine in action ;)

g3xCQfX.png


  • 0

#2 Strawbry_Jam

Strawbry_Jam

    Likes Toast

  • GMC Member
  • 345 posts
  • Version:Unknown

Posted 17 March 2016 - 02:55 PM

How does the code for your grid look? What size is the grid?
  • 0
Spoiler

#3 lolslayer

lolslayer

    GMC Member

  • GMC Member
  • 242 posts
  • Version:GM:Studio

Posted 17 March 2016 - 03:31 PM

How does the code for your grid look? What size is the grid?

This is the code I use to draw the scene:

repeat(20){
    repeat(20){
        cz = grid[# cx/10,cy/10]
        
        sc = 1/max(point_distance_3d(cx,cy,cz,obj_camera.x,obj_camera.y,obj_camera.z),1)*10000
        xd = room_width/2 + angle_difference(obj_camera.dir,point_direction(obj_camera.x,obj_camera.y,cx,cy)) * room_width/2/45
        yd = room_height/2 + angle_difference(obj_camera.zdir,point_direction(0,obj_camera.z,point_distance(obj_camera.x,obj_camera.y,cx,cy),cz)) * room_height/2/45
        
        draw_set_colour(make_colour_rgb(cz,cz,cz))
        draw_rectangle(xd-sc,yd-sc,xd+sc,yd+sc,false)
        
        //draw_set_colour(c_red)
        //draw_rectangle(cx-2,cy-2,cx+2,cy+2,false)
        
        cx += 10
    }
    cy += 10
    cx = 0
}

cx = 0
cy = 0

Each part of the grid is 10x10 px

 

So what I want to do is to check what point on the grid is the furthest on the camera, and then go through all point and at each point I choose the one that is one step closer to the camera


  • 0

#4 Xer0botXer0

Xer0botXer0

    GMC Member

  • GMC Member
  • 770 posts
  • Version:GM:Studio

Posted 18 March 2016 - 07:13 AM

Im not entirely sure how grids work, how ever I assume you can draw a line from camera in a direction and where the line stops colliding with a sprite or so, then the line stops being drawn, and then you've got a distance to the furtherest sprite or point in the grid.

 

Perhaps that wont work as perhaps those aren't sprites.

Im not sure if this is the best way how ever you could use arrays to give those grid points properties. such as x/y/z position ? which is stored for each point when that sprite/whatever is generated.

 

So the two ways Im pointing out is that one may find a position by drawing a line and finding the last point where a collision was found, the second is giving each grid point properties that are declared at creation.

 

You could then with the second method loop through the points to see which is the furtherest, or rather just loop through their x/y/z properties, which ever is required, finding the highest one of them all, you could use direction or a range.

 

Just thought I'd add my thoughts here.


  • 0

//=====================

Good bye, Farewell GMC! :D

See you all in the next TIER!

======================\\


#5 Strawbry_Jam

Strawbry_Jam

    Likes Toast

  • GMC Member
  • 345 posts
  • Version:Unknown

Posted 18 March 2016 - 07:33 AM

Probably the easiest way is to put the grid cell in a priority queue with the priority being distance from camera. Then you can just go down the queue with your points. Any details from the grid you can access from the value in the queue.

Value = gridx + gridy * gridwidth
Priority = distance from camera

To reverse
gridx = value mod gridwidth
gridy = value div gridwidth
  • 0
Spoiler

#6 lolslayer

lolslayer

    GMC Member

  • GMC Member
  • 242 posts
  • Version:GM:Studio

Posted 18 March 2016 - 09:13 AM

Probably the easiest way is to put the grid cell in a priority queue with the priority being distance from camera. Then you can just go down the queue with your points. Any details from the grid you can access from the value in the queue.

Value = gridx + gridy * gridwidth
Priority = distance from camera

To reverse
gridx = value mod gridwidth
gridy = value div gridwidth

Yeah, I thought of that aswell, but I think that that is really calculation intensive so I hoped that somebody had some kind of algorithm for this :/

 

Im not entirely sure how grids work, how ever I assume you can draw a line from camera in a direction and where the line stops colliding with a sprite or so, then the line stops being drawn, and then you've got a distance to the furtherest sprite or point in the grid.

 

Perhaps that wont work as perhaps those aren't sprites.

Im not sure if this is the best way how ever you could use arrays to give those grid points properties. such as x/y/z position ? which is stored for each point when that sprite/whatever is generated.

 

So the two ways Im pointing out is that one may find a position by drawing a line and finding the last point where a collision was found, the second is giving each grid point properties that are declared at creation.

 

You could then with the second method loop through the points to see which is the furtherest, or rather just loop through their x/y/z properties, which ever is required, finding the highest one of them all, you could use direction or a range.

 

Just thought I'd add my thoughts here.

The grid points are all in a grid with the same distance from eachother on the x and y axis, the z is stored in the grid[# ] ds_grid

 

I know that i can loop through all of the points, then add them to a priority and then draw them, but the problem with this is that it will take 2 loops to calculate the depth and the position on the screen, I hope to find a way that I can get the cx and cy variables to go through the grid while calculating the position of the voxel on the screen


Edited by lolslayer, 18 March 2016 - 09:29 AM.

  • 0