Thrown projectil from camera

Hi guys,

Newbie here… :mrgreen: This is my case: I’m trying but without successs thrown a projection from camera position. First I created a ball using the Smiley sphere model and then set its position and hpr to the same of base.camera, this way I could use the mouse to direct the camera and by consequence aim the target. The result is bizarre: the ball is thrown from everywhere except from the camera position!

This is the code piece:

base.camera.setPos(0,-45,0)
 
def fire():
    # load the ball model
    self.ball = loader.loadModel("smiley")
    self.ball.reparentTo(render)

    # the ball will be thrown from camera position and in the same direction
    self.ball.setPosHpr(base.camera.getPos(), base.camera.getHpr())
 
    # setup the projectile interval
    self.trajectory = ProjectileInterval(self.ball,
                                         startPos=self.ball.getPos(),
                                         startVel=Point3(15, 0, 0),
                                         duration=5)
    self.trajectory.loop()

Sure I’m misunsdertanding some concept but the question is WHICH… :frowning: Any help is welcome!

Ok, nevermind. I solved this question. I used the method listed by @Hollower here:

For who may interest:

from direct.showbase.ShowBase import ShowBase
from direct.actor.Actor import Actor

app = ShowBase()

app.disableMouse()
app.acceptOnce("mouse3", app.enableMouse)

environ = loader.loadModel("environment")
environ.reparentTo(render)

panda = Actor("panda")
panda.reparentTo(render)
panda.setPos(40,-150,3)

pandaInterval = panda.hprInterval(5, (360,0,0), (0,0,0))
pandaInterval.loop()

bullets = []
bulletSpeed = 50.0
bulletLife = 5.0    # seconds bullet will remain in the scene

class Bullet(object):
        # model is left unparented because it will be copied
        model = loader.loadModel("smiley")
       
        def __init__(self, pos, hpr, speed, life):
            self.node = Bullet.model.copyTo(render)
            self.node.setPosHpr(pos, hpr)
            self.speed = speed
            self.life = life
            self.alive = True
       
        def update(self, dt):
            if not self.alive:
                return
           
            self.life -= dt
           
            if self.life > 0:
                # You should use the "fluid" versions of these functions
                # if you intend them to work with the collision system.
                self.node.setFluidY(self.node, self.speed * dt)
            else:
                self.node.removeNode()
                self.alive = False

    # Every frame this task calls update on each bullet in the bullets list.
    def updateBullets(task):
        dt = globalClock.getDt()
       
        for bullet in bullets:
            bullet.update(dt)
       
        return task.cont

    def shoot():
        global bullets
        bullets = [b for b in bullets if b.alive] # remove any dead bullets
        bullets.append( Bullet(app.camera.getPos(), app.camera.getHpr(), bulletSpeed, bulletLife) )

app.accept("space", shoot)
taskMgr.add(updateBullets, "updateBullets")

app.camera.setPos(40,-250,70)
app.camera.lookAt(panda)

app.run()

Very important: bullet speed must be positive (different from original code which is negative).