GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2010-03-26 11:30:06

xot
Administrator
Registered: 2007-08-18
Posts: 1,240

ds_grid_get_bilinear & bicubic

I was bored so I made these ds_grid scripts. They will return bilinear and bicubic interpolated cell values when passed a fractional grid coordinate.

The task that comes to mind for these is when using a grid as a height map in a 3D environment. I regularly see requests at GMC for smoothly navigating a height map.

ds_grid_get_bilinear(id,x,y)

Expand//  ds_grid_get_bilinear(id,x,y)
//      returns bilinear interpolation of the
//      four grid cells surrounding the given
//      fractional coordinate
//
//  A-----B
//  |     |
//  | X   |
//  C-----D
//
//  GMLscripts.com
{
    var ix,iy,fx,fy,A,B;
    ix = floor(argument1);
    iy = floor(argument2);
    fx = argument1 - ix;
    fy = argument2 - iy;
    
    A = ds_grid_get(argument0,ix,iy);
    B = ds_grid_get(argument0,ix+1,iy);
    A += fy*(ds_grid_get(argument0,ix,iy+1)-A);
    B += fy*(ds_grid_get(argument0,ix+1,iy+1)-B);
    return A+fx*(B-A);
}

ds_grid_get_bicubic(id,x,y) - depends on http://www.gmlscripts.com/script/spline4

Expand//  ds_grid_get_bicubic(id,x,y)
//      returns bicubic interpolation of the
//      sixteen grid cells surrounding the given
//      fractional coordinate
//  DEPENDENCIES: spline4(t,x0,x1,x2,x3) 
//      Catmull-Rom Spline (four knots)
//      http://www.gmlscripts.com/script/spline4
//
//  A - - B - - C - - D
//  '     '     '     '
//  '     '     '     '
//  E - - F-----G - - H
//  '     |     |     '
//  '     | X   |     '
//  I - - J-----K - - L
//  '     '     '     '
//  '     '     '     '
//  M - - N - - O - - P
//
//  GMLscripts.com
{
    
    var ix,iy,fx,fy;
    ix = floor(argument1);
    iy = floor(argument2);
    fx = argument1 - ix;
    fy = argument2 - iy;
    return spline4(fx,
                   spline4(fy,
                           ds_grid_get(argument0,ix-1,iy-1),
                           ds_grid_get(argument0,ix-1,iy  ),
                           ds_grid_get(argument0,ix-1,iy+1),
                           ds_grid_get(argument0,ix-1,iy+2)),
                   spline4(fy,
                           ds_grid_get(argument0,ix  ,iy-1),
                           ds_grid_get(argument0,ix  ,iy  ),
                           ds_grid_get(argument0,ix  ,iy+1),
                           ds_grid_get(argument0,ix  ,iy+2)),
                   spline4(fy,
                           ds_grid_get(argument0,ix+1,iy-1),
                           ds_grid_get(argument0,ix+1,iy  ),
                           ds_grid_get(argument0,ix+1,iy+1),
                           ds_grid_get(argument0,ix+1,iy+2)),
                   spline4(fy,
                           ds_grid_get(argument0,ix+2,iy-1),
                           ds_grid_get(argument0,ix+2,iy  ),
                           ds_grid_get(argument0,ix+2,iy+1),
                           ds_grid_get(argument0,ix+2,iy+2)));
}

Here are the scripts for download.
http://www.planetxot.com/download/ds_gr … icubic.gml

Should be pretty self-explanitory but here is a demonstration as well.
http://www.planetxot.com/download/ds_gr … icubic.gmk

The demo requires GM7. To use with GM8 you'll need to modify the background creation call found in User Event 1.


Abusing forum power since 1986.

Offline

#2 2011-05-10 03:21:09

Daniel
Member
Registered: 2011-05-04
Posts: 25

Re: ds_grid_get_bilinear & bicubic

This was a Life Saver.

I changed the random filler to favor higher numbers the lower you go, with a forced lower limit. Then I assigned each "mineral" in my game, to a range. The 0 to 40 range got Air, for example. The 240 to 242 range got Diamond.

I generated a random number every 4 pixels and used the Bucubic "smoothing" to fill in the rest. It's a very good level editor with very cool, fun landscapes, and it gives you amazing accuracy on rarity if you use it for generating landscapes. I can have pockets of minerals, with stone being the foundation of the world, and rarer materials encased deep inside more common materials.

Very cool. I would have been doomed without these scripts.

Offline

#3 2011-05-10 05:30:43

xot
Administrator
Registered: 2007-08-18
Posts: 1,240

Re: ds_grid_get_bilinear & bicubic

Hi Daniel! Welcome to the GMLscripts.com forums.

I'm pleased you like the scripts. It sounds like you've found a very interesting application for them. I look forward to learning more about your game and your level editor. Is there any other information you can share?


Abusing forum power since 1986.

Offline

#4 2011-05-10 14:17:29

Daniel
Member
Registered: 2011-05-04
Posts: 25

Re: ds_grid_get_bilinear & bicubic

Sure. Here's a screenshot with all the modifications I made.

mc2dlevels.png

Converting it to my game was super easy. I'm very pleased with how it turned out. It makes for a very vivid landscape full of wonder. Here's an in-game screenshot.

mc2dlevelsbig.png

Offline

#5 2011-05-10 15:13:41

xot
Administrator
Registered: 2007-08-18
Posts: 1,240

Re: ds_grid_get_bilinear & bicubic

Neat! Thanks for sharing that.

The vertical gradient you are using to influence the shape and composition of the random landscape brought to mind an article I read recently in Game Developer Magazine. In it Joshua Tippetts attempts to create a Minecraft-like world builder. The original journal entry the article was based on can be found linked below. You might find it interesting.

http://www.gamedev.net/blog/33/entry-22 … world-gen/

The explanation is fairly vague with implementation details and it has some minor technical errors,  but it's not a bad read.


Abusing forum power since 1986.

Offline

#6 2011-05-10 22:18:27

Daniel
Member
Registered: 2011-05-04
Posts: 25

Re: ds_grid_get_bilinear & bicubic

I stumbled across the same article. It was great.

I soon found that the same map style simply won't work in 2D, and needed something better. I was using linear interpolation initially. Cubic interpolation added a certain bang to the terrain, making it more fun for 2D play. I know with the right kind of noise I could do it (noise can do anything!), but this method was much simpler as I didn't have to generate a noise map that smoothly matched up to the smoothmap in another adjacent chunk. This method I could just throw random values into the unknown territory and it would still look right.

Offline

#7 2011-05-11 01:09:42

xot
Administrator
Registered: 2007-08-18
Posts: 1,240

Re: ds_grid_get_bilinear & bicubic

Just think of it as single-octave aperiodic noise. Sounds fancy.


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB