# how can we define dynamic Path of ViewObject(Bitmap) On Canvas

I am Currently working on One 2D Android Game,

In this game One ViewObject(Bitmap) is moving Across Screen On Parabola Path Like in this Image, But this Path is Static, the Static path is getting throught the Drawing with Fingure on canvas,

As Same as signature Drawing.

The Bitmap Move code On this Static Path is

//animation step
private static int iMaxAnimationStep = 900;
private int iCurStep = 0;
private Path ptCurve = new Path(); //curve
private PathMeasure pm;            //curve measure
private float fSegmentLen;         //curve segment length

//init smooth curve
PointF point = aPoints.get(0);
ptCurve.moveTo(point.x, point.y);

for(int i = 0; i < aPoints.size() - 1; i++){
point = aPoints.get(i);
PointF next = aPoints.get(i+1);
ptCurve.quadTo(point.x, point.y, (next.x + point.x) / 2, (point.y + next.y) / 2);
}

pm = new PathMeasure(ptCurve, false);
fSegmentLen = pm.getLength() / iMaxAnimationStep;//20 animation steps

//animate the Bitmap
Matrix  mxTransform = new Matrix();
if (iCurStep <= iMaxAnimationStep)
{

pm.getMatrix(fSegmentLen * iCurStep, mxTransform,
PathMeasure.POSITION_MATRIX_FLAG);
mxTransform.preTranslate(-Bitmap.getWidth(), -Bitmap.getHeight());

canvas.drawBitmap(Bitmap, mxTransform, null);

iCurStep++; //advance to the next step
mPauseViewHandler.post(mPauseViewRunnable);
} else {
iCurStep = 0;

}


But My Problem is I want to Move This ViewObject(Bitmap) On Dynamic Path(in parabola curve) & that Dynamic curved path will work in Any Device.

I have searched Lot but i can't Find Solution How to get Dynamic Path (in parabola curve).

help! If you have Any Solution,Suggestion, idea ,tutorial regarding this post is Mostly Appreciated.

• You may want to clarify your question, as it seems unclear at the moment. Commented Jun 11, 2013 at 22:23

If I understand your question correctly, you're looking for a damped harmonic function which would be something along the lines of . In this function x would be time and it would give you the vertical acceleration of the bounce.

An example of an optimized version of the function to your situation might be (replaced x with t for time) which would result in the graph of:

At f(0), the function will return 1 or the full "force" of the bounce. You cannot have a -delta_time the function has a limited domain of t >= 0 so the function always returns a number between -1 and 1 so you can use it as a scalar for a velocity.

If you just want the height of a bounce just get the absolute value so it becomes which will look like:

This is a scalar from 0-1 so just multiply it by the height:

Images courtesy of Wolfram Alpha

• thanks for ur answer that better understand physics behind this question. Commented Jun 12, 2013 at 6:11
• That looks cool, but it isn't physically correct. The waves you got there are no parabolas, is are damped sine functions. To be correct, it should have parabolas, in order to get a realistic gravitation effect. Commented Jun 15, 2013 at 13:31
• @MartijnCourteaux - They are making a simple animation not a physics simulation clearly.
– stas
Commented Jun 17, 2013 at 0:27

I would recommend looking at Box2D. It is a physical engine that will take care of all the physics your game needs. However, if you think this engine is an overkill, just write the simple bouncing mechanism yourself.

Create a vector acceleration, a vector velocity and a vector position. Then on each frame you use the formula's found by integration. Assume step is your timestep in seconds (which is probably something like 1/60).

position += integral(velocity, 0, step), where
velocity += integral(acceleration, 0, step), where
acceleration = (0, -9.81).

So substitute these things in each other gives:

velocity += integral(acceleration, 0, step);
vector velocity0 = velocity;
velocity = velocity0 + vector(acceleration.x * step, acceleration.y * step);


The same for position:

position += integral(velocity, step)
position += integral(velocity0 + vector(acceleration.x * step, acceleration.y * step), 0, step)
position += vector(velocity0.x * step + 0.5 * acceleration.x * step * step, velocity0.y * step + 0.5 * acceleration.x * step * step);


So the code you have to apply each frame is:

vector velocity0 = velocity;
velocity += vector(acceleration.x * step, acceleration.y * step);
position += vector(velocity0.x * step + 0.5 * acceleration.x * step * step, velocity0.y * step + 0.5 * acceleration.y * step * step);


Now, to bounce, just check when position.y < 0. If that happens, do this:

velocity = vector(velocity.x, -velocity.y * restitution)
position.y = 0


Where restitution is a factor between 0 and 1. A normal value would be like 0.6.

• thanks for your answer but i don't have to use other game engine Commented Jun 12, 2013 at 6:09
• Just keep reading. Only the first sentence of my answer was referring to a physics engine. The whole other part is about doing it yourself. Commented Jun 15, 2013 at 13:32

Go with simple kinematics. after every bounce the velocity that the entity collided with becomes the initial velocity for the next move. The entity moves in a parabola because of acceleration due to gravity, repeat until many times as necessary. For different curve shapes(more narrow or wider) experiment with direction and velocity at which the object moves.