Skip to content

Commit

Permalink
wheels collide #5
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwulf committed Apr 23, 2015
1 parent 480e0ea commit c040bd4
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 24 deletions.
2 changes: 2 additions & 0 deletions physics-2D-Verlet/MMBalloon.h
Expand Up @@ -25,6 +25,8 @@

-(CGFloat) distanceFromPoint:(CGPoint)point;

-(void) constrainCollisionsWith:(NSArray*)objs;

-(void) constrain;

-(MMBalloon*) cloneObject;
Expand Down
26 changes: 26 additions & 0 deletions physics-2D-Verlet/MMBalloon.m
Expand Up @@ -124,6 +124,32 @@ -(CGFloat) distanceFromPoint:(CGPoint)point{
return dst;
}

-(void) constrainCollisionsWith:(NSArray*)balloons{
// make sure the balloon isn't hitting other balloons
for(MMBalloon* otherB in balloons) {
if(otherB != self){
CGFloat dist = [otherB.center distanceFromPoint:self.center.asCGPoint];
CGFloat movement = (otherB.radius + self.radius) - dist;
if(movement > 0){
// collision!

// fix their offset to be outside their
// combined radius
CGPoint distToMove = [otherB.center differenceFrom:self.center];
distToMove.x = (dist != 0) ? distToMove.x / dist : dist;
distToMove.y = (dist != 0) ? distToMove.y / dist : dist;
distToMove.x *= movement;
distToMove.y *= movement;

self.center.x -= distToMove.x / 2;
self.center.y -= distToMove.y / 2;
otherB.center.x += distToMove.x / 2;
otherB.center.y += distToMove.y / 2;
}
}
}
}

-(void) constrain{
[stick constrain];
}
Expand Down
26 changes: 2 additions & 24 deletions physics-2D-Verlet/MMPhysicsView.m
Expand Up @@ -388,6 +388,7 @@ -(void) constrainWheels{
MMStick* stick = [sticks objectAtIndex:i];
if([stick isKindOfClass:[MMWheel class]]){
MMWheel* wheel = (MMWheel*) stick;
[wheel constrainCollisionsWith:sticks];
[wheel constrain];

// constrain the wheel
Expand Down Expand Up @@ -427,7 +428,6 @@ -(void) constrainWheels{
wheel.center.oldy = wheel.center.y + deltaVY;
}


if(moveX || moveY){
// update other 4 points
for(MMPoint* p in @[wheel.p0, wheel.p1, wheel.p2, wheel.p3]){
Expand Down Expand Up @@ -479,29 +479,7 @@ -(void) constrainBalloons{
}
}
[b constrain];
// make sure the balloon isn't hitting other balloons
for(MMBalloon* otherB in balloons) {
if(otherB != b){
CGFloat dist = [otherB.center distanceFromPoint:b.center.asCGPoint];
CGFloat movement = (otherB.radius + b.radius) - dist;
if(movement > 0){
// collision!

// fix their offset to be outside their
// combined radius
CGPoint distToMove = [otherB.center differenceFrom:b.center];
distToMove.x = (dist != 0) ? distToMove.x / dist : dist;
distToMove.y = (dist != 0) ? distToMove.y / dist : dist;
distToMove.x *= movement;
distToMove.y *= movement;

b.center.x -= distToMove.x / 2;
b.center.y -= distToMove.y / 2;
otherB.center.x += distToMove.x / 2;
otherB.center.y += distToMove.y / 2;
}
}
}
[b constrainCollisionsWith:balloons];
[b constrain];
}
}
Expand Down
4 changes: 4 additions & 0 deletions physics-2D-Verlet/MMWheel.h
Expand Up @@ -17,4 +17,8 @@

+(MMWheel*) wheelWithCenter:(MMPoint*)center andRadius:(CGFloat)radius;

-(void) constrainCollisionsWith:(NSArray*)objs;



@end
30 changes: 30 additions & 0 deletions physics-2D-Verlet/MMWheel.m
Expand Up @@ -98,6 +98,36 @@ -(void) tick{
[crossBar5 tick];
}

-(void) constrainCollisionsWith:(NSArray*)sticks{
// make sure the balloon isn't hitting other balloons
for(MMStick* stick in sticks) {
if(stick != self){
if([stick isKindOfClass:[MMWheel class]]){
MMWheel* otherW = (MMWheel*)stick;
CGFloat dist = [otherW.center distanceFromPoint:self.center.asCGPoint];
CGFloat movement = (otherW.radius + self.radius) - dist;
if(movement > 0){
// collision!

// fix their offset to be outside their
// combined radius
CGPoint distToMove = [otherW.center differenceFrom:self.center];
distToMove.x = (dist != 0) ? distToMove.x / dist : dist;
distToMove.y = (dist != 0) ? distToMove.y / dist : dist;
distToMove.x *= movement;
distToMove.y *= movement;

self.center.x -= distToMove.x / 2;
self.center.y -= distToMove.y / 2;
otherW.center.x += distToMove.x / 2;
otherW.center.y += distToMove.y / 2;
}
}
}
}
}


-(void) constrain{
[super constrain];
[spoke1 constrain];
Expand Down

0 comments on commit c040bd4

Please sign in to comment.