# How to accelerate ?

I have a car and I would like to be able to press arrow_up.

When I press arrow_up my car should go forward and accelerate until it reaches it highest speed.

How ???

Please post code, do not send me to the examples in Panda.

Thank you.

Just move forward according to current speed, and add to that speed so it go’s faster next time the function is called:

``````
from pandac.NodePath import NodePath
from direct.gui.DirectGUI import *

# this class will inherrit functions from DirectObject and NodePath,
# not nescesery for the car stuff, but I just find it practical.
class Car(DirectObject,NodePath):

def __init__(self,car):
# initialize the NodePath with the car node
NodePath.__init__(self,car.node())
# lets set some settings on the car
self.speed = 0 # current speed
self.maxSpeed = 60.0 # maximum speed
self.acceleration = 0.3 # acceleration rate

def moveForward(self):
# cap the speed
if self.speed < self.maxSpeed:
if self.speed + self.acceleration > self.maxSpeed:
self.speed = self.maxSpeed
else:
# add to the speed, each frame
self.speed += self.acceleration

# move a certain distance with current speed
dist = self.speed/10
angle = self.getH()*math.pi/180.0
dx = dist*math.sin(angle)
dy = dist*-math.cos(angle)
self.setPos(self.getX()+dx,self.getY()+dy,self.getZ())``````

Now the car accelerates… thank you.

How about giving the effect of de-accelerating until it reaches speed of 0 ?

Thanks for the code, Yellow

Probine: How about finding that out on your own? Now with the ACCEL-code it should be easy to write the DE-ACCEL-code on your own

Questions like this might be the later reason why others won’t get answers on simple questions they have really tried hard to solve on their own…

Regards, Bigfoot29

No problem, but on your slowing down code, you can figure that one out yourself now. I’m not the one writing your game , and this way its best so you can learn how to adapt your code.

Hints:
self.breakRate = ?
self.accept(“arrow_up-up”,self.slowDown)

Bigfoot: sure, anytime