Camera Bounces

Blitz3D Forums/Blitz3D Beginners Area/Camera Bounces

Giuliani(Posted 2013) [#1]
Hi all,

I have bought a source code in BlitzBasic, everything is already coded and it works great but I would like to make some modifications.
Problem, I'm not really gifted in programing, could you help me please?

the camera in third person works, but it seems to follow the moves of head bone, because when the character runs, the camera "bounces" a lot, it's unbearable!
If I try to make a rotation with the camera around the camera it's even worst...

I copy the parts of the code about camera:

Global Cam, CamDist# = 10.0, CamPitch#, CamYaw#, CamMode, CamHeight# ; Camera
Const CameraViewRange# = 2000

[...]

; Main loop ----------------------------------------------------------------------------------------------------
; Render game
CameraProjMode(GY_Cam, 0)
RenderWorld()
CameraProjMode(GY_Cam, 1)
; Render GUI
CameraProjMode(Cam, 0)
For S.Scenery = Each Scenery
HideEntity(S\EN)
Next
For T.Terrain = Each Terrain
HideEntity(T\EN)
Next
RenderWorld()
CameraProjMode(Cam, 1)
For S.Scenery = Each Scenery
ShowEntity(S\EN)
Next
For T.Terrain = Each Terrain
ShowEntity(T\EN)
Next
PostRenderWorld() ; DebugBanner update

UpdateCamera() ; Camera movement

[...]

; Move
If dist# < CameraViewRange Then PointEntity(AI\CollisionEN, GPP) ; orig < 1000 ; Fix to remove the horizontally floating actor issue - found and submitted by atulos
MoveEntity(AI\CollisionEN, 0.0, 0.0, Speed#)

[...]

; Updates the camera
Function UpdateCamera()

Bonce = FindChild(Me\EN, "Head") : <== I have tried to change "Head" by a pivot that I have created but it doesn't work, I have a camera with Top view?
If Bonce = 0 Then RuntimeError(Me\Actor\Race$ + " actor mesh is missing a 'Head' joint!")

; Third person
If CamMode = 0
; Ugly hack to allow repetion of camera positioning if camera ends up too close to the player (see below)
Repeated = False
.CameraRepeat
; Find desired camera position in global units
CamX# = EntityX#(Cam)
CamY# = EntityY#(Cam)
CamZ# = EntityZ#(Cam)
PlayerX# = EntityX#(Me\CollisionEN)
PlayerY# = EntityY#(Me\CollisionEN) + CamHeight#
PlayerZ# = EntityZ#(Me\CollisionEN)
PositionEntity(Cam, PlayerX#, PlayerY#, PlayerZ#)
RotateEntity(Cam, CamPitch#, CamYaw# + EntityYaw#(Me\CollisionEN) + 180.0, 0.0)
MoveEntity(Cam, 0.0, 0.0, -CamDist#)
DesiredX# = EntityX#(Cam)
DesiredY# = EntityY#(Cam) ; + 1.5 by default
DesiredZ# = EntityZ#(Cam)
; Camera collision with scenery (ensure player is always visible)
SetPickModes()
Result = LinePick(PlayerX#, PlayerY#, PlayerZ#, DesiredX# - PlayerX#, DesiredY# - PlayerY#, DesiredZ# - PlayerZ#)
If Result <> 0
DesiredX# = PickedX#()
DesiredY# = PickedY#()
DesiredZ# = PickedZ#()
EndIf
; Smoothly move camera towards the desired position
ActualX# = CamX# + (((DesiredX# - CamX#) / 6.0) * Delta#)
ActualY# = CamY# + (((DesiredY# - CamY#) / 6.0) * Delta#)
ActualZ# = CamZ# + (((DesiredZ# - CamZ#) / 6.0) * Delta#)
PositionEntity(Cam, ActualX#, ActualY#, ActualZ#)
PositionEntity(GPP, PlayerX#, PlayerY#, PlayerZ#)
PointEntity(Cam, GPP)
; If the camera has collided, move it forwards so it doesn't clip the object
If Result <> 0 Then MoveEntity(Cam, 0.0, 0.0, 0.1)
; Flip camera 180 degrees if pushed too close to the player by a wall (prevents flickering)
PositionEntity(GPP, DesiredX#, DesiredY#, DesiredZ#)
If EntityDistance#(GPP, Bonce) < 2.0 And Repeated = False
CamYaw# = CamYaw# + 180.0
Repeated = True
Goto CameraRepeat
Return
EndIf
; First person
Else
PositionEntity(Cam, EntityX#(Bonce, True), EntityY#(Bonce, True), EntityZ#(Bonce, True))
ActualPitch# = CurveValue(EntityPitch#(Cam), -CamPitch#, 1.1)
RotateEntity(Cam, ActualPitch#, EntityYaw#(Me\CollisionEN) + 180.0, 0.0)
EndIf

; Underwater camera effects
WasUnderwater = CameraUnderwater
CameraUnderwater = False
For W.Water = Each Water
If EntityY#(Cam) < EntityY#(W\EN)
If Abs(EntityX#(Cam) - EntityX#(W\EN)) < (W\ScaleX# / 2.0)
If Abs(EntityZ#(Cam) - EntityZ#(W\EN)) < (W\ScaleZ# / 2.0)
CameraClsColor Cam, W\Red, W\Green, W\Blue
CameraFogColor Cam, W\Red, W\Green, W\Blue
FogNearNow# = 1.0 : FogFarNow# = 50.0
SetViewDistance(Cam, FogNearNow#, FogFarNow#)
HideEntity SkyEN
HideEntity StarsEN
HideEntity CloudEN
CameraUnderwater = True
Exit
EndIf
EndIf
EndIf
Next
If WasUnderwater = True
If CameraUnderwater = False
SetWeather(CurrentWeather)
ShowEntity SkyEN
ShowEntity StarsEN
ShowEntity CloudEN
EndIf
EndIf

End Function

[...]

Thanks in advance for your help


RemiD(Posted 2013) [#2]

could you help me please?


What do you want exactly ?

If you want the camera to follow the character but not move with the animations of the character, you have to create a pivot child of the character and position it at the level of the head, then you will be able to position rotate the camera depending on this pivot position and orientation.


Giuliani(Posted 2013) [#3]
Yes thank you for the answer.
Yes this is exactly what I try to do, I have created a pivot, attached to the bone head, but in game the camera is in the sky, view from the top. I don't inox exactly how to fix it at head level.


RemiD(Posted 2013) [#4]
No you don't want to set the pivot as a child to the bone head because if it is a child of the bone, it will move/rotate like it.
What you want is to set the pivot as a child to the root bone (if it has no animations) and position rotate the pivot with the same position/orientation like the eyes of the character, this way the pivot will not be animated.

Another way is to create a pivot for the "feet" of the character, then set the character mesh as a child of the "feet", then create another pivot for the "eyes" of the character, set the "eyes" as a child of the "feet", then set the camera as a child of the "eyes".

See this example :
http://blitzbasic.com/codearcs/codearcs.php?code=2933


Giuliani(Posted 2013) [#5]
Thanks a lot RemiD !

I will study that to adapt it to my code.