Better Ralph (3rd person camera, "WASD" controls)

This is my first foray into both Panda3d and Python (I’ve worked on games in c++) so I picked a simple project.

I found the controls and camera for the roaming ralph demo a bit ackward. Today’s 3rd person games have a camera and control style almost identical to first person games and for good reason. Its much more natural.

Use W A S and D keys to move the player in any direction. Use the mouse to look around or aim the player. The 3rd person camera will also avoid collision and obstructions.

### Original Header
#iModels: Jeff Styers, Reagan Heller

# Last Updated: 6/13/2005
#
# This tutorial provides an example of creating a character
# and having it walk around on uneven terrain, as well
# as implementing a fully rotatable camera.

### Better Ralph: modifications by Stephen Lujan
# This is a modification of the roaming ralph demo to provide improved controls
# and camera angles, imitating those of modern commercial games

import direct.directbase.DirectStart
from panda3d.core import WindowProperties
from panda3d.core import CollisionTraverser,CollisionNode
from panda3d.core import CollisionHandlerQueue,CollisionRay
from panda3d.core import CollisionTube,CollisionSegment
from panda3d.core import Filename,AmbientLight,DirectionalLight
from panda3d.core import PandaNode,NodePath,Camera,TextNode
from panda3d.core import Point3,Vec3,Vec4,BitMask32
from panda3d.core import LightRampAttrib
from direct.gui.OnscreenText import OnscreenText
from direct.actor.Actor import Actor
from direct.showbase.DirectObject import DirectObject
import random, sys, os, math

SPEED = 0.5

# Function to put instructions on the screen.
def addInstructions(pos, msg):
    return OnscreenText(text=msg, style=1, fg=(1,1,1,1),
                        pos=(-1.3, pos), align=TextNode.ALeft, scale = .05)

# Function to put title on the screen.
def addTitle(text):
    return OnscreenText(text=text, style=1, fg=(1,1,1,1),
                        pos=(1.3,-0.95), align=TextNode.ARight, scale = .07)

class World(DirectObject):

    def __init__(self):

        self.controlMap = {"left":0, "right":0, "forward":0, "backward":0,
            "zoom-in":0, "zoom-out":0, "wheel-in":0, "wheel-out":0}
        self.mousebtn = [0,0,0]
        base.win.setClearColor(Vec4(0,0,0,1))

        # Post the instructions

        self.title = addTitle("Panda3D Tutorial: Better Ralph (Walking on Uneven Terrain)")
        self.inst1 = addInstructions(0.95, "[ESC]: Quit")
        self.inst2 = addInstructions(0.90, "W A S D keys move Ralph forward, left, back, and right, respectively.")
        self.inst3 = addInstructions(0.85, "Use the mouse to look around and steer Ralph.")
        self.inst4 = addInstructions(0.80, "Zoom in and out using the mouse wheel, or page up and page down keys.")

        # Set up the environment
        #
        # This environment model contains collision meshes.  If you look
        # in the egg file, you will see the following:
        #
        #    <Collide> { Polyset keep descend }
        #
        # This tag causes the following mesh to be converted to a collision
        # mesh -- a mesh which is optimized for collision, not rendering.
        # It also keeps the original mesh, so there are now two copies ---
        # one optimized for rendering, one for collisions.

        self.environ = loader.loadModel("models/world")
        self.environ.reparentTo(render)
        self.environ.setPos(0,0,0)

        # Create the main character, Ralph

        ralphStartPos = self.environ.find("**/start_point").getPos()
        self.ralph = Actor("models/ralph",
                                 {"run":"models/ralph-run",
                                  "walk":"models/ralph-walk"})
        self.ralph.reparentTo(render)
        self.ralph.setScale(.2)
        self.ralph.setPos(ralphStartPos)

        # Accept the control keys for movement and rotation

        self.accept("escape", sys.exit)
        self.accept("w", self.setControl, ["forward",1])
        self.accept("a", self.setControl, ["left",1])
        self.accept("s", self.setControl, ["backward",1])
        self.accept("d", self.setControl, ["right",1])
        self.accept("w-up", self.setControl, ["forward",0])
        self.accept("a-up", self.setControl, ["left",0])
        self.accept("s-up", self.setControl, ["backward",0])
        self.accept("d-up", self.setControl, ["right",0])
