Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1075933 Posts in 44152 Topics- by 36119 Members - Latest Member: Royalhandstudios

December 29, 2014, 04:22:00 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Mid-Point distance map generation
Pages: [1]
Print
Author Topic: Mid-Point distance map generation  (Read 829 times)
tergem
Level 1
*

It's a pony!


View Profile
« on: June 28, 2011, 08:24:03 AM »

I feel like such an ID-10t, I can't comprehend the mid-point distance algorithm. I understand the concept, but I don't know how it would translate into code. I've looked at the tutorials, but I still feel like I don't understand it. I don't know what it is today, but my mind is slow.

So please may I be educated?
Logged

Games made so far (completed):Spike teh dodge, Unnamed puzzle game, Galaga clone, Generic Top-Down Shooter, overly simplistic business simulator In dev: Platformer!
civicdude95
Level 0
**



View Profile WWW Email
« Reply #1 on: June 29, 2011, 10:55:56 AM »

I used it in my 2D side on game and wrote about it
http://jefflamoureux.blogspot.com/2011/06/terrain-generation-using-midpoint.html
Check out the link I posted in the blog post. It helped me out a lot.
Logged

Jay_PC
Level 1
*


It, Happens...


View Profile Email
« Reply #2 on: June 29, 2011, 08:18:54 PM »

I Imagine it instead of having a line by default start with an array of 2 points then avrage the 2 and add a random value to it, random multiplied by the roughness constant

Code:
{0,0}
Becomes:
{0,3,0}
Becomes:
{0,2,3,1,0}
{0,1,2,3,1,1,0}
{0,2,1,2,2,3,3,2,1,0,1,0}


Using http://www.gameprogrammer.com/fractal.html I get somthing like this:
Code:
public float[] createHeightMap(int finalArrayLegnth, float startingRand, float roughConstant, float minHeight){
float[] finalHeightMap = new int[arrayLegnth];
float[] startingArray = {minHeight, minHeight};
boolean isAddZero = false;
int currentIndex = 0;
float[] tempAarray;
int randAmount = startingRand;
//some kind of legnth checking would have to go here because the array ends up having to be an odd number unless you clip the last number or something

int currentLegnth = 2;




while(tempArray.legnth()<= finalArrayLegnth){
//expand the array first
for(int i=0; i<=currentLegnth+(currentlegnth-1); i++){
tempAarray = new int[currentLegnth+(currentlegnth-1)];

//this alternates adding a number from currentArray and a 0
if(isAddZero){
 tempArray[i]=0;
 isAddZero = false;
}else{
 tempArray[i]=startingArray[currentIndex];
 currentIndex++;
 isAddZero = true;
}

}

//then you have to actually displace the "empty" points
for(int i=0; i<=tempArray.legnth();i++){
if(tempArray[i]==0){
 tempArray[i] = ((tempArray[i-1]/tempArray[i+1])+Math.Random(randAmount));
}
}

//lastly you have to Decrease the random amount by the constant
randAmount = randAmount*randConstant;

}

//then return the Finished height map

return tempArray;
}

note the only problem to my method is that it will never have points that are 0 since the method replaces anything that is 0 with an average of the 2 numbers next to it.

the only other critizizm is that the array WILL be a odd number since after the first pass it goes 2,3,5,9,17,33 and so on, if you need it to be even clip the end until its the right legnth Ex, if you need 20 exactaly make 33 and cut the last 13 off the end.

Also, I wrote this on the fly, and I don't guarantee it will work copy and paste. but Try and understand what I did.

1)Expand the array inserting 0 in between every number in the array.
2)average the Numbers around the 0's in the Array and add a random number
3)decrease the random amount by the roughness constant.
4)repeat 1-3 until the array is the desired length(or bigger and clip it)

« Last Edit: June 29, 2011, 08:27:38 PM by Jay_PC » Logged

civicdude95
Level 0
**



View Profile WWW Email
« Reply #3 on: June 30, 2011, 05:27:25 AM »

