# Move and stop - based on Asteroids

Hello friends!

I search in the forums, but don´t find the answer.

I´m creating a project and the model moves like the ship in the asteroid game (left and right turns, and up move to the direction currently facing). i try edit the code, but my model always go and go front, and stop when the velocity over.

someone can tell me another program sample in the forums to I learn the formula?
thanks.

import direct.directbase.DirectStart
from pandac.PandaModules import TextNode
from pandac.PandaModules import Point2,Point3,Vec3,Vec4
from direct.gui.OnscreenText import OnscreenText
from direct.showbase.DirectObject import DirectObject
from math import sin, cos, pi
from random import randint, choice, random
from direct.interval.MetaInterval import Sequence
from direct.interval.FunctionInterval import Wait,Func
import cPickle, sys

from direct.actor import Actor

ACCELERATION = 10 #Ship acceleration in units/sec/sec

panda = Actor.Actor(“models/panda-model”, {“walk”:“models/panda-walk4”})
panda.reparentTo(render)
panda.setScale(0.005)
panda.setPos(0,-30,0)
panda.setH(180)
#panda.loop(“walk”)
return panda

#para controlar a camera
m = base.camera.getX() + 2
base.camera.setPos(m,m,m)

class World (DirectObject):
def init(self):
#carrega o fundo
#associa para carregar render e o topo da arvore do que e renderizado
cenario.reparentTo(render)
cenario.setScale(0.25)

``````#carrega modelo parado
#panda.reparentTo(render)
#panda.setScale(0.005)
#panda.setPos(0,-30,0)
self.setVelocity(self.pd, Vec3(0,0,0))
self.keys = {"cima": 0 , "baixo" : 0 ,"esquerda" : 0 ,"direita" : 0 }

#base.camera.setZ(10)
#base.camera.setP(90)

self.accept("w", self.mudapos,["cima" , 1])
self.accept("s", self.mudapos,["baixo" , 1])
self.accept("a", self.mudapos,["esquerda" , 1])
self.accept("d", self.mudapos,["direita" , 1])

self.accept("w-up", self.mudapos,["cima" , 0])
self.accept("s-up", self.mudapos,["baixo" , 0])
self.accept("a-up", self.mudapos,["esquerda" , 0])
self.accept("d-up", self.mudapos,["direita" , 0])

``````

def mudapos(self, key, val): self.keys[key] = val

def setVelocity(self, obj, val):
list = [val[0], val[1], val[2]]
obj.setTag(“velocity”, cPickle.dumps(list))

def getVelocity(self, obj):
return Vec3(list[0], list[1], list[2])

``````   #para mover suavemente
``````

self.updatePos(dt)

def updatePos(self, dt):
giro = self.pd.getH()
if self.keys[“esquerda”]:
giro += dt * 360
self.pd.setH(giro%360)
elif self.keys[“direita”]:
giro -= dt * 360
self.pd.setH(giro%360)

elif self.keys[“cima”]:
newVel += self.getVelocity(self.pd)
self.setVelocity(self.pd, newVel)

vel = self.getVelocity(self.pd)
newPos = self.pd.getPos() + (vel * dt)
self.pd.setPos(newPos)

w = World()
run()

put your code in [ code ][ / code ] tags other wise its unreadable because the indention is lost.

the models is the panda and environment from manual

``````import direct.directbase.DirectStart
from pandac.PandaModules import TextNode
from pandac.PandaModules import Point2,Point3,Vec3,Vec4
from direct.gui.OnscreenText import OnscreenText
from direct.showbase.DirectObject import DirectObject
from math import sin, cos, pi
from random import randint, choice, random
from direct.interval.MetaInterval import Sequence
from direct.interval.FunctionInterval import Wait,Func
import cPickle, sys

from direct.actor import Actor

ACCELERATION = 10 #Ship acceleration in units/sec/sec

panda = Actor.Actor("models/panda-model", {"walk":"models/panda-walk4"})
panda.reparentTo(render)
panda.setScale(0.005)
panda.setPos(0,-30,0)
panda.setH(180)
#panda.loop("walk")
return panda

#para controlar a camera
m = base.camera.getX() + 2
base.camera.setPos(m,m,m)

class World (DirectObject):
def __init__(self):
#carrega o fundo
#associa para carregar render e o topo da arvore do que e renderizado
cenario.reparentTo(render)
cenario.setScale(0.25)

#panda.reparentTo(render)
#panda.setScale(0.005)
#panda.setPos(0,-30,0)
self.setVelocity(self.pd, Vec3(0,0,0))
self.keys = {"cima": 0 , "baixo" : 0 ,"esquerda" : 0 ,"direita" : 0 }

#base.camera.setZ(10)
#base.camera.setP(90)

self.accept("w", self.mudapos,["cima" , 1])
self.accept("s", self.mudapos,["baixo" , 1])
self.accept("a", self.mudapos,["esquerda" , 1])
self.accept("d", self.mudapos,["direita" , 1])

