Domanda

I'm playing around with android and would like to try out to simulate a grenade movement with a sine wave. Would any one please help me with the syntax?

This is what I tried with the small math knowledge I have:

mPositionX += mSpeed;
mPositionY = (float) (0.5f * Math.sin(mTargetX * mPositionX));
setPosition(mPositionX, mPositionY);

But it doesnt really do what I excpected..

My mPositionX/Ys initial value is the spawnpoint of the grenade and mTargetX/Y is where I would like the sine wave to have done a half wave (like a semi circle) in other words; where the grenade should land.

My position values is in percentages so 0.5 is half of the canvas etc. if worth to mention.

È stato utile?

Soluzione

The concept you've used for a sine wave seems strange. In a sine wave the equation is

y=sin(x);

This should work for a sine wave

mSpeed=constant;
mTargetX = constant2;
mOriginalX=constant3;
mDistanceX=mTargetX-mOriginalX;

Now in the repeatedly called onDraw

mPositionX+=mDistanceX/mSpeed; //Distance divided by speed
mPositionY=(float) (0.5* Math.sin(Math.PI*(Math.abs(mPositionX-mOriginalX))/mDistanceX); 
            //Assuming you want it to go to pi degrees
            //Divide current distance by total distance * total angle reqd
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top