Is there a method can get the time a specific key is hold?

I’m planning to make a game like NFS to try what I learn about Panda3D. I’m thinking of the acceleration of the car. The longer a key is hold, the faster the car runs. So I want a method which can return the time that some key is hold. Is there something like that?
Thanks a lot!

Laura

No basic function, but it is possible.
Try something like this:

import direct.directbase.DirectStart
from direct.task.Task import Task
from direct.showbase.DirectObject import DirectObject

class World(DirectObject):
  def __init__(self):
    self.accelerating=False
    self.pressTime=0.0
    self.accept("arrow_up",self.setAcc,[True])
    self.accept("arrow_up-up",self.setAcc,[False])
    taskMgr.add(self.doFrame,"myTask")

  def setAcc(self,pressed):
    self.accelerating=pressed
    self.pressTime=0.0
  def doFrame(self,task):
    if(self.accelerating):
      self.pressTime+=task.time-self.pressTime
    print self.pressTime
    return task.cont
w=World()
run()

That should work.

Hmmmm… smell like an NFS fan too.
Laura, what’s your favourite serie ? I like Underground2 the most, since I can drift on open street, and it’s rendering is very light compared to MostWanted. I haven’t played Carbon yet.
I’ve tried to do car physics using PyODE, but it’s still torque-based, not motion-based like raycar (find it on ODE webpages), so it’s still unstable.

I’m still using 4 hinge2joints (1 for each tire), maybe that’s why there is still small jitter.
The Tiburon car was extracted out from NFS Underground, and I painted it myself. My code is still old, I even haven’t used my OO classes yet, so it’s totally messy and it’s handling is silly, but I still able to do 180 reverse easily.
Press ENTER if you’d like to see the visualizer.

I’m still new in 3d development, this is my first year, and that’s my 1st car physics attempt, so please laugh ! :laughing: :laughing:
download : carPhysics.zip (2.64 MB)

The common way to achieve this is to make your car accelerate each frame where the key is down.
You should have a look at the Verlet integration :

# dt : the time delta between the last frame and now
# carPos : the Vec3 position of the car
# acc : the Vec3 acceleration

acc = Vec3(0,0,0)

if keyIsDown :
    acc += Vec3(0,5,0) # the car accelerates along the Y axis

acc += Vec3(0,0,-5) # gravity ?


damp = math.exp(-dt / .995) # damping effect
tempPos = Vec3(carPos)
carPos += (carPos - carLastPos) * damp + acc * dt * dt
carLastPos = tempPos

The benefit to use dt is that this code is framerate independent.

Edit :
If you want to keep an array with all the pressed keys, you can try :


# AZERTY :-)
self.keyMap = {
	'z' : 'forward',
	'q' : 'left',
	's' : 'backward',
	'd' : 'right',
	'r' : 'up',
	'v' : 'down'
}

self.keyDown = {}

for key in self.keyMap :
	self.keyDown[self.keyMap[key]] = 0
	self.accept(key, self.setKey, [self.keyMap[key],1])
	self.accept(key + '-up', self.setKey, [self.keyMap[key],0])

####

def setKey(self, key, value):
	self.keyDown[key] = value

####
# You can then test is a specific key is down :
if self.keyDown['forward'] :
    pass

Then, you can change your keyMap as you want without altering the code

Thank you very much! I’ll try that out!

Most Wanted maybe my favourite one. Currently I play a lot Ridge Racers on my PSP. Then I think about making such a game by myself.

I’m still new in game development, I have been studied Panda3D for only about 1 month. So sometimes my question may sounds silly, lol! Please laugh, too.
BTW: I cannot install ODE on my computer, I have no idea about that. So I can’t run your game. I’m trying to fix it up, and then I’ll give my comment!
Thanks!!! :slight_smile:

First you must install Python, then PyODE. Find ode.pyd and xode directory in PythonInstallDir\Lib\site-packages, copy them both to the script’s location, or to Panda install dir. Then “import ode” import section will look in the current script location, if it’s not there, python would look into Panda’s install dir. There are some topics about PyODE in the CodeSnippet forum.

Hi I am new to game dev in Panda as well .
I am especially interested in Physics intergration and have learned alot of VERY USEFUL information about PyODE (mainly from ynjh_jo), in the Egg in Bowl physics demo( in the Scripting Area).
I suggest checking out the posted code, and learning from some of the mistakes I encountered being new to PyODE and some of the solutions offered to help me.
PyODE is definitely worth using as it really can make your games do some neat “tricks”. :smiley:

Liquid7800, thanks! I’ll go there to see!!