chasecam function explanation
Blitz3D Forums/Blitz3D Beginners Area/chasecam function explanation
| ||
can someone help me explain what everything thats happening in this function and chasecams i want to understand chasecams better by documenting and making a tutorial on it ;) thanks chris |
| ||
what function?...you haven't posted anything? |
| ||
oh sorry here it isFunction UpdateChaseCam( c.chasecam ) If KeyDown(200) ; someone hits the up arrow TranslateEntity c\heading,0,-3,0 ; move the heading back by 3 if you hit the down arrow Else If KeyDown(208) ; someone hits the down arrow TranslateEntity c\heading,0,+3,0 ; move the heading foward by 3 if you hit the up arrow EndIf dx#=EntityX(c\target,True)-EntityX(c\camera,True) ; set the camera's new position dy#=EntityY(c\target,True)-EntityY(c\camera,True) dz#=EntityZ(c\target,True)-EntityZ(c\camera,True) TranslateEntity c\camera,dx*.1,dy*.1,dz*.1 PointEntity c\camera,c\heading PositionEntity c\target,0,0,0 ResetEntity c\target PositionEntity c\target,0,3,-10 End Function Function CreateChaseCam.ChaseCam( camera,entity ) c.ChaseCam=New ChaseCam ; create new instance of the type chasecam c\entity=entity ; set the entity field to the entity your pointed at c\camera=camera ; set the camera field to the camera your using c\target=CreatePivot( entity ) ; create a pivot for your target located at the entity your pointed at parented to your entity PositionEntity c\target,0,3,-10 ; now position the target 3 units up, and 10 units back from the entity EntityType c\target,TYPE_TARGET ; now set the type to target (i guess for terrain checking) c\heading=CreatePivot( entity ) ; creating another pivot for the heading of the cam parented to the entity PositionEntity c\heading,0,0,20 ; position the heading 20 units in front of your entity CameraRange c\camera, 0.05, 1000 ; set the camera range (viewable distance to 1000 units) Return c ; return chasecam instance End Function |
| ||
ive documented some of it..hope its right the rest im trying to make sense with |
| ||
You may want to include the definition of the ChaseCam type. |
| ||
ok will do now: Type ChaseCam Field entity,camera,target,heading,sky End Type |
| ||
trying to understand chase cams better - how they work |
| ||
if someone could briefly explain the concepts of how to create a chasecam, I would appreciate it. |
| ||
The way I implement a chase cam (I got this from Rob Farley), is to create a pivot. Now, the pivot is called "Cam_Final", is the place where the camera will stop chasing, should the mesh/entity be stopped. This is where the camera will rest. It is parented to the mesh/entity you want to follow. The camera will, every loop, move towards this pivot, whilst looking at the mesh/entity your following. Remember the camera isn't following the entity, rather the pivot attached to it. Now, to achieve this, you only need 3 simple lines of code, within a function: ; entity refers to the entity to be followed ; camera refers to the camera you wish do to the following ; Cam_Final is the pivot that will hold the location for the camera to chase ; speed is how quickly the camera will move towards the resting point. The larger the number, the slower the movement. You do not need to pass this value to the function. It will default to 30. Function update_chase_cam(camera,entity,speed#=30) PointEntity camera,Cam_Final MoveEntity camera,0,0,EntityDistance#(entity,chase_cam)/speed PointEntity camera,entity End Function Now to use that function, you simply create your camera, entity and your pivot. Something like: [code] Graphics3d 800,600 SetBuffer BackBuffer() Global camera = CreateCamera() Global Cam_Final = CreatePivot() Global entity = CreateSphere() EntityParent Cam_Final, camera PositionEntity Cam_Final,0,2,-20 ; the position you want the camera to be, behind the entity. While Not KeyHit(1) If KeyDown(200) then MoveEntity entity,0,0,1 If Keydown(208) then MoveEntity entity,0,0,-1 If KeyDown(203) then TurnEntity entity,0,-1,0 If KeyDown(205) then TurnEntity entity,0,1,0 Update_Chase_Cam(camera,entity,30) UpdateWorld RenderWorld Flip Wend ; entity refers to the entity to be followed ; camera refers to the camera you wish do to the following ; Cam_Final is the pivot that will hold the location for the camera to chase ; speed is how quickly the camera will move towards the resting point. The larger the number, the slower the movement. You do not need to pass this value to the function. It will default to 30. Function update_chase_cam(camera,entity,speed#=30) PointEntity camera,Cam_Final MoveEntity camera,0,0,EntityDistance#(entity,chase_cam)/speed PointEntity camera,entity End Function /[code] Unfortunately I'm at work, and I can't test any of that. But that is the basic way i would do it. |
| ||
thank you Sir :) may I use your input (with your credits on it) for a tutorial site im putting together on blitz tutorials? |
| ||
I'm having trouble visualizing this particular snippet:TranslateEntity c\camera,dx*.1,dy*.1,dz*.1 ;Moves the camera closer to the "static spot" with a smoothing factor. This line's ok. PointEntity c\camera,c\heading ;Ok too PositionEntity c\target,0,0,0 ;? ResetEntity c\target ;reset the collisions for the target pivot PositionEntity c\target,0,3,-10 ;? I think it's the part that eliminates the sliding-response flickering. That's why I'm so curious about how it works (I know what each command does, mind you. I just didn't grasp what those PositionEntities are doing exactly). |
| ||
I didn't read all that code, but the last (omitted) PositionEntity command parameter defaults to false, hence 0,0,0 is meant local, so exactly at the third persons head. Resetentity resets the collisionhandler. Positionentity 0,3,-10 then positions the target pivot just behind the person. If there is a wall or so, the target pivot might collide, with its back aganst the wall sotosay, preventing it from losing the player out of its view. This way it is however possible to turn around near a wall to push the camera into the player mesh. Some engines solve this problem by fading the player mesh to transparency in this situation (eg. early thomb raider releases) Basicly it's a nice system. Like most systems it does these significant steps: -Try to find a position behind the player with no things between the player and the camera (so sometimes it needs to get closer automaticly) -Make sure not to get stuck in level geometry (the cam) -make the camera smoothly follow the cam-position pivot (target) There are other solutions, like the use of linepicks, but this way it is done pretty well. |
| ||
I am trying to implement the following but am very stuck: in code how do you: have the camera start at the player then slowly back away...that way if there is geo inhibiting view it will stop at said geo (level mesh etc) this needs to be done on every update...how is this coded, ive been playing with it last night, and couldnt quite to get it to work. your help is greatly appreciated, Chris |
| ||
Well, what if you, after smoothly moving the camera, just record the cams positions, reset the collision for the camera, move the camera to the players position, set up the camera collision again, then move it back to it's orginal position. It should back against the nearest obstacle. (I am only refering to my code here) |
| ||
Ross is right, you may have to do this with both, the target pivot and the camera itself. |