KX_Camera(KX_GameObject)

base class — KX_GameObject

class KX_Camera(KX_GameObject)

A Camera object.

INSIDE

See sphereInsideFrustum and boxInsideFrustum

INTERSECT

See sphereInsideFrustum and boxInsideFrustum

OUTSIDE

See sphereInsideFrustum and boxInsideFrustum

lens

The camera’s lens value.

Type

float

lodDistanceFactor

The factor to multiply distance to camera to adjust levels of detail. A float < 1.0f will make the distance to camera used to compute levels of detail decrease.

Type

float

fov

The camera’s field of view value.

Type

float

ortho_scale

The camera’s view scale when in orthographic mode.

Type

float

near

The camera’s near clip distance.

Type

float

far

The camera’s far clip distance.

Type

float

shift_x

The camera’s horizontal shift.

Type

float

shift_y

The camera’s vertical shift.

Type

float

perspective

True if this camera has a perspective transform, False for an orthographic projection.

Type

boolean

frustum_culling

True if this camera is frustum culling.

Type

boolean

projection_matrix

This camera’s 4x4 projection matrix.

Note

This is the identity matrix prior to rendering the first frame (any Python done on frame 1).

Type

4x4 Matrix [[float]]

modelview_matrix

This camera’s 4x4 model view matrix. (read-only).

Type

4x4 Matrix [[float]]

Note

This matrix is regenerated every frame from the camera’s position and orientation. Also, this is the identity matrix prior to rendering the first frame (any Python done on frame 1).

camera_to_world

This camera’s camera to world transform. (read-only).

Type

4x4 Matrix [[float]]

Note

This matrix is regenerated every frame from the camera’s position and orientation.

world_to_camera

This camera’s world to camera transform. (read-only).

Type

4x4 Matrix [[float]]

Note

Regenerated every frame from the camera’s position and orientation.

Note

This is camera_to_world inverted.

useViewport

True when the camera is used as a viewport, set True to enable a viewport for this camera.

Type

boolean

sphereInsideFrustum(centre, radius)

Tests the given sphere against the view frustum.

Parameters
  • centre (list [x, y, z]) – The centre of the sphere (in world coordinates.)

  • radius (float) – the radius of the sphere

Returns

INSIDE, OUTSIDE or INTERSECT

Return type

integer

Note

When the camera is first initialized the result will be invalid because the projection matrix has not been set.

from bge import logic
cont = logic.getCurrentController()
cam = cont.owner

# A sphere of radius 4.0 located at [x, y, z] = [1.0, 1.0, 1.0]
if (cam.sphereInsideFrustum([1.0, 1.0, 1.0], 4) != cam.OUTSIDE):
    # Sphere is inside frustum !
    # Do something useful !
else:
    # Sphere is outside frustum
boxInsideFrustum(box)

Tests the given box against the view frustum.

Parameters

box (list of lists) – Eight (8) corner points of the box (in world coordinates.)

Returns

INSIDE, OUTSIDE or INTERSECT

Note

When the camera is first initialized the result will be invalid because the projection matrix has not been set.

from bge import logic
cont = logic.getCurrentController()
cam = cont.owner

# Box to test...
box = []
box.append([-1.0, -1.0, -1.0])
box.append([-1.0, -1.0,  1.0])
box.append([-1.0,  1.0, -1.0])
box.append([-1.0,  1.0,  1.0])
box.append([ 1.0, -1.0, -1.0])
box.append([ 1.0, -1.0,  1.0])
box.append([ 1.0,  1.0, -1.0])
box.append([ 1.0,  1.0,  1.0])

if (cam.boxInsideFrustum(box) != cam.OUTSIDE):
  # Box is inside/intersects frustum !
  # Do something useful !
else:
  # Box is outside the frustum !
pointInsideFrustum(point)

Tests the given point against the view frustum.

Parameters

point (3D Vector) – The point to test (in world coordinates.)

Returns

True if the given point is inside this camera’s viewing frustum.

Return type

boolean

Note

When the camera is first initialized the result will be invalid because the projection matrix has not been set.

from bge import logic
cont = logic.getCurrentController()
cam = cont.owner

# Test point [0.0, 0.0, 0.0]
if (cam.pointInsideFrustum([0.0, 0.0, 0.0])):
  # Point is inside frustum !
  # Do something useful !
else:
  # Box is outside the frustum !
getCameraToWorld()

Returns the camera-to-world transform.

Returns

the camera-to-world transform matrix.

Return type

matrix (4x4 list)

getWorldToCamera()

Returns the world-to-camera transform.

This returns the inverse matrix of getCameraToWorld().

Returns

the world-to-camera transform matrix.

Return type

matrix (4x4 list)

setOnTop()

Set this cameras viewport ontop of all other viewport.

setViewport(left, bottom, right, top)

Sets the region of this viewport on the screen in pixels.

Use bge.render.getWindowHeight and bge.render.getWindowWidth to calculate values relative to the entire display.

Parameters
  • left (integer) – left pixel coordinate of this viewport

  • bottom (integer) – bottom pixel coordinate of this viewport

  • right (integer) – right pixel coordinate of this viewport

  • top (integer) – top pixel coordinate of this viewport

getScreenPosition(object)

Gets the position of an object projected on screen space.

# For an object in the middle of the screen, coord = [0.5, 0.5]
coord = camera.getScreenPosition(object)
Parameters

object (KX_GameObject or 3D Vector) – object name or list [x, y, z]

Returns

the object’s position in screen coordinates.

Return type

list [x, y]

getScreenVect(x, y)

Gets the vector from the camera position in the screen coordinate direction.

Parameters
  • x (float) – X Axis

  • y (float) – Y Axis

Return type

3D Vector

Returns

The vector from screen coordinate.

# Gets the vector of the camera front direction:
m_vect = camera.getScreenVect(0.5, 0.5)
getScreenRay(x, y, dist=inf, property=None)

Look towards a screen coordinate (x, y) and find first object hit within dist that matches prop. The ray is similar to KX_GameObject->rayCastTo.

Parameters
  • x (float) – X Axis

  • y (float) – Y Axis

  • dist (float) – max distance to look (can be negative => look behind); 0 or omitted => detect up to other

  • property (string) – property name that object must have; can be omitted => detect any object

Return type

KX_GameObject

Returns

the first object hit or None if no object or object does not match prop

# Gets an object with a property "wall" in front of the camera within a distance of 100:
target = camera.getScreenRay(0.5, 0.5, 100, "wall")