~fireclawthefox/panda3dcodecollection/trunk

« back to all changes in this revision

Viewing changes to character/extended Char controller/CameraThirdPerson.py

  • Committer: Fireclaw the Fox
  • Date: 2016-12-07 22:29:15 UTC
  • Revision ID: fireclawthefox@gmail.com-20161207222915-0fpexwjrwn46dh83
Bugfixes and new Features

Improved third person camera handling
Add camera system switch function
Fix for tiny panda renderer users
Added time to allow jump after fall
Critical performance improvement
Some work on bullet integration (still early WIP)
Made upward (front collision) wall run highest prio check instead of left wall run
Some minor cleanup
Updated INFO file

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
        the defined model and will correct the position if the cam is to close
63
63
        or to far away"""
64
64
 
 
65
        # Camera Movement Updates
 
66
        camvec = self.cam_floater.getPos(render) - camera.getPos(render)
 
67
        camvec.setZ(0)
 
68
        camdist = camvec.length()
 
69
        camvec.normalize()
65
70
 
66
71
        # check if the camera should be centered
67
72
        if self.parent.isDown(self.parent.key_center_camera):
86
91
            if base.win.movePointer(0, self.parent.win_width_half, self.parent.win_height_half):
87
92
                # Vertical positioning
88
93
                mouse_y = mw.getMouseY()
89
 
                z = mouse_y * self.parent.mouse_speed_y * dt
 
94
                z = mouse_y * self.parent.mouse_speed_y * camdist * dt
90
95
                if self.parent.mouse_invert_vertical:
91
96
                    camera.setZ(camera, z)
92
97
                else:
94
99
 
95
100
                # Horizontal positioning
96
101
                mouse_x = mw.getMouseX()
97
 
                x = mouse_x * self.parent.mouse_speed_x * dt
 
102
                x = mouse_x * self.parent.mouse_speed_x * camdist * dt
98
103
                if self.parent.mouse_invert_horizontal:
99
104
                    camera.setX(camera, x)
100
105
                else:
136
141
            self.last_platform_position_cam = None
137
142
            self.last_platform_rotation_cam = None
138
143
 
139
 
        # Camera Movement Updates
140
 
        camvec = self.cam_floater.getPos(render) - camera.getPos(render)
141
 
        camvec.setZ(0)
142
 
        camdist = camvec.length()
143
 
        camvec.normalize()
144
 
 
145
 
        # camera collision detection
146
 
        # always set the cameras position to the first hitpoint on a collision solid
147
 
        pos = self.parent.getFirstCollisionInLine(self.cam_floater.getPos(render), camera.getPos(render))
148
 
        had_ray_collision = False
149
 
        if pos is not None:
150
 
            had_ray_collision = True
151
 
            camera.setPos(pos)
152
 
 
153
 
        # If the camera is to far from player start following
154
 
        if camdist > self.parent.max_cam_distance:
155
 
            camera.setPos(camera.getPos()+camvec*(camdist-self.parent.max_cam_distance))
156
 
            camdist = self.parent.max_cam_distance
157
 
 
158
 
        # If player is to close move the camera backwards
159
 
        if camdist < self.parent.min_cam_distance:
160
 
            if not had_ray_collision:
161
 
                # move the camera backwards
162
 
                camera.setPos(camera.getPos()-camvec*(self.parent.min_cam_distance-camdist))
163
 
                camdist = self.parent.min_cam_distance
164
 
            else:
165
 
                # we can't move the camera back into the wall, so move it up
166
 
                #TODO: Maybe change max_cam_height_distance with something like
167
 
                #      self.parent.min_cam_dist - camdist
168
 
                camera.setZ(self.cam_floater.getZ() + self.parent.max_cam_height_distance)
169
 
 
170
144
        # Get the cameras current offset to the player model on the z-axis
171
145
        offsetZ = camera.getZ(render) - self.cam_floater.getZ(render)
172
146
        # check if the camera is within the min and max z-axis offset
200
174
            #    # set the cam z position to exactly the desired offset
201
175
            #    camera.setZ(self.cam_floater.getZ(render) + self.parent.cam_height_avg_up)
202
176
 
 
177
        # If the camera is to far from player start following
 
178
        if camdist > self.parent.max_cam_distance:
 
179
            camera.setPos(camera.getPos()+camvec*(camdist-self.parent.max_cam_distance))
 
180
            camdist = self.parent.max_cam_distance
 
181
 
 
182
        # camera collision detection
 
183
        # always set the cameras position to the first hitpoint on a collision solid
 
184
        had_ray_collision = False
 
185
        entry = self.parent.getFirstCollisionEntryInLine(self.cam_floater.getPos(render), camera.getPos(render))
 
186
        if entry is not None:
 
187
            pos = None
 
188
            if entry.hasSurfacePoint():
 
189
                pos = entry.getSurfacePoint(render)
 
190
            wall_normal = None
 
191
            if entry.hasSurfaceNormal():
 
192
                wall_normal = entry.getSurfaceNormal(render)
 
193
                pos.setX(pos.getX() + wall_normal.getX()/2.0)
 
194
                pos.setY(pos.getY() + wall_normal.getY()/2.0)
 
195
            #pos = self.parent.getFirstCollisionInLine(self.cam_floater.getPos(render), camera.getPos(render))
 
196
            if pos is not None:
 
197
                had_ray_collision = True
 
198
                camera.setPos(pos)
 
199
 
 
200
        # If player is to close move the camera backwards
 
201
        if camdist < self.parent.min_cam_distance:
 
202
            if not had_ray_collision:
 
203
                # move the camera backwards
 
204
                camera.setPos(camera.getPos()-camvec*(self.parent.min_cam_distance-camdist))
 
205
                camdist = self.parent.min_cam_distance
 
206
            else:
 
207
                # we can't move the camera back into the wall, so move it up
 
208
                #TODO: Maybe change max_cam_height_distance with something like
 
209
                #      self.parent.min_cam_dist - camdist
 
210
                camera.setZ(self.cam_floater.getZ() + self.parent.max_cam_height_distance)
 
211
 
203
212
        camera.lookAt(self.cam_floater)
204
213
        return task.cont