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 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()