Using http://www.gameprogrammer.com/fractal.html I get somthing like this:

Lol I like the link you give there, that's exactly the same place I went to learn about MDA.
Logged

Jay_PC
Level 1
*


It, Happens...


View Profile Email
« Reply #4 on: June 30, 2011, 10:42:17 PM »

Using http://www.gameprogrammer.com/fractal.html I get somthing like this:

Lol I like the link you give there, that's exactly the same place I went to learn about MDA.

Hahahaha I used that at first, page didn't make sense at first, but nowhere could I find a more specific answer. I couldn't find any source, and what I did find was Over complicated. Hopefully my above code will help aid someone.

I eventually used that to make 2D heightmaps when I was working on a rougelike like a year ago. and the results were:

(note these were generated by a script and the colors were added letter by letter, these may load slow...)
http://shadowphantom.webs.com/Results24320111.html
http://shadowphantom.webs.com/Results1779917981.html
http://shadowphantom.webs.com/Results1761624888.html
http://shadowphantom.webs.com/Results1068689589.html

These also used a Smoothing pattern that did this
Code:
T=Trees ~=water .=grass

Base, no Smoothing

......T....T~~~..T~~~~TT~~TT.......~.....T.....T.
..........~~~~~~..~~...~TT.T....TT...............
........~~.T~~T~~..T.~.~........TT.~.............
..T...~~~~..~.~~.~~.T.~..T...T...............T.T~
.....~..~...~~~~~~~......T.T.T.....T..........T.T
.~~.T..~.~...~~~~~~~~~...........T.......T.......
...~...T.~.T.~~~~.~T~..............T.T...........
..~~~....~..~.~.~~~................T..TT........T
..~...~~..~.~~~~.T..T.......T....T...............
T~~.TT~......T.~~............~...T...............
.~TT....T...T...TT......T...~~~..................
...........~.~.~~............T.....T....T........
.~.........TTT~~~....T.....T.~...T...........T...
~~~...........~.~.T.....T........T.T.............
~TT...........~..T........T.~.T...T..............
...............T......~.......~...~..T......T..T.
...........T...~...T.~~..TT..~~...T..............


after 1 pass
......T....~~~~~..~~~TT...T......TT......T..TT...
.......T...~~~~~~~~.....T......TTTT........TT.TT.
.......~~~.~~~~~~~~....TT....TTTT..........T..TT.
....T...~...~~~~~~~....TT.........T........T...T~
TT.TTT..~...~~~~~~~~~...........TTT.............T
....T...~...~~~~~~~~~.T...........T..............
....TTT.....~~~~~~~~.T..........TTT..............
....TTT.....~~~~~~..T...........................T
.~..T...TT..~~~~~...T.........T..................
....TT..TT........T....TTT.T.....................
~.........T.............TTT......................
...T......TT...~...........T.....................
~~.T......................TT.TT..TT..............
~.T..............T...TTT.TTT.TTTTT...............
.T..........T....T.T.TTT..T.....TTT..............
...........TTT..TTTT..T...T....TTT...............
...........T...~...T.~~..TT..~~...T..............



after 2 Passes
........T..~~~~~~~~~.....T.....TTT........TTTTT..
......T...~~~~~~~~~~....T......TTT.........TT.TT.
.....T.....~~~~~~~~~....T.....TTTT.........TTTTT.
..TTT......~~~~~~~~~~..T........TT.............T~
T..TTT.....~~~~~~~~~~..T.........T..............T
...........~~~~~~~~~..T.........TT...............
.....T.....~~~~~~~~..T..........TT...............
............~~~~~~...T..........................T
.....TTT.....~~~~...T...TT.TTTTT.................
....TTTTTT........T.....TTTT..TT.................
....T.....T.......T......TTT..TT.................
~..T.......T......T......TT...TT.................
~...........T.....T....TTTT..TTTTT...............
..T.........T....TTTTTTTTTT....TTT...............
T...........T....TTTTTTT.TT.....TT...............
...........TT....TT......TT....TT................
...........T...~...T.~~..TT..~~...T..............