#        self.accept("mouse1", self.setControl, ["zoom-in", 1])
#        self.accept("mouse1-up", self.setControl, ["zoom-in", 0])
#        self.accept("mouse3", self.setControl, ["zoom-out", 1])
#        self.accept("mouse3-up", self.setControl, ["zoom-out", 0])
        self.accept("wheel_up", self.setControl, ["wheel-in", 1])
        self.accept("wheel_down", self.setControl, ["wheel-out", 1])
        self.accept("page_up", self.setControl, ["zoom-in", 1])
        self.accept("page_up-up", self.setControl, ["zoom-in", 0])
        self.accept("page_down", self.setControl, ["zoom-out", 1])
        self.accept("page_down-up", self.setControl, ["zoom-out", 0])

        taskMgr.add(self.move,"moveTask")

        # Game state variables
        self.isMoving = False

        # Set up the camera
        # Adding the camera to Ralph is a simple way to keep the camera locked
        # in behind Ralph regardless of ralph's movement.
        base.camera.reparentTo(self.ralph)
        # We don't actually want to point the camera at Ralph's feet.
        # This value will serve as a vertical offset so we can look over Ralph
        self.cameraTargetHeight = 6.0
        # How far should the camera be from Ralph
        self.cameraDistance = 30
        # Initialize the pitch of the camera
        self.cameraPitch = 10
        # This just disables the built in camera controls; we're using our own.
        base.disableMouse()
        # The mouse moves rotates the camera so lets get rid of the cursor
        props = WindowProperties()
        props.setCursorHidden(True)
        base.win.requestProperties(props)
        

        # We will detect the height of the terrain by creating a collision
        # ray and casting it downward toward the terrain.  One ray will
        # start above ralph's head.
        # A ray may hit the terrain, or it may hit a rock or a tree.  If it
        # hits the terrain, we can detect the height.  If it hits anything
        # else, we rule that the move is illegal.

        self.cTrav = CollisionTraverser()

        self.ralphGroundRay = CollisionRay()
        self.ralphGroundRay.setOrigin(0,0,1000)
        self.ralphGroundRay.setDirection(0,0,-1)
        self.ralphGroundCol = CollisionNode('ralphRay')
        self.ralphGroundCol.addSolid(self.ralphGroundRay)
        self.ralphGroundCol.setFromCollideMask(BitMask32.bit(0))
        self.ralphGroundCol.setIntoCollideMask(BitMask32.allOff())
        self.ralphGroundColNp = self.ralph.attachNewNode(self.ralphGroundCol)
        self.ralphGroundHandler = CollisionHandlerQueue()
        self.cTrav.addCollider(self.ralphGroundColNp, self.ralphGroundHandler)

        # We will detect anything obstructing the camera's view of the player

        self.cameraRay = CollisionSegment((0,0,self.cameraTargetHeight),(0,5,5))
        self.cameraCol = CollisionNode('cameraRay')
        self.cameraCol.addSolid(self.cameraRay)
        self.cameraCol.setFromCollideMask(BitMask32.bit(0))
        self.cameraCol.setIntoCollideMask(BitMask32.allOff())
        self.cameraColNp = self.ralph.attachNewNode(self.cameraCol)
        self.cameraColHandler = CollisionHandlerQueue()
        self.cTrav.addCollider(self.cameraColNp, self.cameraColHandler)

