Lined up a placed room. Moves the placed room so the closest vert to basevert lines up exactly, and the edge from basevert to basevert+1 lines up with the corresponding edge on the placed room.
| 616 | // Lined up a placed room. Moves the placed room so the closest vert to basevert lines up exactly, |
| 617 | // and the edge from basevert to basevert+1 lines up with the corresponding edge on the placed room. |
| 618 | void SnapRoom(int basevert) { |
| 619 | vector basecenter, attcenter; |
| 620 | room *baseroomp, *attroomp; |
| 621 | face *bfp, *afp; |
| 622 | vector *bvp; |
| 623 | int baseface, attface; |
| 624 | vector baseedge, attedge; |
| 625 | |
| 626 | // Get values for the attach room |
| 627 | if (Placed_room != -1) { |
| 628 | attroomp = &Rooms[Placed_room]; |
| 629 | attface = Placed_room_face; |
| 630 | } else { |
| 631 | ASSERT(Placed_group != NULL); |
| 632 | attroomp = &Placed_group->rooms[Placed_group->attachroom]; |
| 633 | attface = Placed_group->attachface; |
| 634 | } |
| 635 | |
| 636 | // Set some vars |
| 637 | baseroomp = Placed_baseroomp; |
| 638 | baseface = Placed_baseface; |
| 639 | attcenter = Placed_room_origin; |
| 640 | basecenter = Placed_room_attachpoint; |
| 641 | bfp = &baseroomp->faces[baseface]; |
| 642 | afp = &attroomp->faces[attface]; |
| 643 | bvp = &baseroomp->verts[bfp->face_verts[basevert]]; |
| 644 | |
| 645 | // Find matching vert in attach face |
| 646 | float closest_dist = FLT_MAX; |
| 647 | int closest_v; |
| 648 | for (int v = 0; v < afp->num_verts; v++) { |
| 649 | vector rotvert; |
| 650 | float dist; |
| 651 | |
| 652 | rotvert = ((attroomp->verts[afp->face_verts[v]] - attcenter) * Placed_room_rotmat) + basecenter; |
| 653 | dist = vm_VectorDistance(bvp, &rotvert); |
| 654 | |
| 655 | if (dist < closest_dist) { |
| 656 | closest_dist = dist; |
| 657 | closest_v = v; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | // Compute edge vector in base room |
| 662 | vm_GetNormalizedDir(&baseedge, &baseroomp->verts[bfp->face_verts[(basevert + 1) % bfp->num_verts]], bvp); |
| 663 | |
| 664 | // Compute edge vector in attach room |
| 665 | vector v0 = ((attroomp->verts[afp->face_verts[closest_v]] - attcenter) * Placed_room_rotmat) + basecenter; |
| 666 | vector v1 = ((attroomp->verts[afp->face_verts[(closest_v + afp->num_verts - 1) % afp->num_verts]] - attcenter) * |
| 667 | Placed_room_rotmat) + |
| 668 | basecenter; |
| 669 | vm_GetNormalizedDir(&attedge, &v1, &v0); |
| 670 | |
| 671 | // Get angle between two edge vectors |
| 672 | double dot = baseedge * attedge; |
| 673 | if (dot > 1.0) |
| 674 | dot = 1.0; |
| 675 | double ac = acos(dot); |
no test coverage detected