1
KX_Camera(KX_GameObject)
2
========================
6
base class --- :class:`KX_GameObject`
8
.. class:: KX_Camera(KX_GameObject)
14
See :data:`sphereInsideFrustum` and :data:`boxInsideFrustum`
18
See :data:`sphereInsideFrustum` and :data:`boxInsideFrustum`
22
See :data:`sphereInsideFrustum` and :data:`boxInsideFrustum`
26
The camera's lens value.
30
.. attribute:: ortho_scale
32
The camera's view scale when in orthographic mode.
38
The camera's near clip distance.
44
The camera's far clip distance.
48
.. attribute:: perspective
50
True if this camera has a perspective transform, False for an orthographic projection.
54
.. attribute:: frustum_culling
56
True if this camera is frustum culling.
60
.. attribute:: projection_matrix
62
This camera's 4x4 projection matrix.
66
This is the identity matrix prior to rendering the first frame (any Python done on frame 1).
68
:type: 4x4 Matrix [[float]]
70
.. attribute:: modelview_matrix
72
This camera's 4x4 model view matrix. (read-only).
74
:type: 4x4 Matrix [[float]]
78
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).
80
.. attribute:: camera_to_world
82
This camera's camera to world transform. (read-only).
84
:type: 4x4 Matrix [[float]]
88
This matrix is regenerated every frame from the camera's position and orientation.
90
.. attribute:: world_to_camera
92
This camera's world to camera transform. (read-only).
94
:type: 4x4 Matrix [[float]]
98
Regenerated every frame from the camera's position and orientation.
102
This is camera_to_world inverted.
104
.. attribute:: useViewport
106
True when the camera is used as a viewport, set True to enable a viewport for this camera.
110
.. method:: sphereInsideFrustum(centre, radius)
112
Tests the given sphere against the view frustum.
114
:arg centre: The centre of the sphere (in world coordinates.)
115
:type centre: list [x, y, z]
116
:arg radius: the radius of the sphere
118
:return: :data:`~bge.types.KX_Camera.INSIDE`, :data:`~bge.types.KX_Camera.OUTSIDE` or :data:`~bge.types.KX_Camera.INTERSECT`
123
When the camera is first initialized the result will be invalid because the projection matrix has not been set.
125
.. code-block:: python
127
from bge import logic
128
cont = logic.getCurrentController()
131
# A sphere of radius 4.0 located at [x, y, z] = [1.0, 1.0, 1.0]
132
if (cam.sphereInsideFrustum([1.0, 1.0, 1.0], 4) != cam.OUTSIDE):
133
# Sphere is inside frustum !
134
# Do something useful !
136
# Sphere is outside frustum
138
.. method:: boxInsideFrustum(box)
140
Tests the given box against the view frustum.
142
:arg box: Eight (8) corner points of the box (in world coordinates.)
143
:type box: list of lists
144
:return: :data:`~bge.types.KX_Camera.INSIDE`, :data:`~bge.types.KX_Camera.OUTSIDE` or :data:`~bge.types.KX_Camera.INTERSECT`
148
When the camera is first initialized the result will be invalid because the projection matrix has not been set.
150
.. code-block:: python
152
from bge import logic
153
cont = logic.getCurrentController()
158
box.append([-1.0, -1.0, -1.0])
159
box.append([-1.0, -1.0, 1.0])
160
box.append([-1.0, 1.0, -1.0])
161
box.append([-1.0, 1.0, 1.0])
162
box.append([ 1.0, -1.0, -1.0])
163
box.append([ 1.0, -1.0, 1.0])
164
box.append([ 1.0, 1.0, -1.0])
165
box.append([ 1.0, 1.0, 1.0])
167
if (cam.boxInsideFrustum(box) != cam.OUTSIDE):
168
# Box is inside/intersects frustum !
169
# Do something useful !
171
# Box is outside the frustum !
173
.. method:: pointInsideFrustum(point)
175
Tests the given point against the view frustum.
177
:arg point: The point to test (in world coordinates.)
178
:type point: 3D Vector
179
:return: True if the given point is inside this camera's viewing frustum.
184
When the camera is first initialized the result will be invalid because the projection matrix has not been set.
186
.. code-block:: python
188
from bge import logic
189
cont = logic.getCurrentController()
192
# Test point [0.0, 0.0, 0.0]
193
if (cam.pointInsideFrustum([0.0, 0.0, 0.0])):
194
# Point is inside frustum !
195
# Do something useful !
197
# Box is outside the frustum !
199
.. method:: getCameraToWorld()
201
Returns the camera-to-world transform.
203
:return: the camera-to-world transform matrix.
204
:rtype: matrix (4x4 list)
206
.. method:: getWorldToCamera()
208
Returns the world-to-camera transform.
210
This returns the inverse matrix of getCameraToWorld().
212
:return: the world-to-camera transform matrix.
213
:rtype: matrix (4x4 list)
215
.. method:: setOnTop()
217
Set this cameras viewport ontop of all other viewport.
219
.. method:: setViewport(left, bottom, right, top)
221
Sets the region of this viewport on the screen in pixels.
223
Use :data:`bge.render.getWindowHeight` and :data:`bge.render.getWindowWidth` to calculate values relative to the entire display.
225
:arg left: left pixel coordinate of this viewport
227
:arg bottom: bottom pixel coordinate of this viewport
228
:type bottom: integer
229
:arg right: right pixel coordinate of this viewport
231
:arg top: top pixel coordinate of this viewport
234
.. method:: getScreenPosition(object)
236
Gets the position of an object projected on screen space.
238
.. code-block:: python
240
# For an object in the middle of the screen, coord = [0.5, 0.5]
241
coord = camera.getScreenPosition(object)
243
:arg object: object name or list [x, y, z]
244
:type object: :class:`KX_GameObject` or 3D Vector
245
:return: the object's position in screen coordinates.
248
.. method:: getScreenVect(x, y)
250
Gets the vector from the camera position in the screen coordinate direction.
257
:return: The vector from screen coordinate.
259
.. code-block:: python
261
# Gets the vector of the camera front direction:
262
m_vect = camera.getScreenVect(0.5, 0.5)
264
.. method:: getScreenRay(x, y, dist=inf, property=None)
266
Look towards a screen coordinate (x, y) and find first object hit within dist that matches prop.
267
The ray is similar to KX_GameObject->rayCastTo.
273
:arg dist: max distance to look (can be negative => look behind); 0 or omitted => detect up to other
275
:arg property: property name that object must have; can be omitted => detect any object
276
:type property: string
277
:rtype: :class:`KX_GameObject`
278
:return: the first object hit or None if no object or object does not match prop
280
.. code-block:: python
282
# Gets an object with a property "wall" in front of the camera within a distance of 100:
283
target = camera.getScreenRay(0.5, 0.5, 100, "wall")