Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
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 1458 times)
0 Members and 1 Guest are viewing this topic.
Offline Zerolife

Senior Newbie

« Posted 2010-06-04 16: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: 11
Exp: 10 years

Game Engineer

« Reply #1 - Posted 2010-06-04 20: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.
//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 06: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  

EgonOlsen (59 views)
2018-06-10 19:43:48

EgonOlsen (42 views)
2018-06-10 19:43:44

EgonOlsen (61 views)
2018-06-10 19:43:20

DesertCoockie (240 views)
2018-05-13 18:23:11

nelsongames (142 views)
2018-04-24 18:15:36

nelsongames (141 views)
2018-04-24 18:14:32

ivj94 (883 views)
2018-03-24 14:47:39

ivj94 (144 views)
2018-03-24 14:46:31

ivj94 (795 views)
2018-03-24 14:43:53

Solater (159 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
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!