/// <summary> /// Check current brick position. Check if it is valid (all joints fits). /// </summary> /// <returns><c>true</c>, if position is valid, <c>false</c> otherwise.</returns> bool CheckPosition(PossibleJoint possibleJoint) { var brickJoints = possibleJoint.projectedBrickJoint.joint.parentBrick.joints; //Check free joints with another neighbour bricks //get bounds AgaQBrick brick = possibleJoint.projectedBrickJoint.joint.parentBrick; var bounds = brick.GetBounds(); //extend it by joint size bounds.Expand(jointColisionDistance * 2); //collide with other bricks var otherBricks = Physics.OverlapBox(bounds.center, bounds.extents, brick.transform.rotation, 1); //check joints at those other bricks foreach (var otherBrick in otherBricks) { if (otherBrick.gameObject == possibleJoint.projectedBrickJoint.joint.parentBrick.gameObject) { continue; } var agaQBrick = otherBrick.GetComponent <AgaQBrick>(); if (agaQBrick != null && !CheckJoints(brickJoints, agaQBrick.joints)) { return(false); } } return(true); }
/// <summary> /// Try to position brick to fulfill given joint and check if this positions is valid. /// </summary> /// <returns><c>true</c>, if joint was checked, <c>false</c> otherwise.</returns> /// <param name="possibleJoint">Joint.</param> bool CheckJoint(PossibleJoint possibleJoint, bool snapToGrid) { //calculate position and rotation differences Vector3 positionDiff = possibleJoint.projectedOtherJoint.joint.transform.position - possibleJoint.projectedBrickJoint.joint.transform.position; Quaternion rotationDiff = Quaternion.Inverse(possibleJoint.projectedOtherJoint.joint.transform.rotation) * possibleJoint.projectedBrickJoint.joint.transform.rotation; //remember old position var oldPosition = brick.transform.position; var oldRotation = brick.transform.rotation; //set brick in position provided by joints brick.transform.position += positionDiff; brick.transform.rotation *= Quaternion.Inverse(rotationDiff); bool isValid = (!snapToGrid || brick.transform.position.y + lowestYoffset >= 0) && CheckPosition(possibleJoint); //if position is not valid, restore old postition if (!isValid) { brick.transform.position = oldPosition; brick.transform.rotation = oldRotation; } return(isValid); }