PDG engine v0.9.5
 All Classes Namespaces Functions Variables Groups Pages
Classes | Variables
Physics

Things used for handling physics simulation. More...

Collaboration diagram for Physics:

Classes

class  CpArbiter
 arbitrates collisions between sprites using Chipmunk Physics (Chipmunk Physics Only) More...
 
class  CpConstraint
 describes how two sprites are connected to one another (Chipmunk Physics Only) More...
 
class  CpSpace
 container for simulating objects (Chipmunk Physics Only) More...
 
class  ISpriteCollideHelper
 helper for deciding whether 2 sprites should collide or not More...
 
struct  pdg::SpriteBreakEvent
 a Sprite joint is breaking because it was overstressed (Chipmunk Physics Only) More...
 
struct  pdg::SpriteCollideEvent
 a Sprite collided with something (Optional) More...
 

Variables

const action_CollideSprite = 0
 
const action_CollideWall = 1
 eventType_SpriteAnimate action: a sprite has hit the edge of the sprite layer
 
const action_JointBreak = 13
 

Detailed Description

Things used for handling physics simulation.


Class Documentation

class pdg::ISpriteCollideHelper

helper for deciding whether 2 sprites should collide or not

Warning
API Stability: 1 - Experimental. This API was introduced recently and gotten little or no real use. It may change or be removed in future versions. It may not be completely implemented and may be missing important pieces of functionality. Please try it out and provide feedback.

Implement this interface to do handle special case collision logic between two sprites. For example, you might want bullet sprites to collide with enemy sprites but not with one another. If a collide helper is installed, then on initial contact between two sprites the collide helper for the moving sprite will be called, and you the helper can decide if the collision should happen or not.

For those coding in Javascript, there is an implementation of ISpriteCollideHelper that maps a function definition to the draw call. So to create a helper:

var myHelper = new pdg.ISpriteCollideHelper(function(sprite, withSprite) {
console.log("in my sprite collide helper for " + sprite );
if (withSprite.id == 100) {
return true; // only collide with the player sprite (id 100)
}
return false; // don't collide with anything else
});
mySprite.setCollisionHelper(myHelper);

If you need something more complex, you can also use classify to create a new Javascript class that derives from pdg.ISpriteCollideHelper, and it will call the allowCollision() method of your class. For example:

classify(pdg.ISpriteCollideHelper, 'MyCollideHelperClass', function() {
def('allowCollision', function(sprite, withSprite) {
console.log("MyCollideHelperClass.allowCollision(" + sprite + ", " + withSprite + ")" );
return true; // let these sprites collide
});
});
mySprite.setCollisionHelper( new MyDrawHelperClass() );

Your Custom Function:

As you can see from the examples above, your Sprite Collide Helper function (or allowCollision() method) will be called with two parameters, and return a boolean:

Parameters
spritethe Sprite that is moving
withSpritethe Sprite it came in contact with
Returns
true if the collision should happen; or false if the collision should be ignored.
Note
You should use other techniques wherever possible to eliminate unnecessary collision helper callback, since these callbacks are relatively expensive. You should turn off collisions for sprites that never collide with anything; put sets of sprites that collide only with each other in separate layers; and use collision groups to set up groups of sprites that collide with other things but not each other.
See Also
Sprite.setCollisionHelper()
Sprite.enableCollisions()
Sprite.disableCollisions()
Sprite.setCollideGroup()
SpriteLayer.enableCollisions()
SpriteLayer.disableCollisions()
SpriteLayer.enableCollisionsWithLayer()
SpriteLayer.disableCollisionsWithLayer()
struct pdg::SpriteBreakEvent

a Sprite joint is breaking because it was overstressed (Chipmunk Physics Only)

This event is generated by a Sprite whenever the forces acting on a joint with another Sprite are greater then the breaking force of the joint. To prevent the break from happening, return true from the handler that gets this event. If you return false then the joint will go ahead and break.

{
    emitter: {},            // the emitter that generated this event
    eventType: 21,          // the event type (eventType_SpriteBreak)
    action: 13,                 // what happened (action_JointBreak)
    actingSprite: {},       // the Sprite to which the forces were applied
    inLayer: {},            // the SpriteLayer that contains the Sprite
    targetSprite: {},       // the Sprite that was previously joined
    impulse: {              // the impulse applied 
      x: 29.35,
      y: 0.883
    },
    force: 384.0,           // the force of the collision
    breakForce: 100.0,      // the maximum force the joint could stand before breaking
    joint: {}               // the CpConstraint that defines the joint
}
Todo:
Need a lot more documentation on Joints
Note
Only joints that have a breaking force assigned will generate these events; without it joints between sprites are considered unbreakable.
See Also
eventType_SpriteBreak
Sprite.makeJointBreakable()
Sprite.makeJointUnbreakable()
Physics
struct pdg::SpriteCollideEvent

a Sprite collided with something (Optional)

This event is generated by a Sprite whenever it hits something. If it hits another Sprite action will be action_CollideSprite, if it hits a wall it will be action_CollideWall.

{
    emitter: {},            // the emitter that generated this event
    eventType: 20,          // the event type (eventType_SpriteCollide)
    action: 0,                  // what happened (action_CollideSprite or action_CollideWall)
    actingSprite: {},       // the moving Sprite
    inLayer: {},            // the SpriteLayer that contains the Sprite
    targetSprite: {},       // the Sprite that was collided with (if action was action_CollideSprite)
    normal: {               // the normal vector for the collision
      x: 1.0,
      y: 0.0
    },
    impulse: {              // the impulse imparted by the collision
      x: 29.35,
      y: 0.883
    },
    force: 384.0,           // the force of the collision
    kineticEnergy:883       // the total kinetic energy of the collision
}
Todo:

Need a lot more documentation on Collisions

Calculate kinetic energy of collision for Sprites not using Chipmunk Physics

Note
Collisions must be turned on for each Sprite using Sprite.enableCollisions() or Sprite.setCollisionRadius()
See Also
eventType_SpriteCollide
action_CollideSprite
action_CollideWall
Sprite.enableCollisions()
Sprite.setCollisionRadius()

Variable Documentation

action_CollideSprite = 0
action_CollideWall = 1

eventType_SpriteAnimate action: a sprite has hit the edge of the sprite layer

These events are only sent for sprites that have setWantsCollideWallEvents(true) and where the SpriteLayer has a size explicitly set.

See Also
Sprite.setWantsCollideWallEvents()
SpriteLayer.setSize()
eventType_SpriteCollide
action_ExitLayer
Todo:
the impulse, force and kinetic energy values for the event are not calculated
action_JointBreak = 13

User Comments