2011-03-07 7 views
8

मैं के रूप में gamedev.stackexchange.com पर की सिफारिश की Midpoint Displacement Algorithm जावास्क्रिप्ट का उपयोग और canvas उपयोग करने के लिए कोशिश कर रहा हूँ।क्या यह जावास्क्रिप्ट कोड मिडपॉइंट विस्थापन एल्गोरिदम का पालन करता है?

नीचे दिया गया कोड उन बिंदुओं को उत्पन्न करता है जहां सरणी अनुक्रमणिका x स्थिति है और इसका मान y स्थिति है।

var createTerrain = function(chops, range) { 
    chops = chops || 2; 
    range = parseInt(range || 100); 

    if (chops > 8) return; 

    var cycle = parseInt(width/chops);  

    for (var i = 0; i <= width; i += cycle) { 

     var y = (height/2) + getRandomNumber(-range, range); 

     terrain[i] = y; 

    } 

    chops *= 2; 
    range *= 0.5; 
    createTerrain(chops, range); 

} 

getRandomNumber() के तर्क min और max हैं। width और heightcanvas के संबंध में हैं।

यह कुछ की तरह पैदा करता है ... Canvas

यह बहुत अस्थिर लगता है, लेकिन है कि बस हो सकता है कि यह कैसे है।

मैं इस एल्गोरिथ्म का अधिकार मिल गया है? यदि नहीं, तो क्या गलत है, और क्या आप मुझे सही दिशा में इंगित कर सकते हैं?

+0

आप को कम करके इसे बाहर चिकनी में सक्षम होना चाहिए यादृच्छिक संख्या सीमा। क्या आप पहले से ही कोशिश कर चुके थे? –

+0

@ मैट हाँ, मैंने कुछ चीजों की कोशिश की। अगर मुझे यह एल्गोरिदम सही है तो मुझे और अधिक दिलचस्पी है :) – alex

उत्तर

11

मैं कहना चाहता हूँ यह मध्य बिंदु विस्थापन की तरह नहीं दिखता है, बस क्योंकि हर सेगमेंट के एक नियमित रूप से एक्स अक्ष लंबाई (आप लगभग ऊर्ध्वाधर क्षेत्रों है) हो रही किया जाना चाहिए। क्या आपने एक साधारण 4-सेगमेंट सरणी उत्पन्न करने का प्रयास किया है? वो कैसा दिखता है?

यहाँ एक संक्षिप्त ट्यूटोरियल मैंने लिखा है, आप इसे कार्रवाई में देख सकते हैं: Mid-point algorithm in Javascript

स्रोत कोड:

function Terrain(segmentCount) { 
    this.segmentCount = segmentCount; 
    this.points = []; 
    for (i=0; i<=segmentCount; ++i) { 
     this.points[i] = 0; 
    } 
}; 

/** 
* Generate the terrain using the mid-point displacement algorithm. This is in fact 
* a shortcut to the recursive function with the appropriate value to start 
* generating the terrain. 
* 
* @param maxElevation the maximal vertical displacement to apply at this iteration 
* @param sharpness how much to attenuate maxElevation at each iteration (lower 
*  value means a smoother terrain). Keep between 0 and 1. 
*/ 
Terrain.prototype.generateUsingMidPoint = function(maxElevation, sharpness) { 
    this.midPoint(0, this.segmentCount, maxElevation, sharpness); 
} 

/** 
* This is the recursive function to actually generate the terrain. It computes a new height for the point 
* between "start" and "end" (the mid-point): averages start and end heights and adds a random 
* displacement. 
* 
* @param maxElevation the maximal vertical displacement to apply at this iteration 
* @param sharpness how much to attenuate maxElevation at each iteration (lower 
*  value means a smoother terrain). Keep between 0 and 1. 
*/ 
Terrain.prototype.midPoint = function(start, end, maxElevation, sharpness) { 
    var middle = Math.round((start + end) * 0.5); 
    if ((end-start<=1) || middle==start || middle==end) { 
     return; 
    } 

    var newAltitude = 0.5 * (this.points[end] + this.points[start]) + maxElevation*(1 - 2*Math.random()); 
    this.points[middle] = newAltitude; 

    this.midPoint(start, middle, maxElevation*sharpness, sharpness); 
    this.midPoint(middle, end, maxElevation*sharpness, sharpness); 
}; 
+0

इसके लिए धन्यवाद - अच्छी तरह से स्वीकार्य उत्तर! – alex

0

एल्गोरिथ्म आप को लागू किया है स्पष्ट रूप से गलत है, परिणाम में देखने से। जहां आप गलत हो गए थे, कहना मुश्किल है, क्योंकि आपके द्वारा पोस्ट किया गया कोड स्वयं निहित नहीं है। आप माता-पिता के दायरे में परिभाषित कई चर पर काम कर रहे हैं और यह तुरंत स्पष्ट नहीं है कि वे क्या हैं या वे कैसे व्यवहार करते हैं।

उदाहरण के लिए terrain क्या है?

आप सब से पहले, है एहसास करने के लिए है क्या है कि यदि प्रारंभ और अंतिम बिंदुओं y- अक्ष आप कुछ गलत किया है पर मेल नहीं खाते;)

संबंधित मुद्दे