Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Following a landscape (2D)  (Read 698 times)
0 Members and 1 Guest are viewing this topic.
Offline Zerolife

Senior Newbie





« Posted 2010-06-04 18:53:24 »

I know I ask a lot of stupid questions that have probably been answered before, but I never seem to find my answer.

Im trying to program an object to follow along the landscape that I made, Right now, the landscape is only in a picture. All I want is to have the object follow along the landscape and change the direction to be parallel with the ground as it goes. It doesn't need to be overly complex as there wont be a situation where the object cannot go up or down, it is a basic landscape.

If you can at least provide a link to an old thread that is fine, as there probably is one. Search engines and I do not get along :\
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #1 - Posted 2010-06-04 22:27:17 »

If the landscape is a picture you have two options:

1 - Look at all the pixel values and do some sort of color comparison at any given X to find what the floor is at that position. i.e.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
//Assume ground is a BufferedImage with type BufferedImage.TYPE_INT_ARGB
//Assume we have a ground color that is a 4 int array (might be like {0,0,0,0})
//x is the X position to check and startY is where the entity is standing (so you can make caves and the like work).
public int get groundHeightAtX(int x, int startY)
{
    int[] testArray = new int[4];
    for (int y = startY; y < ground.getData().getHeight(); y++)
    {
            int[] pixels = ground.getData().getPixel(x,y,testArray);
            if (pixels[0] == groundColor[0] && pixels[1] == groundColor[1] && pixels[2] == groundColor[2] && pixels[3] == groundColor[3])
            {
                return y;
            }
    }
    //If there's no floor found, this is considered a bottomless pit.
   return Integer.MAX_VALUE;
}

If you do that, it would be smart to cache the values for every X (in another array, probably) so that you're not calling this constantly for every single entity.

2 - Create some sort of geometry that represents the ground, probably a bunch of points - you'd find which points that you lie between and then use the slope of the line to decide the height at that particular point. Faster but requires more effort.

See my work:
OTC Software
Offline krasse
« Reply #2 - Posted 2010-06-05 08:04:58 »

You can evaluate the partial derivatives of the heightmap (the image) and get the gradient (dHeight/dx, dHeight/dy).

Since the image changes in steps, you have to interpolate these values first though before you can compute the gradient. You can use bilinear interpolation for this.

When you then have the gradient, you can choose to follow this vector forward (uphill), backward (downhill), or perhaps a vector that is perpendicular to it to stay at the same height.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (55 views)
2014-04-15 18:08:23

BurntPizza (53 views)
2014-04-15 03:46:01

UprightPath (66 views)
2014-04-14 17:39:50

UprightPath (49 views)
2014-04-14 17:35:47

Porlus (66 views)
2014-04-14 15:48:38

tom_mai78101 (90 views)
2014-04-10 04:04:31

BurntPizza (151 views)
2014-04-08 23:06:04

tom_mai78101 (246 views)
2014-04-05 13:34:39

trollwarrior1 (204 views)
2014-04-04 12:06:45

CJLetsGame (211 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!