# How to decrease velocity constantly?

I am developing a golf game using the GameSalad framework for iPhone. I have one attribute named speed of type integer with value 5000. I also have one boolean attribute named speedControl which is initially false.

When the ball is pressed and its sppedControl is true, I accelerate it with game.speed i.e. 5000.

Now I want to decrease the speed of the ball every 0.5 seconds, so i added a timer behavior and set it to every 0.5 seconds. Every tick of the timer, I set the CHANGE ATTRIBUTE game.speed to game.speed-300. but it's not working.

Sorry, I have no reputation, therefore I can't send you screenshots.

-
pls i really need help – kandarp Sep 3 '11 at 6:14
I've provided an answer that suggests a better way of doing this, but if you want to know what's happening in your specific example we need to know more information than it 'is not working'. Do you have code samples? Have you debugged it to find out exactly what is happening that you didn't expect? – Martin Foot Sep 3 '11 at 6:45
-1 The question is a mess, and it is basically unanswerable. The unexpected behaviour is presumably due to a simple bug, but it is impossible to know what it is without seeing the code. I think you need help on the very basic level of coding, and that is not really within the scope of this site. Also you'd probably be better off with a simpler project. – aaaaaaaaaaaa Sep 4 '11 at 18:42

Why do you want to decrease the velocity every 0.5 seconds, rather than modelling the forces acting on the ball accurately (in a perfect world)? This is not too complicated and will be far more realistic.

Rather than setting a speed, consider taking into account initial velocity in both X and Y, a starting angle of elevation, and a direction. The articles here and here provide ways of taking that information calculating the ball's position at any specific time, t, which is the time since the person hit the ball. They also allow you to calculate the angle required to hit the ball at to hit the target which could be useful to apply a random offset to for bots depending on their difficulty setting.

-
ok thanks i will try and i will refer the link – kandarp Sep 3 '11 at 7:18

The golf game I wrote worked as follows:

In between shots (i.e. when the ball is not moving), the game is using a standard UI event model to allow you to place and execute your shot.

During a shot (i.e. while the ball is in motion), UI event listeners are turned off, and a standard real-time game loop is turned on, to resolve all physics -- motion, friction on that motion, and collisions. As soon as the ball comes to a halt, that physics loop is turned off, and UI event listeners are once again switched on, so the player can line up their next shot.

As Martin Foot has said, you want motion modifiers to your ball (damping from rolling along the green, and also collision checks and resolutions) to be applied every tick of your game/physics loop, typically between 1 in 20 and 1 in 60 times per second. If you have other movable obstacles, then you will include processing of these, here.

-