diff --git a/physics-2D-Verlet/MMBalloon.h b/physics-2D-Verlet/MMBalloon.h index faeafa7..75bf125 100644 --- a/physics-2D-Verlet/MMBalloon.h +++ b/physics-2D-Verlet/MMBalloon.h @@ -25,6 +25,8 @@ -(CGFloat) distanceFromPoint:(CGPoint)point; +-(void) constrainCollisionsWith:(NSArray*)objs; + -(void) constrain; -(MMBalloon*) cloneObject; diff --git a/physics-2D-Verlet/MMBalloon.m b/physics-2D-Verlet/MMBalloon.m index 6b4f6a7..a3b81c0 100644 --- a/physics-2D-Verlet/MMBalloon.m +++ b/physics-2D-Verlet/MMBalloon.m @@ -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]; } diff --git a/physics-2D-Verlet/MMPhysicsView.m b/physics-2D-Verlet/MMPhysicsView.m index 0f181c6..95bf44c 100644 --- a/physics-2D-Verlet/MMPhysicsView.m +++ b/physics-2D-Verlet/MMPhysicsView.m @@ -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 @@ -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]){ @@ -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]; } } diff --git a/physics-2D-Verlet/MMWheel.h b/physics-2D-Verlet/MMWheel.h index fa84839..e7fef23 100644 --- a/physics-2D-Verlet/MMWheel.h +++ b/physics-2D-Verlet/MMWheel.h @@ -17,4 +17,8 @@ +(MMWheel*) wheelWithCenter:(MMPoint*)center andRadius:(CGFloat)radius; +-(void) constrainCollisionsWith:(NSArray*)objs; + + + @end diff --git a/physics-2D-Verlet/MMWheel.m b/physics-2D-Verlet/MMWheel.m index 44e1517..f3e12a4 100644 --- a/physics-2D-Verlet/MMWheel.m +++ b/physics-2D-Verlet/MMWheel.m @@ -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];