Graphics Reference
In-Depth Information
//start the timer
self
.
lastStep
=
CACurrentMediaTime
();
self
.
timer
= [
CADisplayLink
displayLinkWithTarget
:
self
selector
:
@selector
(step:)];
[
self
.
timer
addToRunLoop
:[
NSRunLoop
mainRunLoop
]
forMode
:
NSDefaultRunLoopMode
];
}
void
updateShape(
cpShape
*shape,
void
*unused)
{
//get the crate object associated with the shape
Crate
*crate = (
__bridge
Crate
*)shape->
data
;
//update crate view position and angle to match physics shape
cpBody
*body = shape->
body
;
crate.
center
=
cpBodyGetPos
(body);
crate.
transform
=
CGAffineTransformMakeRotation
(
cpBodyGetAngle
(body));
}
- (
void
)step:(
CADisplayLink
*)timer
{
//calculate step duration
CFTimeInterval
thisStep =
CACurrentMediaTime
();
CFTimeInterval
stepDuration = thisStep -
self
.
lastStep
;
self
.
lastStep
= thisStep;
//update physics
cpSpaceStep
(
self
.
space
, stepDuration);
//update all the shapes
cpSpaceEachShape
(
self
.
space
, &
updateShape
,
NULL
);
}
@end