Problem with the cam.

I cant to fix my cam to watch behind the palyer.

You’ll need to give more information, otherwise we have no idea what you’re trying to do, what you’ve tried so far, and where you’re stuck.

I want to make the view from the sky(like diablo) or behind the player(like freedom fighters) but alawys there is an erorr in the code like this:

import direct.directbase.DirectStart
from direct.task import Task 
from direct.actor import Actor
from pandac.PandaModules import * 
import math


#Load the first environment mode
environ = loader.loadModel("models/ssw.egg")
environ.reparentTo(render)
environ.setScale(0.25,0.25,0.25)
environ.setPos(-8,42,0)

ssw = loader.loadModel("models/ssw.egg")

player = ssw 

# Correct the perspective before getting the distance 
oldP = base.cam.getP() 
base.cam.setP(0) 
                       
# Get the x and y distance beetwen the player and the camera 
x = base.cam.getX(player) 
y = base.cam.getY(player) 

# The method for getting this angle is up to you 
a = 60 
                       
# Linear transform to rotate around a point 
w1 = x * cos(a) - y * sin(a) 
w2 = x * sin(a) + y * cos(a) 

# Set the new position 
base.cam.setX(player, w1) 
base.cam.setY(player, w2) 
                       
# Rotate the camera towards the player 
base.cam.lookAt(player) 

# Reset the pitch to its original value 
base.cam.setP(oldP)
 
run()

and the error:

try math.cos ( and math.sin)

or from math import sin, cos

You really don’t want to use sin/cos for this, create a dummy node instead, reparent the camera to the dummy node, offset it and rotate the dummy node.

Oh realy I will not math cos and sin every frame.But with the dummy there is no efect.May be I have make error.Look:

import direct.directbase.DirectStart
from direct.task import Task 
from direct.actor import Actor
from pandac.PandaModules import * 
import math
from math import sin
from math import cos

#Load the first environment mode
environ = loader.loadModel("models/ssw.egg")
environ.reparentTo(render)
environ.setScale(0.25,0.25,0.25)
environ.setPos(-8,42,0)

#base.disableMouse()


ssw = loader.loadModel("models/ssw.egg")

player = ssw 

dummy=render.attachNewNode("models/ssw.egg") 
base.camera.reparentTo(dummy) 
base.camera.setPos(4,42,0) # 10 = distance between cam and point 
base.camera.setH(180) #this will rotate it 60 degrees around the point
 
run()

This is wrong:

base.camera.setH(180) #this will rotate it 60 degrees around the point

Do this instead:

dummy.setH(180) #this will rotate it 60 degrees around the point

Right.But now I cant to move the cam with the

base.camera.setPos(200,82,100) # 10 = distance between cam and point 

because there is no effect.I thing that it is wrong.

Move the dummy instead, then.

Like this:

dummy.setPos(200,82,100) # 10 = distance between cam and point 

Offfffff.I MAKE IT.Thaks for the help.Here the right code:

base.disableMouse()


mydummy = loader.loadModel("location of mydummy.egg")

player = mydummy 

dummy=render.attachNewNode("location of mydummy.egg") 
base.camera.reparentTo(dummy) 
dummy.setPos(30,30,10) # 10 = distance between cam and point 
dummy.setH(60) #this will rotate it 60 degrees around the point

base.cam.lookAt(mydummy)

That code alone would never do it, sorry. But if you achieve a Diablo-like camera, i.e. a cam, that is fixed relative to the player, moves with him and the environment moves around … please post the code, I am trying that too.

Right now I use the method that others use too:

taskMgr.add(self.camerafollow, "camerafollowTask", priority = 30)

def camerafollow(self, task):		
base.camera.setPos(self.spaceship.getX() -6, self.spaceship.getY() -6, 16)
base.camera.lookAt(self.spaceship)		
return Task.cont

Now that works and thanks to the priority number its smooth. Now, you dont find this documented. priority 50 and it does not work anymore. 20, 25, 30, 34, 35 all work.
Another thing is that I don’t even see why one would need a task for this.

What are you trying to do for which just parenting the camera to the player wouldn’t work? If you want a smooth but potentially delayed motion, i.e. a camera ‘sprung’ to the player, you do fundamentally need a task updating the camera independently of when the player moves. Something as simple as

distance=target.getPos(camera)
camera.setPos(camera,distance*n*dt)

in a task would get you close.