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. :unamused:

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 direct.task.Task import Task
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

DEG_TO_RAD = pi/180 #translates degrees to radians for sin and cos
ACCELERATION = 10 #Ship acceleration in units/sec/sec

def loadModelo():
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
def giramundo(task):
m = base.camera.getX() + 2
base.camera.setPos(m,m,m)
return task.cont

class World (DirectObject):
def init(self):
#carrega o fundo
cenario = loader.loadModel(“models/environment”)
#associa para carregar render e o topo da arvore do que e renderizado
cenario.reparentTo(render)
cenario.setScale(0.25)

#carrega modelo parado
#panda = loader.loadModel("models/panda-model")
#panda.reparentTo(render)
#panda.setScale(0.005)
#panda.setPos(0,-30,0)
self.pd = loadModelo()
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])

#executa a task comando que repete a cada render
self.gametask = taskMgr.add(self.gameloop,"gameloop")
self.gametask.last = 0         #Task time of the last frame

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):
list = cPickle.loads(obj.getTag(“velocity”))
return Vec3(list[0], list[1], list[2])

   #para mover suavemente

def gameloop(self, task):
dt = task.time - task.last
task.last = task.time
self.updatePos(dt)
return Task.cont

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”]:
giro_rad = (DEG_TO_RAD * giro) * (-1)
newVel = (Vec3(sin(giro_rad), cos(giro_rad),0 ) * ACCELERATION * dt)
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 direct.task.Task import Task 
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 

DEG_TO_RAD = pi/180 #translates degrees to radians for sin and cos 
ACCELERATION = 10 #Ship acceleration in units/sec/sec 

def loadModelo(): 
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 
def giramundo(task): 
m = base.camera.getX() + 2 
base.camera.setPos(m,m,m) 
return task.cont 



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

#carrega modelo parado 
#panda = loader.loadModel("models/panda-model") 
#panda.reparentTo(render) 
#panda.setScale(0.005) 
#panda.setPos(0,-30,0) 
self.pd = loadModelo() 
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]) 

#executa a task comando que repete a cada render 
self.gametask = taskMgr.add(self.gameloop,"gameloop") 
self.gametask.last = 0 #Task time of the last frame 

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): 
list = cPickle.loads(obj.getTag("velocity")) 
return Vec3(list[0], list[1], list[2]) 

#para mover suavemente 
def gameloop(self, task): 
dt = task.time - task.last 
task.last = task.time 
self.updatePos(dt) 
return Task.cont 

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"]: 
giro_rad = (DEG_TO_RAD * giro) * (-1) 
newVel = (Vec3(sin(giro_rad), cos(giro_rad),0 ) * ACCELERATION * dt) 
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 direct.task.Task import Task
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

DEG_TO_RAD = pi/180 #translates degrees to radians for sin and cos
ACCELERATION = 10   #Ship acceleration in units/sec/sec

def loadModelo():
	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
def giramundo(task):
	m = base.camera.getX() + 2
	base.camera.setPos(m,m,m)
	return task.cont


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

	#carrega modelo parado
	#panda = loader.loadModel("models/panda-model")
	#panda.reparentTo(render)
	#panda.setScale(0.005)
	#panda.setPos(0,-30,0)
	self.pd = loadModelo()
	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])
	
	#executa a task comando que repete a cada render
	self.gametask = taskMgr.add(self.gameloop,"gameloop")
	self.gametask.last = 0         #Task time of the last frame
	
  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):
    list = cPickle.loads(obj.getTag("velocity"))
    return Vec3(list[0], list[1], list[2])
   
	   #para mover suavemente
  def gameloop(self, task):
	dt = task.time - task.last
	task.last = task.time
	self.updatePos(dt)
	return Task.cont
  
  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"]:
	 giro_rad = (DEG_TO_RAD * giro) * (-1)
	 newVel = (Vec3(sin(giro_rad), cos(giro_rad),0 ) *  ACCELERATION * dt)
	 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])
    taskMgr.add(self.move,"moveTask")

  def move(self,task):
    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)
    return Task.cont

I wish that works well with you

thanks D_A_M_H for your help. but i´m learning panda and to be clear in the forum. :unamused:

this code works, but i want move in the X and Y directions at the same time, the direction that the actor is facing. like the ship in asteroids, but without the gravity move. in the -up command, the actor stops totally.

I think this is part of that basic commands, like characters w, a, s and d to move, space to jump, mouse to change camera, mouse 1 fire… it´s the same for every games but the first time is hard!!!
thanks for all

my topic is lost!