self.accept("w-up", self.mudapos,["cima" , 0])
self.accept("s-up", self.mudapos,["baixo" , 0])
self.accept("a-up", self.mudapos,["esquerda" , 0])
self.accept("d-up", self.mudapos,["direita" , 0])

def mudapos(self, key, val): self.keys[key] = val

def setVelocity(self, obj, val):
list = [val[0], val[1], val[2]]
obj.setTag("velocity", cPickle.dumps(list))

def getVelocity(self, obj):
return Vec3(list[0], list[1], list[2])

#para mover suavemente
self.updatePos(dt)

def updatePos(self, dt):
giro = self.pd.getH()
if self.keys["esquerda"]:
giro += dt * 360
self.pd.setH(giro%360)
elif self.keys["direita"]:
giro -= dt * 360
self.pd.setH(giro%360)

elif self.keys["cima"]:
newVel += self.getVelocity(self.pd)
self.setVelocity(self.pd, newVel)

vel = self.getVelocity(self.pd)
newPos = self.pd.getPos() + (vel * dt)
self.pd.setPos(newPos)

w = World()
run()``````

christhi, are you like a rebellious teenager?

Don’t be so mean. By hitting “Quote” on the original post you can always get the original code with indentation.

oh i did not know that. The last thing i remember doing was looking at the HTML source and fighting with the HTML encoding.

ok people, it´s my first post and i´m learning yet, I will try again:

``````import direct.directbase.DirectStart
from pandac.PandaModules import TextNode
from pandac.PandaModules import Point2,Point3,Vec3,Vec4
from direct.gui.OnscreenText import OnscreenText
from direct.showbase.DirectObject import DirectObject
from math import sin, cos, pi
from random import randint, choice, random
from direct.interval.MetaInterval import Sequence
from direct.interval.FunctionInterval import Wait,Func
import cPickle, sys

from direct.actor import Actor

ACCELERATION = 10   #Ship acceleration in units/sec/sec

panda = Actor.Actor("models/panda-model", {"walk":"models/panda-walk4"})
panda.reparentTo(render)
panda.setScale(0.005)
panda.setPos(0,-30,0)
panda.setH(180)
#panda.loop("walk")
return panda

#para controlar a camera
m = base.camera.getX() + 2
base.camera.setPos(m,m,m)

class World (DirectObject):
def __init__(self):
#carrega o fundo
#associa para carregar  render e o topo da arvore do que e renderizado
cenario.reparentTo(render)
cenario.setScale(0.25)

#panda.reparentTo(render)
#panda.setScale(0.005)
#panda.setPos(0,-30,0)
self.setVelocity(self.pd, Vec3(0,0,0))
self.keys = {"cima": 0 , "baixo" : 0 ,"esquerda" : 0 ,"direita" : 0 }

#base.camera.setZ(10)
#base.camera.setP(90)

self.accept("w", self.mudapos,["cima" , 1])
self.accept("s", self.mudapos,["baixo" , 1])
self.accept("a", self.mudapos,["esquerda" , 1])
self.accept("d", self.mudapos,["direita" , 1])

self.accept("w-up", self.mudapos,["cima" , 0])
self.accept("s-up", self.mudapos,["baixo" , 0])
self.accept("a-up", self.mudapos,["esquerda" , 0])
self.accept("d-up", self.mudapos,["direita" , 0])

def mudapos(self, key, val):   self.keys[key] = val

def setVelocity(self, obj, val):
list = [val[0], val[1], val[2]]
obj.setTag("velocity", cPickle.dumps(list))

def getVelocity(self, obj):
return Vec3(list[0], list[1], list[2])

#para mover suavemente
self.updatePos(dt)

def updatePos(self, dt):
giro = self.pd.getH()
if self.keys["esquerda"]:
giro += dt * 360
self.pd.setH(giro%360)
elif self.keys["direita"]:
giro -= dt * 360
self.pd.setH(giro%360)

elif self.keys["cima"]:
newVel += self.getVelocity(self.pd)
self.setVelocity(self.pd, newVel)

vel = self.getVelocity(self.pd)
newPos = self.pd.getPos() + (vel * dt)
self.pd.setPos(newPos)

w = World()
run()``````

Look christhi, I used the example of Roaming Ralph for walking in direction

``````def loadModelo():
panda = Actor.Actor("models/panda-model", {"walk":"models/panda-walk4"})
panda.reparentTo(render)
class World (DirectObject):
def __init__(self):
self.accept("w-up", self.mudapos,["cima" , 0])
self.accept("s-up", self.mudapos,["baixo" , 0])
self.accept("a-up", self.mudapos,["esquerda" , 0])
self.accept("d-up", self.mudapos,["direita" , 0])

if (self.keyMap("cima")!=0:
self.panda.setY(self.panda,0.5)

if (self.keyMap("baixo")!=0:
self.panda.setY(self.panda,-0.5)

if (self.keyMap("esquerda")!=0:
self.panda.setH(self.panda,0.5)

if (self.keyMap("direita")!=0:
self.panda.setH(self.panda,-0.5)