############## CollisionTube doesn't seem to be working
#        self.cameraRay = CollisionTube( (0,0,self.cameraTargetHeight),
#                                        (0,25,25),
#                                        (self.cameraTargetHeight/2))
#        self.cameraCol = CollisionNode('cameraRay')
#        self.cameraCol.addSolid(self.cameraRay)
#        self.cameraCol.setFromCollideMask(BitMask32.bit(0))
#        self.cameraCol.setIntoCollideMask(BitMask32.allOff())
#        self.cameraColNp = self.ralph.attachNewNode(self.cameraCol)
#        self.cameraColHandler = CollisionHandlerQueue()
#        self.cTrav.addCollider(self.cameraColNp, self.cameraColHandler)
############

        # Uncomment this line to see the collision rays
        #self.ralphGroundColNp.show()
        #self.camGroundColNp.show()
        #self.cameraColNp.show()

        # Uncomment this line to show a visual representation of the
        # collisions occuring
        #self.cTrav.showCollisions(render)

        # Create some lighting
        # lets add hdr lighting for fun
        render.setShaderAuto()
        render.setAttrib(LightRampAttrib.makeHdr1())
        ambientLight = AmbientLight("ambientLight")
        # existing lighting is effectively darkened so boost ambient a bit
        ambientLight.setColor(Vec4(.4, .4, .4, 1))
        directionalLight = DirectionalLight("directionalLight")
        directionalLight.setDirection(Vec3(-5, -5, -5))
        # hdr can handle any amount of lighting
        # lets make things nice and sunny
        directionalLight.setColor(Vec4(2.0, 2.0, 2.0, 1.0))
        directionalLight.setSpecularColor(Vec4(2.0, 2.0, 2.0, 1))
        render.setLight(render.attachNewNode(ambientLight))
        render.setLight(render.attachNewNode(directionalLight))

    #Records the state of the arrow keys
    def setControl(self, key, value):
        self.controlMap[key] = value


    # Accepts arrow keys to move either the player or the menu cursor,
    # Also deals with grid checking and collision detection
    def move(self, task):

        # save ralph's initial position so that we can restore it,
        # in case he falls off the map or runs into something.

        startpos = self.ralph.getPos()

        # If a move-key is pressed, move ralph in the specified direction.
        if (self.controlMap["forward"]!=0):
            self.ralph.setY(self.ralph, -25 * globalClock.getDt())
        if (self.controlMap["backward"]!=0):
            self.ralph.setY(self.ralph, 25 * globalClock.getDt())
        if (self.controlMap["left"]!=0):
            self.ralph.setX(self.ralph, 25 * globalClock.getDt())
        if (self.controlMap["right"]!=0):
            self.ralph.setX(self.ralph, -25 * globalClock.getDt())

        # If a zoom button is pressed, zoom in or out
        if (self.controlMap["wheel-in"]!=0):
            self.cameraDistance -= 0.1 * self.cameraDistance;
            if (self.cameraDistance < 5):
                self.cameraDistance = 5
            self.controlMap["wheel-in"] = 0
        elif (self.controlMap["wheel-out"]!=0):
            self.cameraDistance += 0.1 * self.cameraDistance;
            if (self.cameraDistance > 250):
                self.cameraDistance = 250
            self.controlMap["wheel-out"] = 0
        if (self.controlMap["zoom-in"]!=0):
            self.cameraDistance -= globalClock.getDt() * self.cameraDistance;
            if (self.cameraDistance < 5):
                self.cameraDistance = 5
        elif (self.controlMap["zoom-out"]!=0):
            self.cameraDistance += globalClock.getDt() * self.cameraDistance;
            if (self.cameraDistance > 250):
                self.cameraDistance = 250

        # Use mouse input to turn both Ralph and the Camera
        if base.mouseWatcherNode.hasMouse():
            # get changes in mouse position
            md = base.win.getPointer(0)
            x = md.getX()
            y = md.getY()
            deltaX = md.getX() - 200
            deltaY = md.getY() - 200
            # reset mouse cursor position
            base.win.movePointer(0, 200, 200)
            # alter ralph's yaw by an amount proportionate to deltaX
            self.ralph.setH(self.ralph.getH() - 0.3* deltaX)
            # find the new camera pitch and clamp it to a reasonable range
            self.cameraPitch = self.cameraPitch + 0.1 * deltaY
            if (self.cameraPitch < -60): self.cameraPitch = -60
            if (self.cameraPitch >  80): self.cameraPitch =  80
            base.camera.setHpr(0,self.cameraPitch,0)
            # set the camera at around ralph's middle
            # We should pivot around here instead of the view target which is noticebly higher
            base.camera.setPos(0,0,self.cameraTargetHeight/2)
            # back the camera out to its proper distance
            base.camera.setY(base.camera,self.cameraDistance)

        # point the camera at the view target
        viewTarget = Point3(0,0,self.cameraTargetHeight)
        base.camera.lookAt(viewTarget)
        # reposition the end of the  camera's obstruction ray trace
        self.cameraRay.setPointB(base.camera.getPos())


        # If ralph is moving, loop the run animation.
        # If he is standing still, stop the animation.

        if (self.controlMap["forward"]!=0) or (self.controlMap["left"]!=0) or (self.controlMap["right"]!=0) or (self.controlMap["backward"]!=0):
            if self.isMoving is False:
                self.ralph.loop("run")
                self.isMoving = True
        else:
            if self.isMoving:
                self.ralph.stop()
                self.ralph.pose("walk",5)
                self.isMoving = False

        # Now check for collisions.

        self.cTrav.traverse(render)

        # Adjust ralph's Z coordinate.  If ralph's ray hit terrain,
        # update his Z. If it hit anything else, or didn't hit anything, put
        # him back where he was last frame.

        entries = []
        for i in range(self.ralphGroundHandler.getNumEntries()):
            entry = self.ralphGroundHandler.getEntry(i)
            entries.append(entry)
        entries.sort(lambda x,y: cmp(y.getSurfacePoint(render).getZ(),
                                     x.getSurfacePoint(render).getZ()))
        if (len(entries)>0) and (entries[0].getIntoNode().getName() == "terrain"):
            self.ralph.setZ(entries[0].getSurfacePoint(render).getZ())
        else:
            self.ralph.setPos(startpos)

        # We will detect anything obstructing the camera via a ray trace
        # from the view target around the avatar's head, to the desired camera
        # podition. If the ray intersects anything, we move the camera to the
        # the first intersection point, This brings the camera in between its
        # ideal position, and any present obstructions.

        entries = []
        for i in range(self.cameraColHandler.getNumEntries()):
            entry = self.cameraColHandler.getEntry(i)
            entries.append(entry)
        entries.sort(lambda x,y: cmp(-y.getSurfacePoint(self.ralph).getY(),
                                     -x.getSurfacePoint(self.ralph).getY()))
        if (len(entries)>0):
            collisionPoint =  entries[0].getSurfacePoint(self.ralph)
            collisionVec = ( viewTarget - collisionPoint)
            if ( collisionVec.lengthSquared() < self.cameraDistance * self.cameraDistance ):
                base.camera.setPos(collisionPoint)
                if (entries[0].getIntoNode().getName() == "terrain"):
                    base.camera.setZ(base.camera, 0.2)
                base.camera.setY(base.camera, 0.3)

        return task.cont

