This site requires JavaScript, please enable it in your browser!
Greenfoot back
behappy2
behappy2 wrote ...

2012/12/30

inertia

behappy2 behappy2

2012/12/30

#
i was thinking of having a car that keeps going for a bit after u take the throttle off, does anyone know how to get this to work?
danpost danpost

2012/12/30

#
My Teleport Demo scenario has an example of an object (the spaceship) that gradually slows down as long as no power is added (even though there is actually no resistance in space).
behappy2 behappy2

2012/12/30

#
int ds = -1;
        if (Greenfoot.isKeyDown("up")) ds += 20;
        // adjust speed
        speed += ds;
        if (speed < 0) speed = 0;
        if (speed > 2000) speed = 2000;
        // and move
        if (speed >= 200) move(speed / 100);
i dont get this, could you explain it in english please, eg. if (the speed of object is less than 0)...
nooby123 nooby123

2012/12/30

#
The ds is the acceleration of the car. When you press "up", you increase the acceleration, therefore the object moves slowly forward. If the speed is below 0, set the speed to 0. If the speed is above 2000, set it to 2000. This is just the speed range. After you adjusted the speed, move. Only move if the speed is above 200 or else it would pop up an error.
behappy2 behappy2

2012/12/30

#
so the speed in the ifs is only a representation, and the move is the actual mooving bit? i think i get it, so i could put 'cake' >= whatever, but move(cake / 100)?
nooby123 nooby123

2012/12/30

#
Yes, as long as (cake/100) is above or equal to the value of 1.
behappy2 behappy2

2012/12/30

#
thx
danpost danpost

2012/12/30

#
Sure. First, let me explain the way I dealt with it; and then, the code. Some people would use 'double' type numerics to control the speed for smoother movement; where I used large integers. Notice the last statement where I divide the speed by 100, which makes one unit of speed equal to one hundreth of a pixel; and the other values ('200' and '2000') are used for comparisons and limits. Maybe now the code would be more easily understood. Line one is the friction/drag (gradual slowing down) of the object) with 'ds' being the change in speed. Line two checks for acceleration. It takes five act cycles for a one-pixel change in speed. Line four adds the change in speed to the current speed. Line five makes sure the speed is not negative (restricting object from going backwards). Line six limits the maximum speed to 20 pixels per act cycle. Line eight performs the actual moving of the object requiring that the speed is at least 2 pixels per act. This requirement might have been put in place due to the particulars of my scenario (I do not remember exactly why I put that requirement in there).
behappy2 behappy2

2012/12/30

#
does it have to be ds?
danpost danpost

2012/12/30

#
It could be any allowable variable name you want it to be. 'ds' was sufficient for me because the prefix 'd' usually indicates 'change in'; and 's' is standard abbreviation for 'speed'.
behappy2 behappy2

2012/12/30

#
thx
You need to login to post a reply.