Class R.components.Collider
Extends
R.components.Base.
Creates a collider component which tests the collision model for
potential collisions. Each frame, the component will update a potential
collision list (PCL) using the game objects current position
obtained from R.objects.Object2D#getPosition. Each object which meets
certain criteria will be passed to an onCollide() method which
must be implemented by the game object. By design, an object cannot
collide with itself. However, this can be changed with the #setCollideSame
method.
onCollide: function(obj, time, dt, targetMask) { if (targetMask == SomeObject.COLLISION_MASK) { obj.explode(); return R.components.Collider.STOP; } return R.components.Collider.CONTINUE; }The game object must determine if the collision is valid for itself, and then return a value which indicates whether the component should contine to check for collisions, or if it should stop. If the onCollide() method is not implemented on the game object, no collision events will be passed to the game object. Additionally, a game object can implement onCollideEnd() to be notified when collisions have stopped. The time the collisions stopped and the time since the last frame was generated will be the only arguments.
Defined in: collider.js.
Constructor Attributes | Constructor Name and Description |
---|---|
R.components.Collider(name, collisionModel, priority)
A collider component is responsible for handling potential collisions by
updating the associated collision model and checking for possible collisions.
|
Field Attributes | Field Name and Description |
---|---|
When onCollide() is called on the host object, it should
return this value if a collision occurred and the host wishes to be notified
about other potential collisions.
|
|
When onCollide() is called on the host object, it should
return this value if there was no collision and the host
wishes to be notified about other potential collisions.
|
|
For box and circle collider components, this will perform a more complex
test which will result in a R.struct.CollisionData structure.
|
|
For box and circle collider components, this will perform a simple
intersection test.
|
|
When onCollide() is called on the host object, it should
return this if a collision occurred and no more collisions should be reported.
|
- Fields borrowed from class R.components.Base:
- TYPE_COLLIDER, TYPE_INPUT, TYPE_LOGIC, TYPE_RENDERING, TYPE_TRANSFORM
Method Attributes | Method Name and Description |
---|---|
destroy()
Destroy the component instance.
|
|
execute(renderContext, time, dt)
Updates the object within the collision model and determines if
the game object should to be alerted whenever a potential collision
has occurred.
|
|
Get the class name of this object
|
|
Returns true if an object can collide with an object with the same mask.
|
|
Returns the collision data object, or
null . |
|
Returns a set of flags which can result in a collision between two objects.
|
|
Get the collision model being used by this component.
|
|
Get the linked physical body.
|
|
Get the object type that this collider component will respond to.
|
|
Get the collision node the host object is within, or null if it
is not within a node.
|
|
Determine the type of testing this component will perform.
|
|
linkPhysicalBody(physicalBody)
Link a rigid physical body to the collision component.
|
|
release()
Releases the component back into the pool for reuse.
|
|
setCollideSame(state)
Set whether or not an object can collide with an object with the same mask.
|
|
setCollisionData(cData)
Set the collision data object
|
|
setCollisionMask(collisionMask)
Collision masks allow objects to be considered colliding or not depending on ANDing
the results.
|
|
setCollisionModel(collisionModel)
Set the collision model which the host object participates in.
|
|
setGameObject(gameObject)
Establishes the link between this component and its game object.
|
|
setHostObject(hostObj)
Deprecated in favor of #setGameObject.
|
|
setObjectType(objType)
Set the object type that this component will respond to.
|
|
setTestMode(mode)
Set the type of testing to perform when determining collisions.
|
|
testCollision(time, dt, collisionObj, hostMask, targetMask)
Call the host object's onCollide() method, passing the time of the collision,
the delta since the last time the world was updated,
the potential collision object, and the game object and target's masks.
|
|
Update the collision model that this component was initialized with.
|
- Methods borrowed from class R.components.Base:
- getGameObject, getHostObject, getPriority, getType, getTypeString, setPriority
- Methods borrowed from class R.engine.BaseObject:
- addEvent, addEvents, getElement, jQ, removeEvent, setElement, triggerEvent, update
- Methods borrowed from class R.engine.PooledObject:
- clearObjectDataModel, getId, getName, getObjectDataModel, getProperties, isDestroyed, setName, setObjectDataModel, toString, toXML
Class Detail
R.components.Collider(name, collisionModel, priority)
A collider component is responsible for handling potential collisions by
updating the associated collision model and checking for possible collisions.
- Parameters:
- name
- {String} Name of the component
- collisionModel
- {SpatialCollection} The collision model
- priority
- {Number} Between 0.0 and 1.0, with 1.0 being highest
Field Detail
COLLIDE_AND_CONTINUE
When onCollide() is called on the host object, it should
return this value if a collision occurred and the host wishes to be notified
about other potential collisions.
CONTINUE
When onCollide() is called on the host object, it should
return this value if there was no collision and the host
wishes to be notified about other potential collisions.
DETAILED_TEST
For box and circle collider components, this will perform a more complex
test which will result in a R.struct.CollisionData structure.
SIMPLE_TEST
For box and circle collider components, this will perform a simple
intersection test.
STOP
When onCollide() is called on the host object, it should
return this if a collision occurred and no more collisions should be reported.
Method Detail
destroy()
Destroy the component instance.
execute(renderContext, time, dt)
Updates the object within the collision model and determines if
the game object should to be alerted whenever a potential collision
has occurred. If a potential collision occurs, an array (referred to
as a Potential Collision List, or PCL) will be created which
contains objects that might be colliding with the game object. It
is up to the game object to make the final determination that a
collision has occurred. If no collisions have occurred, that will be reported
as well.
Each object within the PCL will be tested and, if a collision occurs, is
passed to the onCollide() method (if declared) of the game object.
If a collision occurred and was handled, the onCollide() method should return
CollisionComponent#STOP, otherwise, it should return CollisionComponent#CONTINUE to continue
checking objects from the PCL against the game object.
- Parameters:
- renderContext
- {R.rendercontexts.AbstractRenderContext} The render context for the component
- time
- {Number} The current engine time in milliseconds
- dt
- {Number} The delta between the world time and the last time the world was updated in milliseconds.
{String}
getClassName()
Get the class name of this object
- Returns:
- {String} "R.components.Collider"
{Boolean}
getCollideSame()
Returns true if an object can collide with an object with the same mask.
Default: false
- Returns:
- {Boolean}
{R.struct.CollisionData}
getCollisionData()
Returns the collision data object, or
null
.
- Returns:
- {R.struct.CollisionData}
{Number}
getCollisionMask()
Returns a set of flags which can result in a collision between two objects.
The flags are bits within a 31-bit Integer that correspond to possible collisions.
- Returns:
- {Number}
{R.collision.broadphase.AbstractCollisionModel}
getCollisionModel()
Get the collision model being used by this component.
- Returns:
- {R.collision.broadphase.AbstractCollisionModel} The collision model
{R.components.physics.BaseBody}
getLinkedBody()
Get the linked physical body.
- Returns:
- {R.components.physics.BaseBody}
{BaseObject}
getObjectType()
Get the object type that this collider component will respond to. If
the value is null, all objects are potential collision objects.
- Deprecated:
- see #getCollisionFlags
- Returns:
- {BaseObject} The only object type to collide with, or null for any object
{R.spatial.AbstractSpatialNode}
getSpatialNode()
Get the collision node the host object is within, or null if it
is not within a node.
- Returns:
- {R.spatial.AbstractSpatialNode}
{Number}
getTestMode()
Determine the type of testing this component will perform. either #SIMPLE_TEST
or #DETAILED_TEST
- Returns:
- {Number}
linkPhysicalBody(physicalBody)
Link a rigid physical body to the collision component. When using a physical body to represent
a component, it is oftentimes useful to use the body shape as the collision shape.
- Parameters:
- physicalBody
- {R.components.physics.BaseBody}
release()
Releases the component back into the pool for reuse. See R.engine.PooledObject#release
for more information.
setCollideSame(state)
Set whether or not an object can collide with an object with the same mask. By default,
this is false. If you have objects which should collide, and they have the
same mask, this should be set to true.
- Parameters:
- state
- {Boolean} true if an object can collide with objects with the same mask
setCollisionData(cData)
Set the collision data object
- Parameters:
- cData
- {R.struct.CollisionData} The collision data, or
null
to clear it
setCollisionMask(collisionMask)
Collision masks allow objects to be considered colliding or not depending on ANDing
the results. The flags occupy the lowest 31 bits, so there can be a number of
combinations which result in a collision.
- Parameters:
- collisionMask
- {Number} A 31-bit integer
setCollisionModel(collisionModel)
Set the collision model which the host object participates in.
- Parameters:
- collisionModel
- {R.collision.broadphase.AbstractCollisionModel} The collision model, or null for none
setGameObject(gameObject)
Establishes the link between this component and its game object.
When you assign components to a game object, it will call this method
so that each component can refer to its game object, the same way
a game object can refer to a component with R.engine.GameObject#getComponent.
- Parameters:
- gameObject
- {R.engine.GameObject} The object which hosts this component
setHostObject(hostObj)
Deprecated in favor of #setGameObject.
- Parameters:
- hostObj
setObjectType(objType)
Set the object type that this component will respond to. Setting this to null
will trigger a potential collision when any object comes into possible contact
with the component's host based on the collision model. If the object isn't of this type,
no collision tests will be performed. This allows the developer to fine tune which
object the collision component is responsible for. As such, multiple collision components
could be used to handle different types of collisions.
- Parameters:
- objType
- {BaseObject} The object type to check for
- Deprecated:
- see #setCollisionFlags
setTestMode(mode)
Set the type of testing to perform when determining collisions. You can specify
either R.components.Collider#SIMPLE_TEST or R.components.Collider#DETAILED_TEST.
- Parameters:
- mode
- {Number} The testing mode to use
{Number}
testCollision(time, dt, collisionObj, hostMask, targetMask)
Call the host object's onCollide() method, passing the time of the collision,
the delta since the last time the world was updated,
the potential collision object, and the game object and target's masks. The return value should
indicate if the collision tests should continue or stop.
For R.components.Collider the collision test is up to the game object to determine.
- Parameters:
- time
- {Number} The engine time (in milliseconds) when the potential collision occurred
- dt
- {Number} The delta between the world time and the last time the world was updated in milliseconds.
- collisionObj
- {R.engine.GameObject} The game object with which the collision potentially occurs
- hostMask
- {Number} The collision mask for the host object
- targetMask
- {Number} The collision mask for collisionObj
- Returns:
- {Number} A status indicating whether to continue checking, or to stop
updateModel()
Update the collision model that this component was initialized with.
As objects move about the world, the objects will move to different
areas (or nodes) within the collision model. It is necessary to
update this model frequently so collisions can be determined.