w = World()
run()

For me it says “cannot find [‘modules/world’]”

you need to run it in the same folder as the original roaming ralph example, that’s where the /models are…
Or copy your models folder from the sample folder, but solution above is more logic.

And indeed, interesting stuff.

1 Like

Seems nice. Personally I like how I did WASD control better than your solution, but I didn’t do mouse stuff yet, so I’ll steal that :slight_smile:

My version of Ralph incase you are interested: discourse.panda3d.org/viewtopic.php?t=7304

Specifically my WASD controls:
(see github.com/Craig-Macomber/Panda3 … er/main.py look inside the World class, and ignore the other mess)

class keyTracker(DirectObject):
    """
    Class for tracking the state of keys. keyMap holds if a key is down
    Multiple keys can map to one name, though the value will be set to false when the first is relased
    """
    def __init__(self):
        DirectObject.__init__(self)
        self.keyMap = {}
        
    def setKey(self, key, value):
        """Records the state of key"""
        self.keyMap[key] = value
    
    def addKey(self,key,name,allowShift=True):
        self.accept(key, self.setKey, [name,True])
        self.accept(key+"-up", self.setKey, [name,False])  
        self.accept(key.upper()+"-up", self.setKey, [name,False])
        
        if allowShift:
            self.addKey("shift-"+key,name,False)
        
        self.keyMap[name]=False
 

