private void handleBoundaryCollisions(BlockBS block) { if (block.Position.x - block.radiusX < _backgroundHitBox.x) { Vec2 barrier = new Vec2(_backgroundHitBox.x + block.radiusX, 0); Vec2 newPos = Vec2.PointOfImpact(block._position, block.Velocity, barrier); block._position.x = newPos.x; } else if (block.Position.x + block.radiusX > _backgroundHitBox.x + _backgroundHitBox.width) { Vec2 barrier = new Vec2(_backgroundHitBox.x + _backgroundHitBox.width - block.radiusX, 0); Vec2 newPos = Vec2.PointOfImpact(block._position, block.Velocity, barrier); block._position.x = newPos.x; } else if (block.Position.y - block.radiusY < _backgroundHitBox.y) { Vec2 barrier = new Vec2(0, _backgroundHitBox.y + block.radiusY); Vec2 newPos = Vec2.PointOfImpact(block._position, block.Velocity, barrier); block._position.y = newPos.y; } else if (block.Position.y + block.radiusY > _backgroundHitBox.y + _backgroundHitBox.height) { Vec2 barrier = new Vec2(0, _backgroundHitBox.y + _backgroundHitBox.height - block.radiusY); Vec2 newPos = Vec2.PointOfImpact(block._position, block.Velocity, barrier); block._position.y = newPos.y; } }
private void checkCollisions() { for (int i = 0; i < blocks.Count; i++) { if (blocks[i].Holding) { for (int j = 0; j < blocks.Count; j++) { BlockBS thisBlock = blocks[i]; BlockBS otherBlock = blocks[j]; if (thisBlock != otherBlock) { if (Mathf.Abs(otherBlock.Position.x - thisBlock.Position.x) <= thisBlock.radiusX + otherBlock.radiusX && Mathf.Abs(otherBlock.Position.y - thisBlock.Position.y) <= thisBlock.radiusY + otherBlock.radiusY) { handleCollisions(thisBlock, otherBlock); } } } handleBoundaryCollisions(blocks[i]); } foreach (Bones bone in bones) { BlockBS block = blocks[i]; if (Mathf.Abs(block.Position.x - bone.x) <= block.radiusX + bone.width / 2 && Mathf.Abs(block.Position.y - bone.y) <= block.radiusY + bone.height / 2) { bone._isBoneMoving = true; if (_infoBoard.alpha < 0.5f) { _infoBoard.alpha = 1.0f; } if (bone == bone1) { _infoBoard.SetFrame(0); } else if (bone == bone2) { _infoBoard.SetFrame(1); } else if (bone == bone3) { _infoBoard.SetFrame(2); } } } } }
private void load2() { BlockBS block1 = new BlockBS("stone3.png", new Vec2(531, 153), true); AddChild(block1); blocks.Add(block1); BlockBS block2 = new BlockBS("stone2V.png", new Vec2(434, 298), false); AddChild(block2); blocks.Add(block2); BlockBS block3 = new BlockBS("stone3V.png", new Vec2(628, 444), false); AddChild(block3); blocks.Add(block3); BlockBS block4 = new BlockBS("stone2.png", new Vec2(191, 444), true); AddChild(block4); blocks.Add(block4); BlockBS block5 = new BlockBS("stone2V.png", new Vec2(240, 590), false); AddChild(block5); blocks.Add(block5); BlockBS block6 = new BlockBS("stone4.png", new Vec2(482, 638), true); AddChild(block6); blocks.Add(block6); _goal = new BlockBS("bigBone.png", new Vec2(240, 347), true); AddChild(_goal); blocks.Add(_goal); bone1 = new Bones("collectBone.png", new Vec2(240, 153), targetPoints[0]); AddChild(bone1); bone1.SetOrigin(bone1.width / 2, bone1.height / 2); bones.Add(bone1); bone2 = new Bones("collectBone.png", new Vec2(531, 347), targetPoints[1]); AddChild(bone2); bone2.SetOrigin(bone2.width / 2, bone2.height / 2); bones.Add(bone2); bone3 = new Bones("collectBone.png", new Vec2(143, 638), targetPoints[2]); AddChild(bone3); bone3.SetOrigin(bone3.width / 2, bone3.height / 2); bones.Add(bone3); _infoBoard = new AnimSprite("INFOmediumBoneslider.png", 3, 1); AddChild(_infoBoard); _infoBoard.alpha = 0.0f; _infoBoard.SetXY(705, 90); }
private void handleCollisions(BlockBS thisBlock, BlockBS otherBlock) { if (thisBlock.Velocity.Length() == 0) { return; } CollisionInfo info = timeOfImpact(thisBlock._oldPosition, thisBlock.radiusX, thisBlock.radiusY, thisBlock.Velocity, otherBlock.Position, otherBlock.radiusX, otherBlock.radiusY); Vec2 point; if (!thisBlock.CanMoveHorizontal) { float delta1Y = (otherBlock.Position.y + otherBlock.radiusY + thisBlock.radiusY) - thisBlock.Position.y; float delta2Y = (otherBlock.Position.y - otherBlock.radiusY - thisBlock.radiusY) - thisBlock.Position.y; if (Mathf.Abs(delta1Y) < Mathf.Abs(delta2Y)) { point = new Vec2(0, otherBlock.Position.y + otherBlock.radiusY + thisBlock.radiusY + 1); } else { point = new Vec2(0, otherBlock.Position.y - otherBlock.radiusY - thisBlock.radiusY - 1); } Vec2 newPos = Vec2.PointOfImpact(thisBlock._oldPosition, thisBlock.Velocity, point); thisBlock._position.y = newPos.y; } else { float delta1X = (otherBlock.Position.x + otherBlock.radiusX + thisBlock.radiusX) - thisBlock.Position.x; float delta2X = (otherBlock.Position.x - otherBlock.radiusX - thisBlock.radiusX) - thisBlock.Position.x; if (Mathf.Abs(delta1X) < Mathf.Abs(delta2X)) { point = new Vec2(otherBlock.Position.x + otherBlock.radiusX + thisBlock.radiusX + 1, 0); } else { point = new Vec2(otherBlock.Position.x - otherBlock.radiusX - thisBlock.radiusX - 1, 0); } Vec2 newPos = Vec2.PointOfImpact(thisBlock._oldPosition, thisBlock.Velocity, point); thisBlock._position.x = newPos.x; } }