Skip to content
This repository has been archived by the owner on Jan 14, 2021. It is now read-only.

Commit

Permalink
GoneBananas: Fixing up how CCPhysicsSprite updates transform
Browse files Browse the repository at this point in the history
* Also setting ball friction to 0 to fix weird collision behaviour
  • Loading branch information
rtabbara committed Sep 1, 2014
1 parent 6f2c5fa commit 26f8593
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
20 changes: 9 additions & 11 deletions GoneBananas/GoneBananasShared/CCPhysicsSprite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ public CCPhysicsSprite (CCTexture2D f, CCRect r, float ptmRatio) : base (f, r)

public b2Body PhysicsBody { get; set; }

public override CCAffineTransform AffineLocalTransform {
get {
if (PhysicsBody == null)
return base.AffineLocalTransform;

public void UpdateBallTransform()
{
if (PhysicsBody != null)
{
b2Vec2 pos = PhysicsBody.Position;

float x = pos.x * ptmRatio;
float y = pos.y * ptmRatio;

if (IgnoreAnchorPointForPosition) {
if (IgnoreAnchorPointForPosition)
{
x += AnchorPointInPoints.X;
y += AnchorPointInPoints.Y;
}
Expand All @@ -36,15 +36,13 @@ public CCPhysicsSprite (CCTexture2D f, CCRect r, float ptmRatio) : base (f, r)
var c = (float)Math.Cos (radians);
var s = (float)Math.Sin (radians);

if (!AnchorPointInPoints.Equals (CCPoint.Zero)) {
if (!AnchorPointInPoints.Equals (CCPoint.Zero))
{
x += c * -AnchorPointInPoints.X + -s * -AnchorPointInPoints.Y;
y += s * -AnchorPointInPoints.X + c * -AnchorPointInPoints.Y;
}

// Rot, Translate Matrix
var m_sTransform = new CCAffineTransform (c, s, -s, c, x, y);

return m_sTransform;
Position = new CCPoint(x, y);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions GoneBananas/GoneBananasShared/GameLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void StartScheduling()
sprite.Visible = false;
sprite.RemoveFromParent ();
} else {
sprite.UpdateTransformedSpriteTextureQuads ();
sprite.UpdateBallTransform();
}
}
});
Expand Down Expand Up @@ -341,17 +341,18 @@ void AddBall ()
var def = new b2BodyDef ();
def.position = new b2Vec2 (p.X / PTM_RATIO, p.Y / PTM_RATIO);
def.linearVelocity = new b2Vec2(0.0f, - 1.0f);
def.type = b2BodyType.b2_dynamicBody;
b2Body body = world.CreateBody (def);
var circle = new b2CircleShape ();
circle.Radius = 0.5f;
circle.Radius = 0.3f;
var fd = new b2FixtureDef ();
fd.shape = circle;
fd.density = 1f;
fd.restitution = 0.85f;
fd.friction = 0.3f;
fd.friction = 0f;
body.CreateFixture (fd);
sprite.PhysicsBody = body;
Expand Down

0 comments on commit 26f8593

Please sign in to comment.