CAMERA

 

CoolBasic handles objects in so-called 2D-space. It's like an empty world in which you can create game characters, maps and items. Each "object" can be freely moved and rotated inside the world. Now, there exists one camera in that world. The world is viewed from the aspect of the camera. By moving the camera you affect which part of the game world is shown on the srcreen. You must comprehend this camera-world-relationship in order to fully understand and benefit the concept of 2D-game worlds. Working with objects and camera will make game creation so much simplier and easier for you.

 

CloneCameraPosition

This command will position the camera exactly to same location as the given object. The ideal purpose is to make camera follow certain game object (the main character, for example) in one single call. If you have automatic collision enabled on the object that is to be followwd, you might want to call UpdateGame before this to prevent a little bite.

Usage:

CloneCameraPosition obj

 

CloneCameraOrientation

Aligns the camera to the same angle as the given object. The purpose of this command isn't that on-way-round than in CloneCameraPosition, but the explain should brighten that up.

Usage:

CloneCameraOrientation obj

Example:

FrameLimit 40

'Make an infinite lawn the guy can walk on

floor= MakeObjectFloor()

'Texture the EMPTY floor as grass

lawn= LoadImage("Media\grass.bmp")

PaintObject floor,lawn

guy= LoadObject("Media\guy.bmp",75)

AddText "Use arrows to move..."

AddText "Press RETURN to 'zoom'"

Repeat

    'Update controls

    If LeftKey() Then TurnObject guy,5

    If RightKey() Then TurnObject guy, -5

    If UpKey() Then MoveObject guy,2

    If DownKey() Then MoveObject guy, -2   

    'Glue camera to object position

    CloneCameraPosition guy   

    'The 'zooming' system

    If KeyDown(cbkeyreturn) Then

        zoom#=CurveValue(100,zoom#,20.0)

    Else

        zoom#=CurveValue(0,zoom#,20.0)

    EndIf       

    CloneCameraOrientation guy

    MoveCamera zoom#   

    DrawScreen

Until EscapeKey()

 

CameraFollow

This command sets the camera to follow the given object automatically. You won't need to take care of positioning the camera manually every time in the loop. In addition, you've got several options of following methods from which to choose. The available following modes are:

 

1 = Soft follow

This method is the same as if you were using the CurveValue –function to interpolate camera towards the object. The camera will smoothly follow the player. The greater the soph#-parameter the slower, but smoother is the follow.

 

2 = Autolimit

This method sets the max border inside screen from outside the object can't get without causing the screen to scroll with it. See the example #1.

 

3 = Field of View

This mode simulates the field of view from the aspect of the object i.e. you can see relatively far ahead, but not much behind or sides. The soph#-parameter effects the volume of FOV. Try to change the values to see how it affects. See the example #2.

 

Usage:

CameraFollow obj, soph#

Example #1:

FrameLimit 40 'don't run too fast

grass= MakeObjectFloor()

lawn= LoadImage("Media\grass.bmp")

PaintObject grass, lawn

'load the character

hero= LoadObject("Media\guy.bmp", 72)

'set the camera to follow it (follow style 2 is used)

CameraFollow hero,2,90

AddText "Try to guide the hero outside screen..."

Repeat

    'guide

    If LeftKey() Then TurnObject hero, 5

    If RightKey() Then TurnObject hero, -5    

    If UpKey() Then MoveObject hero, 2

    If DownKey() Then MoveObject hero, -2     

    DrawScreen

Forever

Example #2:

FrameLimit 40 'don't run too fast

grass= MakeObjectFloor()

lawn= LoadImage("Media\grass.bmp")

PaintObject grass, lawn

'load the character

hero= LoadObject("Media\guy.bmp", 72)

'set the camera to follow it (follow style 3 is used)

CameraFollow hero,3,100

AddText "Guide and turn"

Repeat

    'guide

    If LeftKey() Then TurnObject hero, 5

    If RightKey() Then TurnObject hero, -5

    If UpKey() Then MoveObject hero, 2

    If DownKey() Then MoveObject hero, -2

    DrawScreen

Forever

 

CameraPick

This command performs a pick according to camera position. You can actually do the pick and find which object is under certain SCREEN COORDINATES. Useful in menus or object picking by mouse. Use PickedObject to find out the picked object. Also, object that you wish to be able to be picked, should've been introduced pickable (ObjectPickable). See the ObjectPickable example for more details.

Usage:

CameraPick X, Y

 

PointCamera

This command makes the camera point an object. The ideal usage is to align first, then move towards. See the example.

Usage:

PointCamera obj

Example:

FrameLimit 40

'Make infinite grass

floor=MakeObjectFloor()

lawn=LoadImage("Media\grass.bmp")

PaintObject floor,lawn

target=LoadObject("Media\crosshair.bmp")AddText "Move the crosshair with arrows..."

Repeat

    'move the crosshair at speed 2

    If LeftKey() Then TranslateObject target, -3,0

    If RightKey() Then TranslateObject target,3,0

    If UpKey() Then TranslateObject target,0,3

    If DownKey() Then TranslateObject target,0, -3

    'Point camera to the crosshair and move it

    'towards

    PointCamera target

    MoveCamera 1

    'Visualize the camera angle

    DrawGame

    Circle 190,140,20,OFF

    Line 200,150,200+Cos(CameraAngle())*10,150-Sin(CameraAngle())*10

    DrawScreen

Until EscapeKey()

 

TurnCamera

Turns camera relative to its own angle. See TurnObject for more details. Notice that you won't see any visible results until you use MoveCamera.

Usage:

TurnCamera Angle#

 

RotateCamera

Turns camera relative to world axes i.e. set the absolute angle of the camera. See RotateObjectfor more details. Notice that you won't see any visible results until you use MoveCamera.

Usage:

RotateCamera Angle#

 

MoveCamera

Moves the camera according to its angle. See MoveObject for more details.

Usage:

MoveCamera forw# [, side#]

Example:

FrameLimit 40

floor=MakeObjectFloor()

texture=LoadImage("Media\grass.bmp")

PaintObject floor,texture

guy=LoadObject("Media\guy.bmp")

Repeat

    'Make the camera run circle

    TurnCamera 2

    MoveCamera 1

    DrawScreen

Until EscapeKey()

 

TranslateCamera

Moves the camera, but doesn't take its angle into account. See TranslateObject for more details.

Usage:

TranslateCamera horizontal# [, vertical#]

Example:

FrameLimit 40

'Make scene

floor=MakeObjectFloor()

texture=LoadImage("Media\grass.bmp")

PaintObject floor,texture

guy=LoadObject("Media\guy.bmp")

AddText "Press SPACE for default camera position"

Repeat

    'move camera as absolute

    TranslateCamera 1,1   

    'return to origin?

    If KeyDown(cbkeyspace) Then PositionCamera 0,0

    'Print the camera coordinates

    DrawGame

    Text 20,20,CameraX()

    Text 20,40,CameraY()

    DrawScreen

Until EscapeKey()

 

PositionCamera

Sets the camera's absolute position in the game world. Use this to immediately position the camera to wanted coordinates. See the example above.

Usage:

PositionCamera X#, Y#

 

CameraX, CameraY

These return the current camera position in game coordinates.

Usage:

X# = CameraX ()

Y# = CameraY ()

 

CameraAngle

This tells what's the current angle of the camera.

Usage:

Angle# = CameraAngle ()