Physics2D
Polycode::PhysicsScreen Class Reference

A 2D Physics enabled screen. More...

List of all members.

Public Member Functions

 PhysicsScreen (Number worldScale, Number freq)
 Creates a new physics screen.
 PhysicsScreen ()
 Default constructor.
PhysicsScreenEntityaddPhysicsChild (ScreenEntity *newEntity, int entType, bool isStatic, Number friction=0.1, Number density=1, Number restitution=0, bool isSensor=false, bool fixedRotation=false)
 Adds a ScreenEntity as a physics enabled child.
void removePhysicsChild (ScreenEntity *entityToRemove)
 Removes a physics child from the screen.
PhysicsScreenEntityaddCollisionChild (ScreenEntity *newEntity, int entType)
 Begins tracking collisions for a ScreenEntity.
void destroyJoint (PhysicsJoint *joint)
 Removes an existing joint.
PhysicsJoint * createDistanceJoint (ScreenEntity *ent1, ScreenEntity *ent2, bool collideConnected)
 Creates a new distance joint.
PhysicsJoint * createPrismaticJoint (ScreenEntity *ent1, ScreenEntity *ent2, Vector2 worldAxis, Number ax, Number ay, bool collideConnected=false, Number lowerTranslation=0, Number upperTranslation=0, bool enableLimit=false, Number motorSpeed=0, Number motorForce=0, bool motorEnabled=false)
 Creates a new prismatic joint.
PhysicsJoint * createRevoluteJoint (ScreenEntity *ent1, ScreenEntity *ent2, Number ax, Number ay, bool collideConnected=false, bool enableLimit=false, Number lowerLimit=0, Number upperLimit=0, bool motorEnabled=false, Number motorSpeed=0, Number maxTorque=0)
 Creates a new revolute joint.
void applyForce (ScreenEntity *ent, Number fx, Number fy)
 Applies linear force to an entity.
void applyImpulse (ScreenEntity *ent, Number fx, Number fy)
 Applies an impulse to an entity.
void setGravity (Vector2 newGravity)
 Sets the gravity for this screen.
void setTransform (ScreenEntity *ent, Vector2 pos, Number angle)
 Warps an entity to the specified location and angle.
void setVelocity (ScreenEntity *ent, Number fx, Number fy)
 Sets the linear velocity of an entity.
void setVelocityX (ScreenEntity *ent, Number fx)
 Sets the linear velocity of an entity on the X axis.
void setVelocityY (ScreenEntity *ent, Number fy)
 Sets the linear velocity of an entity on the Y axis.
void setSpin (ScreenEntity *ent, Number spin)
 Sets the spin of an entity.
Vector2 getVelocity (ScreenEntity *ent)
 Returns the velocity of an entity.
void wakeUp (ScreenEntity *ent)
 Wake up a sleeping entity.
ScreenEntity * getEntityAtPosition (Number x, Number y)
 Returns the entity at the specified position.
bool testEntityAtPosition (ScreenEntity *ent, Number x, Number y)
 Returns true if the specified entity is at the specified position.
bool testEntityCollision (ScreenEntity *ent1, ScreenEntity *ent2)
 Tests collision between two entities.
PhysicsScreenEntitygetPhysicsByScreenEntity (ScreenEntity *ent)
 Returns the physics entity for the specified screen entity.

Detailed Description

A 2D Physics enabled screen.

A PhysicsScreen acts like a normal screen, except that entities added to it with addPhysicsChild have physics automatically simulated. You can also use it to check collisions using addCollisionChild.


Member Function Documentation

PhysicsScreenEntity * PhysicsScreen::addCollisionChild ( ScreenEntity *  newEntity,
int  entType 
)

Begins tracking collisions for a ScreenEntity.

