Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an image background in surface viewenter image description here

I want to move something according to black surface.But i can not do this using height of this image as it return same height.and one more thing bitmap.getPixel is also not working.and my background is moving.So what is the way to achieve this feat

share|improve this question
up vote 4 down vote accepted

To answer the question directly - you need to extract the gradient of the surface from the image. The image pixels need to be analysed using an algorithm such as marching squares. However there are two problems with this approach:

  1. Image analysis uses up valuable processing resources
  2. The result will only be an approximation of the surface

A better solution would be to not use an image in the first place, but to rather keep the path as a vector and use that to render the curve at runtime.


Consider using a pre-existing physics engine, see here for some references:

share|improve this answer
can you suggest some example for last approach mention by you. – TofeeqAhmad Dec 21 '11 at 7:43
You could also pre-calculate the curve and hard code it into your game, if you can't use the vector path.. I don't recommend hard cording though! – Zolomon Dec 21 '11 at 11:26
@Zolomon:i want to use vector path as it sound nice so can you suggest me some tutorial for vector path to meet my problem – TofeeqAhmad Dec 21 '11 at 11:37
@lukevanin:Library mention by you in link is for java.And i think it will not work for android. – TofeeqAhmad Dec 21 '11 at 11:51
@TofeeqAhmad - I'm not sure what you are trying to say, as unless you are writing with NDK, you are using Java on Android. Also, don't add a useless tag to bump this, if that's what you are trying to do... – DMan Dec 27 '11 at 2:34

If I understood right, you need a "raster" background image to display and a gemetric shape that describes that background's silhouette for physical simulations issues.

Looking at your example image I can see a bezier profile so you may store the parameters you used to generate it, along with your image; is up to you to store the rasterized version or use the bezier parameters to generate the displaying image (cpu vs storage trade-off).

The interesting case comes if your background shape does not come from a bezier curve or from an equivalent parametrization: and hand-made artwork for example.

I such cases you may find this paper useful. Here is shown how to get a bezier parametrization from an hand-drawn sketch.

They basically illustrate the process with the following steps:

1 - apply a logarithmic transformation to make it easier to extract the edge.

2 - erosion - dilatation is used to smoth the image

3 - laplacian is applied to find out the edges

once they get the edges, as set of points laying on the shape edge, they start to construct a set of (bezier) curves each of which produces a better edge approximation.

4 - the set of edge points are grouped and for each group a regression line segment is computed

5 - using the intersection of the line segments in the step 4 a set of bezier 2nd grade curves are computed

6 - a method to generate 5th degree Bezier Curve is given: the curve is derived from that obtained in the previous step by letting the derivate and curvature discontinuities be smoothed.

All these steps are obviously computationally relevant and are not intended to be done at runtime.

This should not be a problem since you do not do dynamic backgrounds, if you do so you should have some parametrization and the problem solved in (moved to) an earlier phase of the project.

share|improve this answer
thank you for your answer.I will give it a try. – TofeeqAhmad Dec 27 '11 at 10:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.