Edit: I added your mouse control code (though not quite all the features yet, and I messed with some stuff) to my project and pushed it up to git hub. Works well. Thanks.

You’re quite right. Your keyTracker class is a much cleaner solution. I wasn’t really aiming to do a complete rewrite of the roaming ralph code though so I used its existing method of key-binding. Also the mouse movement control was taken from another one of the samples though I think in the end I modified it a lot. Even the camera collision code wasn’t written from scratch, I started out by copying bits of code from the Ralph’s collision raytrace.

Overall I guess it was hardly a noteworthy project, but it never hurts to post it online and see if anyone can benefit.

Wow, this is great, thanks for sharing. I learned a lot :smiley:

Nice! I’ll be studying this example for sure! Thanks for sharing with us. :slight_smile:

but it never hurts to post it online and see if anyone can benefit.

thank you so much :smiley:
I learned GREATLY from this.

This helped me. Thank you for your time and sharing.

I, like a lot of people probably, aspire to make my own 3d game and don’t really want a big bloated starting point. This is perfect since I can still make since of what’s there so I can change it how I want.

Hi,

Thanks. I was studying this script but the camera controlled mouse is unplayable slow and it jitters a lot.

Anyone else with this issue?

Working on this config:

Model: MacBookPro5,2, BootROM MBP52.008E.B05, 2 processors, Intel Core 2 Duo, 2.8 GHz, 4 GB, SMC 1.42f4
OS X: 10.9.1
Graphics: NVIDIA GeForce 9400M, NVIDIA GeForce 9400M, PCI, 256 MB
Graphics: NVIDIA GeForce 9600M GT, NVIDIA GeForce 9600M GT, PCIe, 512 MB

Thanks

I also tried to run it on a VM running WinXP to no avail.

But In a WinXp PC no problem at all.

Should I post a bug report somewhere?

What do you think of this? It simulates your typical console 3rd person camera and control scheme on the limited WASD configuration, plus two other buttons for horizontal camera rotation. I took inspiration from Dreamcast and Saturn platformers where most of the time camera rotation was applied to the shoulder buttons.

def move(self, task):

		# If the camera-left key is pressed, move camera left.
		# If the camera-right key is pressed, move camera right.
		base.camera.lookAt(self.ralph)
		if (self.keyMap["cam-left"] != 0):
			base.camera.setX(base.camera, - 15 * globalClock.getDt())
		if (self.keyMap["cam-right"] != 0):
			base.camera.setX(base.camera, + 15 * globalClock.getDt())
		
		# Define variable to get zeroed out each frame	
		startpos = self.ralph.getPos()
		self.moveVector = Vec3()
		
		# Change move vector by pressing keys
		if (self.keyMap["left"] != 0):
			self.moveVector.setX(-1)
		if (self.keyMap["right"] != 0):
			self.moveVector.setX(1)
		if (self.keyMap["forward"] != 0):
			self.moveVector.setY(1)
		if (self.keyMap["backward"] != 0):
			self.moveVector.setY(-1)
		
		# Transform the move vector relatively to camera and apply it as motion
		# as well as make Ralph face the direction he is running
		self.newVector = render.getRelativeVector(base.camera, self.moveVector)
		self.newPoint = startpos - self.newVector
		if self.newVector.length() > 1:
			self.newVector.normalize()
		self.newVector *= 5
		self.newVector *= globalClock.getDt()
		self.ralph.setPos(self.ralph.getPos() + self.newVector)
		if self.moveVector.length() != 0:
			self.ralph.lookAt(self.newPoint)