Parameters:
newEntityEntity to track collisions for.
entTypePhysics shape of the entity. Possible values are PhysicsScreenEntity::ENTITY_RECT or PhysicsScreenEntity::ENTITY_CIRCLE.
entityToRemoveEntity to remove from the screen.
PhysicsScreenEntity * PhysicsScreen::addPhysicsChild ( ScreenEntity *  newEntity,
int  entType,
bool  isStatic,
Number  friction = 0.1,
Number  density = 1,
Number  restitution = 0,
bool  isSensor = false,
bool  fixedRotation = false 
)

Adds a ScreenEntity as a physics enabled child.

Parameters:
newEntityScreen entity to add.
entTypePhysics entity type to add as. Possible values are PhysicsScreenEntity::ENTITY_RECT, PhysicsScreenEntity::ENTITY_CIRCLE and PhysicsScreenEntity::ENTITY_MESH. If the type is ENTITY_MESH, the ScreenEntity passed must be a ScreenMesh!
isStaticIf this parameter is true, the body is static (doesn't move on its own).
frictionFriction of the physics entity. Friction controls how entities drag along each other.
densityDensity of the physics entity. Density controls how heavy the entity is.
restitutionRestitution of the physics entity. Restitution controls how bouncy the entity is.
isSensorIf this is set to true, the entity won't collide with other entities, but its collision will register.
fixedRotationIf this is set to true, the entity will always have a locked rotation.
Returns:
The physics entity wrapper.
void PhysicsScreen::applyForce ( ScreenEntity *  ent,
Number  fx,
Number  fy 
)

Applies linear force to an entity.

Parameters:
entEntity to apply force to.
fxX value of the force direction vector.
fyY value of the force direction vector.
void PhysicsScreen::applyImpulse ( ScreenEntity *  ent,
Number  fx,
Number  fy 
)

Applies an impulse to an entity.

Parameters:
entEntity to apply force to.
fxX value of the impulse direction vector.
fyY value of the impulse direction vector.
PhysicsJoint * PhysicsScreen::createDistanceJoint ( ScreenEntity *  ent1,
ScreenEntity *  ent2,
bool  collideConnected 
)

Creates a new distance joint.

Distance joints keep the two entities at a fixed distance.

Parameters:
ent1First entity to join.
ent2Second entity to join.
collideConnectedIf set to true, both entities will collide with each other, if false, they will not.
Returns:
Created physics joint.
PhysicsJoint * PhysicsScreen::createPrismaticJoint ( ScreenEntity *  ent1,
ScreenEntity *  ent2,
Vector2  worldAxis,
Number  ax,
Number  ay,
bool  collideConnected = false,
Number  lowerTranslation = 0,
Number  upperTranslation = 0,
bool  enableLimit = false,
Number  motorSpeed = 0,
Number  motorForce = 0,
bool  motorEnabled = false 
)

Creates a new prismatic joint.

Prismatic joints provide one degree of freedom between two entities.

Parameters:
ent1First entity to join.
ent2Second entity to join.
collideConnectedIf set to true, both entities will collide with each other, if false, they will not.
axAnchor point x (relative to first entity)
ayAnchor point y (relative to first entity)
enableLimitIf true, the rotation will be limited to the specified values
lowerTranslationIf enableLimit is true, specifies the lower translation limit.
upperTranslationIf enableLimit is true, specifies the upper translation limit.
motorEnabledIf enabled, applies a constant motor to the rotation joint.
motorSpeedIf motorEnabled is true, controls the speed at which the motor rotates.
maxTorqueIf motorEnabled is true, specifies the maximum force applied.
worldAxisSpecifies the relative world axis for the prismatic joint.
Returns:
Created physics joint.
PhysicsJoint * PhysicsScreen::createRevoluteJoint ( ScreenEntity *  ent1,
ScreenEntity *  ent2,
Number  ax,
Number  ay,
bool  collideConnected = false,
bool  enableLimit = false,
Number  lowerLimit = 0,
Number  upperLimit = 0,
bool  motorEnabled = false,
Number  motorSpeed = 0,
Number  maxTorque = 0 
)

Creates a new revolute joint.

Revolute joints enable one entity to rotate around a point on another entity.

Parameters:
ent1Entity to anchor to.
ent2Entity to anchor.
axAnchor point x (relative to first entity)
ayAnchor point y (relative to first entity)
enableLimitIf true, the rotation will be limited to the specified values
lowerLimitIf enableLimit is true, specifies the lower limit of the rotation in degrees.
upperLimitIf enableLimit is true, specifies the upper limit of the rotation in degrees.
motorEnabledIf enabled, applies a constant torque to the rotation joint.
motorSpeedIf motorEnabled is true, controls the speed at which the motor rotates.
maxTorqueIf motorEnabled is true, specifies the maximum torque applied.
Returns:
Created physics joint.
void PhysicsScreen::destroyJoint ( PhysicsJoint *  joint)

Removes an existing joint.

Parameters:
jointJoint to remove.
ScreenEntity * PhysicsScreen::getEntityAtPosition ( Number  x,
Number  y 
)

Returns the entity at the specified position.

Parameters:
xX position.
yY position.
Returns:
If there is a collision-tracked entity at the specified position, it will be returned, NULL if there isn't.
PhysicsScreenEntity * PhysicsScreen::getPhysicsByScreenEntity ( ScreenEntity *  ent)

Returns the physics entity for the specified screen entity.

When you add ScreenEntities to the physics screen, these physics entities are created to track the physics status of the screen entities. You don't need to deal with these ever, but if you want, you can get them anyway.

Parameters:
entScreenEntity instance to return the physics entity for.
Returns:
PhysicsEntity associated with the ScreenEntity.
Vector2 PhysicsScreen::getVelocity ( ScreenEntity *  ent)

Returns the velocity of an entity.

Parameters:
entEntity to return velocity for
Returns:
Velocity of the specified entity.
void PhysicsScreen::removePhysicsChild ( ScreenEntity *  entityToRemove)

Removes a physics child from the screen.

Parameters:
entityToRemoveEntity to remove from the screen.
void PhysicsScreen::setGravity ( Vector2  newGravity)

Sets the gravity for this screen.

Parameters:
newGravityThe new gravity vector.
void PhysicsScreen::setSpin ( ScreenEntity *  ent,
Number  spin 
)

Sets the spin of an entity.

Parameters:
entEntity to apply spin to.
spinSpin value.
void PhysicsScreen::setTransform ( ScreenEntity *  ent,
Vector2  pos,
Number  angle 
)

Warps an entity to the specified location and angle.

Parameters:
entEntity to transform.
posNew position to set.
angleNew angle to set.
void PhysicsScreen::setVelocity ( ScreenEntity *  ent,
Number  fx,
Number  fy 
)

Sets the linear velocity of an entity.

Parameters:
entEntity to set velocity to.
fxX direction of velocity vector.
fyY direction of velocity vector.
void PhysicsScreen::setVelocityX ( ScreenEntity *  ent,
Number  fx 
)

Sets the linear velocity of an entity on the X axis.

Parameters:
entEntity to set velocity to.
fxX direction of velocity vector.
void PhysicsScreen::setVelocityY ( ScreenEntity *  ent,
Number  fy 
)

Sets the linear velocity of an entity on the Y axis.

Parameters:
entEntity to set velocity to.
fyY direction of velocity vector.
bool PhysicsScreen::testEntityAtPosition ( ScreenEntity *  ent,
Number  x,
Number  y 
)

Returns true if the specified entity is at the specified position.

Parameters:
entEntity to test.
xX position.
yY position.
Returns:
If there specified entity overlaps the specified position, this returns true.
void PhysicsScreen::wakeUp ( ScreenEntity *  ent)

Wake up a sleeping entity.

The physics engine puts non-moving entities to sleep automatically. Use this to wake them up.

Parameters:
entEntity to wake up.