grid...

Blitz3D Forums/Blitz3D Beginners Area/grid...

mindstorms(Posted 2006) [#1]
There ought to be a better way to snap to a grid than to do it this way:



edit: I think this is better...uses some globals and a type...the idea is that the grid starts at -width/2 and -depth/2...


Any ideas?


b32(Posted 2006) [#2]
I think what you are doing is basically right, the main idea would be this:
snapx = (x + (size/2) / size) * size
snapz = (z + (size/2) / size) * size


GfK(Posted 2006) [#3]
Try this - written in Blitzmax but should work in Blitz3D (might need to replace 'DrawOval' with 'Oval' though):
Graphics 800,600

Const xStep% = 32
Const yStep% = 32

While Not KeyDown(key_escape)
	Cls
	drawgrid()
	drawcursor()
	Flip
Wend

Function DrawGrid()
	Local x%
	Local y%
	For X = 0 To GraphicsWidth() Step xStep
		For Y = 0 To GraphicsHeight() Step yStep
			DrawOval x-1,y-1,2,2
		Next
	Next
End Function

Function drawCursor()
	Local mX = MouseX()
	Local mY = MouseY()
	Local x% = mX - (mX Mod xStep)
	Local y% = mY - (mY Mod yStep)
	If mX Mod xStep > xStep/2
		x = x + xStep
	EndIf
	If mY Mod yStep > yStep/2
		y = y + yStep
	EndIf
	DrawOval x-4,y-4,8,8
End Function



mindstorms(Posted 2006) [#4]
Thanks, they both work...Which one is faster?

(it probably is a very small difference, but any speed bonus is welcome :)


GfK(Posted 2006) [#5]
Dunno, I don't have Blitz3D installed so I could test it, but it'd be in Blitzmax.

Write a loop that runs out after, say, 10 seconds, and count how many times the code runs in that time.

You might get false results from mine though as its drawing a lot of ovals. So take that out if you want a speed test based purely on the maths.


mindstorms(Posted 2006) [#6]
Turns out they are about the same on my computer...Had to go for a minute each before there was a difference of more than 1 loop....
Thanks everyone.


Jsoren(Posted 2006) [#7]
@ Gfk: you code worked nicely, a few things needed changing, and it seemed to flicker a little, not sure if is my graphics card, or the code. i fleshed it out alittle, and used a type. it seems to work well (and dosent flicker):

Graphics 640,480
;x and y steps
Const x_step=32
Const y_step=32
;type tile
Type tile
	Field x,y
End Type
;create grid
Dim grid.tile(GraphicsWidth(),GraphicsHeight())
;set buffer
SetBuffer BackBuffer()


;;MAIN LOOP;;
While Not KeyDown(1)
	Cls
	makegrid()
	drawcursor()
	Flip
	Wend
End
;;END;;

;;draw the grid function;;
Function makegrid()
For x=0 To GraphicsWidth() Step x_step
	For y=0 To GraphicsHeight() Step y_step
		grid(x,y)=New tile
		grid(x,y)\x=x
		grid(x,y)\y=y
		Oval x,y,2,2
	Next
Next
End Function

;;draw the cursor function;;
Function drawCursor()
	mX = MouseX()
	mY = MouseY()
	x% = mX - (mX Mod x_Step)
	y% = mY - (mY Mod y_Step)
	If mX Mod x_Step > x_Step/2
		x = x + x_Step
	EndIf
	If mY Mod y_Step > y_Step/2
		y = y + y_Step
	EndIf
	Oval x-4,y-4,8,8
End Function


P.S. plz tell me if it works 4 u :)


mindstorms(Posted 2006) [#8]
there is a huge memory leak, because each time you are creating new tiles and not deleting the old ones. In fact, there is no need for the type at all because you are simply taking the x and y values.


b32(Posted 2006) [#9]
The fastest way is using SHR instead of / and SHL instead of * The disadvantage is that you can only divide/multiply with a power of 2.


Jsoren(Posted 2006) [#10]
@mindsotrms: im using types, because although this is a stand alone program, i presume it would be for use in an actual game, and usually a i figure you going to store bits of data on each tile (i know i do), not just using it as a grid (otherwise you could just texture a plain).

and oops...yeah, i should of cought that leak.....

JS


mindstorms(Posted 2006) [#11]
Exactly why my code has the globals and the array type :)


mindstorms(Posted 2006) [#12]
If I could ask another question:

I now have the 3 levels of pathfinding working individually, but need an idea as to when to use the 3 levels (small, med, large).

I am thinking that it should start in large until it is about 70-75% done, then switch to medium until it is about 95% and then go to small. If then the object runs into a collision their should be a second function that takes the next node and finds a path to it with a lower grid setting, inserting it into the first path...I can't figure out an efficient way to do this, and was also wondering if there were other cases I needed to account for.

The way the existing pathfinding works is you specify a startx/z and an endx/z and a size, and it finds a path to that size. Do I need to modify this, or do I just need to create a function that decides which grid size to use?


mindstorms(Posted 2006) [#13]
heres the pathfinding3 file:

note that the find_path is just a test to see if the levels work with each other.

here is the main...slightly disorganized



GfK(Posted 2006) [#14]
Gfk: you code worked nicely, a few things needed changing, and it seemed to flicker a little
Yep, my bad. I set a 2D graphics mode and assumed that because its Blitz3D it uses the BackBuffer by default. I haven't used Blitz3D for over a year now, and forgotten that the BackBuffer is only used by default in 3D graphics modes, hence the flicker (which didn't actually show up on my PC anyway).


mindstorms(Posted 2006) [#15]
I'm still looking for solutions to this...It is still not working any faster than just using the small or medium modes...


Subirenihil(Posted 2007) [#16]
Nicely done! :)