• Content count

  • Joined

  • Last visited

Community Reputation

127 Neutral

About ProgrammerGuy123

  • Rank
  1. I need help on how I should go about updating entities in my game with a chunk based approach. It's a 2D tile based side scroller. This is about updating the entity logic not drawing tiles.   My game is going to have different entities like monsters and projectiles. I don't wan't to update all the hundreds of entities in the map at the same time so I need some sort of chunk system, where I split the map up into chunks and only update the chunks that are visible.   With this in mind I thought about giving each chunk a list of entities that belong to it. Then I could update each visible chunk, which would cycle through the chunk's list and update each individual entity. If an entity like a monster moves from one chunk to another I could remove it from the previous chunk's list and add it two the new chunk's list.   However, say I shoot a projectile off my screen, then the entity would not get updated until the chunk is able to update which means making that chunk visible. I guess I would just have to force update the chunks the projectile is in then.   It seems like that could work, but there are probably more efficient ways to handle cutting down hundreds of entity updates to tens of entity updates, which is why I'm asking. This is a very fundamental concept to any large tile based game, so I want to here what your method is. How do you handle updating entities in a efficient matter?   Thanks.
  2. Objects in my game have both update and draw functions as expected, the update function sometimes need a delta time variable and the draw function sometimes needs a window object.   I have seen lots of engines pass in the delta time variable through every update function and pass in the window object through every draw function like so: //C++ void State::update(float deltaTime) {      sprite.pos += vel * deltaTime; } void State::draw(Window window) {      window.draw(sprite); } The problem with passing these objects through every single update and draw method is that sometimes the're not used. For example, a main menu does not need a delta time variable but does need a window object, but you can't just omit the parameter if you don't need it because the main menu inherits from the game state object which contains the parameters.   So would it be better in design to never pass these objects into functions but instead make them static like so? //C++ void State::update() {      sprite.pos += vel * GameManager::deltaTime; } void State::draw() {      GameManager::window.draw(sprite); } I'm asking because this is a very fundamental concept to game engines and the latter example just seems to make more since but it seems like the former example is favored.   Which is better code design? Thanks.
  3. Hovercraft Physics

    Ah yes thank you. Very informative links.
  4. Hovercraft Physics

    I'm trying to implement hovercraft like physics into my game but I need help. An improtant thing to note is that the hovercraft will be pretty high off the ground unlike realistic hovercrafts. After searching and asking questions I arrived at this source code: [CODE] double g = 9.8d; //gravity double m = 1.0d; //mass double fMax = 15; //maximum upwards force double idealHeight = 100; //ideal height the craft should be off the ground double v; //velocity void Update() { double h = Math.Abs((craft.Y + craft.Height) - ground.Y); //determin the craft's height from the ground. double a = (g - f(h)) * m; //acceleration v += a; //velocity v = MathHelper.Clamp((float)v, -15.0f, 15.0f); //make sure the acceleration doesn't get out of hand craft.Y += (int)v; //assign the velocity } double f(double h) { //this function return the amount of upwards force return Math.Max(0, h * (g - fMax) / idealHeight + fMax); } [/CODE] One cunfusing thing to note is that my game is 2D with (0, 0) being the top left corner of the window and (100, 100) being accrosed and down 100 pixels. This is why I have the Math.Abs(craft.Y + craft.Height) - ground.Y whole thing. Now the problem: This code works but the craft just goes up and down up and down up and down enlessly. I would like the craft's velocity to diminish over time to equal out with the idealHeight. Like [url=""]this[/url] picture shows. How would I do that? Thanks.
  5. Whats Wrong With My Perlin Noise Function?

    Thank you and everyone else I just need the /100 and it works perfectly now!
  6. Whats Wrong With My Perlin Noise Function?

    That is indeed how I'm called the method: [CODE] byte[,] temp = new byte[height, width]; for(int x = 0; x < width; x++) { byte value = (byte)((PerlinNoise2D((double)x) * 64) + 64); temp[value, x] = 1; } [/CODE] This is exactly why I was using integers in my pervious attempts for everything and not decimals. Is there any way I could efficiently convert the integers into decimals and back into inegers again? Or should I go back to the way I was doing it and have everything calculated with integers. Right now I'm multiplying the perlin value by 64 just for testing as you can see then adding the same value to get rid of any negatives. But like you said I'm only going to get values based on integer boundries so what should I do?
  7. Whats Wrong With My Perlin Noise Function?

    [quote name='FLeBlanc' timestamp='1338478848' post='4945015'] You have revised it to be even more wrong than before. By just calling Next without the seeding operation, you guarantee you get a different value for any given input point each time it is called. Random isn't really what you want to use here. You want a hash function, that correlates an input, x, to an output, y. The hash needs to ensure that the hash for x+1 is in no way correlated to the hash for x, so that patterns do not appear in the output. jtippets linked to the reference implementation for Perlin noise. It might be a good idea to look and see how it does hashing, interpolation, etc... Also I note that you seem to be using a lot of integer coordinates and values. There is a problem with this. The algorithm you are using will generate pseudo-random values at integer coordinates, but since you never sample the "between" points (non-integer), the result is probably going to look a great deal like white noise. [/quote] Ok so I tried to fix a lot all of these problems but the function is still acting up and I'm really confused on why because I scrapped the whole thing to use double values instead of integers and I even took the complete RNG from [url=""]this[/url] article about perlin noise. This solves the problem of having to use hash tables because now I'll get a random number where x and x+1 is a completely different number. However now it seems to return negative values which makes absolutely no since considering I use his RNG not .NET's. Also the InterpolatedNoise method is almost the exact same I'm just not smoothing the values because I want to simplify it. Then there is the CosineInterpolate method. To me logically this must be the reason for my negative values because as stated before I took the entire RNG from the article so it can't be that. The CosineInterpolate method was taken from [url=""]this[/url] article. I've made no methods all the methods responsible for generating perlin values are from reliable sources. What is going on? [CODE] public double PerlinNoise2D(double x) { int octaves = 1; double freq = 16; double amp = 1; double total = 0; for(int i = 0; i < octaves; i++) { total += InterpolateNoise(x * freq) * amp; } return total; } double InterpolateNoise(double x) { int xInt = (int)x; double xFrac = x - xInt; double v1 = Noise(xInt); double v2 = Noise(xInt + 1); return CosineInterpolate(v1, v2, xFrac); } double Noise(int x) { x = (x<<13) ^ x; return ( 1.0 - ((x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0); } double CosineInterpolate(double a, double b, double x) { double f = (1 - Math.Cos(x * Math.PI)) / 2; return (a * (1 - f) + b * f); } [/CODE]
  8. Whats Wrong With My Perlin Noise Function?

    [quote name='jefferytitan' timestamp='1338441167' post='4944882'] If I recall correctly, each time around the main loop you're meant to double the frequency and decrease the amplitude. You seem to be keeping them constant. [/quote] The values were not changing because I only had one octave for simplicity therefor there was no need for them to change. [quote name='Olof Hedman' timestamp='1338448657' post='4944897'] You are also using Random() wrong. You should initialize it once, with a single seed of your choosing, and then just continuously call "next" to read out the random value. [/quote] This was a mistake on my part I was thinking I needed a random value based on "x" which mean if "x" is ever the same it should generate the same number. I have revised the code I think to fix this problem but no luck. First I need just one octaves to work then I can generate more and add them up but for now I'm focusing on one octave which you will see in my code: [CODE] public int PerlinNoise2D(int x) { int total = 0; int octaves = 1; //only one octave for simplicity for(int i = 0; i < octaves; i++) { int freq = 16;//16 for simplicity I only have one octave so it doesn't need to change int amp = 1; //1 for simplicity again ^^^ total += InterpolateNoise(x * freq) * amp; } return total / octaves; } int InterpolateNoise(int x) { int v1 = random.Next(64); int v2 = random.Next(64); return CosineInterpolate(v1, v2, x, 16); // the 16 is from the frequency } int CosineInterpolate(int a, int b, int x, int length) { return (int)((1 + Math.Cos(3.1415f * x / length)) / 2 * (a - b) + b); } [/CODE]
  9. I understand the concept behind perlin noise but I can't understand why the method doesn't return perlin values: [CODE] public int PerlinNoise2D(int x) { int freq = 4;//initFrequency; int amp = 1;//initAmplitude; int total = 0; for(int i = 0; i < octaves; i++) { total += InterpolateNoise(x * freq) * amp; } return total; } int InterpolateNoise(int x) { int v1 = new Random(x).Next(64);//this returns a pseudo-random number less than 64 with "x" as the seed int v2 = new Random(x + 1).Next(64); return CosineInterpolate(v1, v2, x, 4); } int CosineInterpolate(int a, int b, int x, int length) { return (int)((1 + Math.Cos(3.1415f * x / length)) / 2 * (a - b) + b); }[/CODE] It generates repeating values that are not perlin and as explained before I need help on fixing it. Thanks.
  10. 2D Perlin Noise

    [quote name='JTippetts' timestamp='1336344285' post='4937893'] You interpolate the top-left and top-right corner values based on the x coordinate, interpolate the bottom-left and bottom-right corners based on the x coordinate, then interpolate these two intermediate values based on the y coordinate. [/quote] What do you mean by "corner values"? The octaves are made up of multiple squares. Am I getting the corner values of those squares? Wouldnt they be the same value then? Could you explain what you mean by this?
  11. 2D Perlin Noise

    I'm trying to create perlin noise but I'm having trouble understanding how interpolation works. I understand how to do it in a single dimension but can't wrap my head around it in two. Right now I have this for an octave: [img][/img] Obviously this is not coherent and I need to interpolate to get something similar to this: [img][/img] Most sources such as [url=""]this[/url] explain how to do it one dimension but don't in two. Quote: "You can, of course, do the same in 2 dimensions." So could someone explain the concept of how [u]linear[/u] interpolation works in two dimensions? Thanks.