After 3 Passes
......T...~~~~~~~~~~....TT.....TTT.........TTTT..
.....TT...~~~~~~~~~~....TT....TTTT.........TTTTT.
....TT....~~~~~~~~~~~...T.....TTTT.........TTTTT.
TTTTT.....~~~~~~~~~~~..TT......TTT............TT~
..........~~~~~~~~~~~..T.......TTT.............TT
..........~~~~~~~~~~..TT.......TTT...............
...........~~~~~~~~...TTT.....TTTT...............
...........~~~~~~~...TTTTTTTTTTTT...............T
............~~~~~...TTTTTTTTTTTT.................
.....TTTT.....~~...T....TTTT..TT.................
....T....TT.......TT....TTT...TTT................
~..T......TT......TT...TTTT...TTT................
...........T......TTTTTTTTT....TTT...............
.TT........TT.....TTTTTTTTT.....TT...............
...........TT.........TTTTT.....TT...............
...........TT..........T.TT....T.................
...........T...~...T.~~..TT..~~...T..............
Logged

Ashkin
Level 10
*****


meow


View Profile
« Reply #5 on: July 01, 2011, 01:25:19 AM »

Funny, I've been working on this recently in Flixel as a matter of fact. I've all but given up on the code, perhaps someone could help me out while we're here, or perhaps my unfinished scrap could help someone else?
Code:
package com
{
import flash.display.*;
import flash.geom.*;
import org.flixel.*;
import org.flixel.data.*;

/**
* ...
* @author Ashkin
*/
public class PlayStateAlt extends FlxState
{
[Embed(source = 'Skull.png')] public var ImgSkull:Class;
[Embed(source = 'RedSkull.png')] public var ImgRedSkull:Class;

public var node0:Node;
public var node1:Node;
public var node2:Node;

public var iterations:int = 3;

public var curnode:int = 0;
public var curiteration:int = 2;

public var nodelist:Array = new Array();
public var nodenum:int = 0;

public var nodeshifter:int = 1;
public var nodeshiftertime:int = 0;

public var linearray:Array = new Array();

override public function create():void
{
bgColor = 0x000000;



node0 = new Node("0", 0, FlxG.height - 20);
node0.addJoined("1");
curnode += 1;

var sprite0:FlxSprite;
sprite0 = new FlxSprite(node0.x, node0.y, ImgRedSkull);
add(sprite0);

var txt0:FlxText = new FlxText(node0.x, node0.y - 12, 8, node0.id);
txt0.setFormat(null, 8, 0xFFFF0000, "left");
add(txt0);



node1 = new Node("1", 150, FlxG.height - 20);
node1.addJoined("0");
curnode += 1;

var sprite1:FlxSprite;
sprite1 = new FlxSprite(node1.x, node1.y, ImgRedSkull);
add(sprite1);

var txt1:FlxText = new FlxText(node1.x, node1.y - 12, 8, node1.id);
txt1.setFormat(null, 8, 0xFFFF0000, "left");
add(txt1);



node2 = new Node("2", 75, FlxU.random() * (FlxG.height - 30));
node2.addJoined("0");
node2.addJoined("1");
curnode += 1;

var sprite2:FlxSprite;
sprite2 = new FlxSprite(node2.x, node2.y, ImgSkull);
add(sprite2);

var txt2:FlxText = new FlxText(node2.x, node2.y - 12, 8, node2.id);
txt2.setFormat(null, 8, 0xFFFF0000, "left");
add(txt2);


nodelist.push(node0);
nodelist.push(node1);
nodelist.push(node2);

for (curiteration;  curiteration < iterations + 1; curiteration++)
{
trace("~~~ITERATION " + curiteration + "~~~")
for (curnode; curnode < ((Math.pow(2, curiteration)) + 1); curnode++)
{
trace("+node: "+curnode);
var node:Node = new Node(curnode.toString(), getSpace(150/(Math.pow(2, curiteration))), FlxG.height - 20);
trace(nodelist[curnode-nodeshifter].x / 2);
nodelist.push(node);
/*
var id:int = new int(node.id);
nodelist.sortOn("x", Array.NUMERIC);
node.addJoined(nodelist[getIDindex(nodelist, node.id) - 1])
nodelist.push(node);
//trace(nodelist[nodelist.indexOf(node.id) - 1]);
//trace(nodelist.indexOf("[Node 0] x: 0 y: 130 joined: 1"));
//node.addJoined(nodelist[nodelist.indexOf(node.id)-1].id);
*/



var sprite:FlxSprite = new FlxSprite(node.x, node.y, ImgSkull);
add(sprite);

var txt:FlxText = new FlxText(node.x, node.y - 12, 8, node.id);
txt.setFormat(null, 8, 0xFFFF0000, "left");
add(txt);



nodeshiftertime += 1;
if (nodeshiftertime >= 2)
{
nodeshiftertime = 0;
nodeshifter += 1;
}

nodelist.sortOn("x", Array.NUMERIC);
}
}
for (var i:int; i < nodelist.length - 1; i++)
{
var line:Shape = new Shape();
line.graphics.lineStyle(2);
line.graphics.moveTo(nodelist[i].x, nodelist[i].y);
line.graphics.lineTo(nodelist[i + 1].x, nodelist[i + 1].y);
linearray.push(line);
}
trace(nodelist);
}

public function getIDindex(array:Array, search:String):int
{
for (var i:int = 0; i < array.length; i++)
{
if (array[i].id == search)
{
return i;
}
}
return -1;
}

public function getSpace(min:int):int
{
var maxspace:int = 0;
for (var i:int = 1; i < nodelist.length; i++)
{
nodelist.sortOn(x, Array.DESCENDING);
if ((nodelist[i].x - nodelist[i - 1].x) > maxspace)
{
maxspace = (nodelist[i].x/2) - (nodelist[i - 1].x/2);
}
}
return maxspace;
}

override public function render():void
{
for (var i:int; i < linearray.length; i++)
{
FlxG.buffer.draw(linearray[i]);
}
super.render();
}

override public function update():void
{
super.update();
}

}

}
Honestly the code is pretty awful, but I guess it's because I'm an amateur at best. So maybe someone could improve on this or something.
Logged
Jay_PC
Level 1
*


