# How to compute for a falling speed?

How to compute for a falling speed?
0.0 0

#1

Good day to all!

I was wondering if there is a formula or a way to compute for the falling speed of the sprite?
Right now what I am doing is to just deduct 0.5 to the Y of the sprite but it’s not really
realistic

Any help or pointers is appreciated!

#2

You mean falling due to gravity?

If you just want it for 1 sprite then doing it manually would be ok, if you want more than just falling then using something like box2d to do the calculations would be a good idea.

To do it manually, you would need to store a sprite speed value and change the speed by gravity(10 m/s sq to make it easy) computed for the delta time.
Bare in mind that this would give you meters per second, you might have to apply a factor to make it look ok on the device.

If you have a tick or update function that looks like
void tick(float dt) //dt is seconds since last call

Then you would do

spriteSpeed.y =
Then you would update the sprite position by spriteSpeed
sprite->setPositionY.y
spriteSpeed.y * dt)

Or something like that… ignore everything I said if you didn’t mean gravity.

#3

I know using box2d is of great help but I am having a hard time understanding
box2d so as much as possible if I can do it using only cocos2d-x I will do it cocos2d-x side only

Thanks for your answer and that is what I really need! THANKS!

#4

I would avoid something like that. If I’m using physics I’d prefer Box2D to do the calculation.
But for manual sprite movement using CCMoveTo I would do something like this:

``````CCPoint begin = ccp(100, 100);
setPosition(begin);

float velocity = 100;

// you'll get speed that you want it by dividing distance with velocity
float duration = ccpDistance(begin, end) / velocity;

CCPoint end = ccp(200, 200);
runAction(CCMoveto::create(duration, end));
``````

#5

I think I cannot do it using moveTo, because if the moveTo function has not yet finished the sequence
I cannot move the sprite position in the x axis since I am tilting it left or right to move the sprite x-axis
and have it descending at the same time.

#6

Then Adam’s solution is the best for you.

#7

Also, that move to wont take in to account the acceleration effect of gravity right?

I know there are some move to actions that get faster over time but how easy is it to make it look like gravity?

I’m still assuming the OP wanted falling due to gravity.

#8

Thanks anyway, I might used your solution in the future

#9

If falling has the same meaning with scrolling then using schedule should be the best approach since you will have full control of anything. My approach is only fit if you’re doing ‘static’ moving. While Box2D will only works if you wanted gravity.

#10

Terry Bleger wrote:

If falling has the same meaning with scrolling then using schedule should be the best approach since you will have full control of anything. My approach is only fit if you’re doing ‘static’ moving. While Box2D will only works if you wanted gravity.

Right, I have a lot of actions happening while descinding that is why I can’t use the moveTo function but your approach will be used to some
movements of the enemies, thanks for that! :)
@Adam Reed: your formula works great! just need to play with the default speed value to get the correct falling speed :
)