Could you please help me out to find the correct deceleration rate for the following scenario:
There’s a point A where car has to stop at. Let’s say the car is 200 pixels away from A and it’s travelling with velocity of 50. Now how can I achieve the correct deceleration so that the car kills down the speed gradually and finally stops at point A? In real world it’s quite easy but in computer world we have to take into account the fact that the car does not move in fact it’s just the screen redrawing it each time at a newer position. Just to mention there’s a schedule that fires the method that takes the car forward with the given velocity. So I think the schedule interval has also to be taken into account while calculating.
I don’t think language, device or platform matters but just in case I’m doing this in iPhone cocos2d
Could you please help me out to find the correct deceleration rate for the
Share
Don’t worry, you don’t need a special kind of math here, you can use your “real world math” just fine. The problem with the scheduler and the framerate also isn’t a real one, but related to framerate dependend movement. Assuming that you have this function to move your object (half pseudo code):
This code depends on the framerate, for example, if you render with 30 FPS and start with an
velocity.xof 150, it will take exactly a second for the object to decelerate to 0. However, if you run the same method at an FPS of 60, it will take only half a second for the object to decelerate to 0.So what you really want is a way to move your car independent of the framerate, so that it moves in one second n points no matter what framerate you run your code. And the way to achieve this is by using the time that the frame will use to render, and multiply this delta time with your movement. And because you can’t predicate how long the frame will take to render before or while you are rendering it, you usually use the delta time of the previous frame. And then you end up with code looking like this:
(If you actually use this, you will see that your movement suddenly became A LOT slower because previously you moved 5 points per frame and now you are moving with 5 points per second!)
I’m not a Cocos 2D pro, but it appears (from a quick google search), that you get the delta time in a few methods (I can’t name them, the class references don’t seem to mention them, but if you somewhere have a method which gets a
ccTimepassed every frame, thats probably one of them)