It, Happens...


View Profile Email
« Reply #6 on: July 01, 2011, 01:51:29 AM »

So you have an Array of nodes that represents the heights, and you then create a array of  line segments between the nodes? Makes sense to me, Does it not do what you want? or just now work at all.


Logged

Ashkin
Level 10
*****


meow


View Profile
« Reply #7 on: July 01, 2011, 01:55:54 AM »

So you have an Array of nodes that represents the heights, and you then create a array of  line segments between the nodes? Makes sense to me, Does it not do what you want? or just now work at all.
That's the theory at work here. Unfortunately I'm not exactly professional at working with arrays or much of anything, so I ran into issues, though I can't recall my current one. I think it was trying to find the correct place to put the next node? Bleh.
Logged
Jay_PC
Level 1
*


It, Happens...


View Profile Email
« Reply #8 on: July 01, 2011, 02:30:32 AM »

Gotcha, I was trying to figure out how you were inserting nodes into the array. because from the tiny amount i can remember .push() adds to the end of the array.

while I posted some code above that doesn't exactly match your situation but its good for thought. again Make a formula that could produce this:

{1,1} ... 1+1+random = 3;
new Array inset 3
{1,3,1} ... 1+3+random = 2; 3+1+random = 3;
new Array insert 2 and 3
{1,2,3,3,1} ... 1+2+random = 2; 2+3+random = 5; 3+3+random = 7; 3+1+random = 4;
new array insert 2,5,7,4
{1,2,2,5,3,7,3,4,1}

Now in this case these would represent you nodes Y value as the x value likely changes at a constant rate.
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic