1

Given an image that can contain any variety of solid color images, what is the best method for parsing the image at a given point and then determining the slope (or Vector if you prefer) of that area?

Being new to XNA development, I feel there must be an established method for doing this sort of thing but I have Googled this issue for awhile now.

By way of example, I have mocked up a quick image to demonstrate what I am trying to do. The white portion of the image (where the labels are shown) would be transparent pixels. The "ground" would be a RenderTarget2D or Texture2D object that will provide the Color array of pixels.

Example

1 Answers1

0

What you are looking for is the tangent, which is 90 degrees to the normal (which is more commonly used). These two terms should assist you in your searching.

This is trivial if you've got the polygon outline data. If all you have is an image, then you have to come up with a way to convert it into a polygon.

It may not be entirely suitable for your problem, but the first place I would go is the Farseer Physics Engine, which has a "texture to polygon" feature you could possibly reuse.


If you are using the terrain as some kind of "ground", you can possibly cheat a bit by looking at the adjacent column of pixels and using that to determine the ground slope at that exact point. Kind of like what Lemmings and Worms do.

If you make that determination at the boundary between each pixel, you can get gradients of rise:run between two pixels horizontally. Usually you just break it into categories: so flat (1:1), 45 degrees (2:1) or too steep (>3:1). With a more complicated algorithm, that looks outwards to more columns, you can get better resolution.

Andrew Russell
  • 26,924
  • 7
  • 58
  • 104
  • The second part of your suggestion is what I am doing now (I sample a 7x7 section and have rules in place to ensure non-transparent pixels are actually part of the current ground and not some outcropping that got included in the slice). It just seems terribly messy (but it works). – DigitalMoss Apr 09 '11 at 21:18