Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Finding the Average

This topic is 5189 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm not at my best with math, so any advice or tips you can give me on this will be appreciated. First, I'll just throw out the question, then I'll explain reasons I need to use it (if anyone is interested). What is the most efficient method to get an average of 9 points, and dump off points which are [so much] below the highest point? For example, if the points range from 55-70, they would all be used. If they range 20-70, any points below (70-30)==40 would be set to 40 before being averaged. My code is at the bottom for doing this. I'm creating a 2D side scrolling which has a pixel based height map. The height map ranges in USHORT values. To determine where an object should rest, I use 9 points of the object ground area and get the average. The points are the corners and the centers of each. Like this:
X   X   X

X   X   X

X   X   X  
To be able to walk from one height to another, the difference cannot be more than 30. More than 30 is steeper than 45 degrees for character sizes. Everything from tiny bumps to hills to big buildings and cliffs are made with the height map. This is the code I currently use, and it's a bit ugly. x1 is left, X is center, and x2 is right, etc. Each section is just moving the pointer to the next point and comparing it. I know a loop would look nice, but then I would end up making a lookup table anyways:
// Setup Attributes
LONG XMs = Pos.X - Pos.x1;
LONG XEs = Pos.x2 - Pos.X;

// Start Pointers
USHORT HVal = GetHighestPoint(); // highest point
USHORT *Hp = GETHEIGHTMAPPTR(Pos.x1, Pos.z1);
LONG AVal = 0; // average point

// Top-Left
if(HVal - *Hp > 30)
	AVal += HVal - 30;
else
	AVal += *Hp;

// Top-Center
Hp += XMs;
if(HVal - *Hp > 30)
	AVal += HVal - 30;
else
	AVal += *Hp;

// Top-Right
Hp += XEs;
if(HVal - *Hp > 30)
	AVal += HVal - 30;
else
	AVal += *Hp;

// Center-Right
Hp += (Pos.Z - Pos.z1) * HeightMapWidth;
if(HVal - *Hp > 30)
	AVal += HVal - 30;
else
	AVal += *Hp;

// Center-Center
Hp -= XEs;
if(HVal - *Hp > 30)
	AVal += HVal - 30;
else
	AVal += *Hp;

// Center-Left
Hp -= XMs;
if(HVal - *Hp > 30)
	AVal += HVal - 30;
else
	AVal += *Hp;

// Bottom-Left
Hp += (Pos.z2 - Pos.Z) * HeightMapWidth;
if(HVal - *Hp > 30)
	AVal += HVal - 30;
else
	AVal += *Hp;

// Bottom-Center
Hp += XMs;
if(HVal - *Hp > 30)
	AVal += HVal - 30;
else
	AVal += *Hp;

// Bottom-Right
Hp += XEs;
if(HVal - *Hp > 30)
	AVal += HVal - 30;
else
	AVal += *Hp;

return (AVal / 9);  
Feel free to call me an idiot. I would hope it would mean you have a much better way to do this. Thanks again for any help [edited by - Jiia on March 6, 2004 7:21:48 PM]

Share this post


Link to post
Share on other sites
Advertisement

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!