public
Created

wat are they doing?

  • Download Gist
MathUtil.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
/********************************************************************
 
The Multiverse Platform is made available under the MIT License.
 
Copyright (c) 2012 The Multiverse Foundation
 
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:
 
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
 
*********************************************************************/
 
#define NO_CULL_BACKFACES
 
#region Using directives
 
using System;
using System.Text;
using System.Diagnostics;
 
using Axiom.MathLib;
 
#endregion
 
namespace Multiverse.MathLib
{
public class MathUtil
{
const float Epsilon = 0.000001f;
 
public MathUtil() {
}
 
public static Matrix4 GetTransform(ref Quaternion orientation, ref Vector3 position) {
Matrix4 rv = Matrix4.FromMatrix3(orientation.ToRotationMatrix());
rv.Translation = position;
return rv;
}
 
public static Matrix4 GetTransform(Quaternion orientation, Vector3 position) {
return GetTransform(ref orientation, ref position);
}
#if CPP
static vector<int> g_oDebugTriangleIndices;
static vector<const OBBTreeNode *> g_oDebugCollisionNodes;
 
static inline bool
equals(float a, float b, float epsilon = .001) {
return (fabs(a - b) < epsilon);
}
 
// Check the time of collision on an axis, replacing the min_time with a higher
// collision time if the collision (on this axis) is later.
// We pass in the minimum and maximum projections points for the two objects,
// as well as the projected velocity vector magnitude.
// I also added an epsilon that will allow objects to move away in an axis
// where they are close, but will prevent them from getting any closer.
// This version is used for checking collisions with triangles.
static inline bool
CheckTime(float *min_time, float *max_time,
float min1, float max1, float min2, float max2, float w,
float epsilon = UNITS_PER_METER / 100.0f) {
float mult;
if (w >= 0) { // moving right
if (max1 < min2) { // object 1 is to the left of object 2
if ((max1 + epsilon) + w < min2) // too far away
return false;
mult = (min2 - (max1 + epsilon)) / w;
if (mult > *min_time)
*min_time = mult;
if (min1 + w > max2) { // will move partway past 2
mult = (max2 - min1) / w;
if (mult < *max_time)
*max_time = mult;
}
} else if (max2 < min1) { // object 2 is to the left of object 1
return false;
} else { // objects start out overlapping
if (min1 + w > max2) { // will move partway past 2
mult = (max2 - min1) / w;
if (mult < *max_time)
*max_time = mult;
}
}
} else { // moving left
if (max1 < min2) { // object 1 is to the left of object 2
return false;
} else if (max2 < min1) { // object 2 is to the left of object 1
if ((max2 + epsilon) < min1 + w) // too far away
return false;
mult = ((max2 + epsilon) - min1) / w;
if (mult > *min_time)
*min_time = mult;
if (max1 + w < min2) { // we will move partway past 2
mult = (min2 - max1) / w;
if (mult < *max_time)
*max_time = mult;
}
} else { // objects start out overlapping
if (max1 + w < min2) { // we will move partway past 2
mult = (min2 - max1) / w;
if (mult < *max_time)
*max_time = mult;
}
}
}
return (*min_time < *max_time);
}
 
// Get the vectors that go from the object's center to the faces
static inline void
GetFaceVectors(Vector *vResults, const Vector3 &vOBBDims) {
vResults[0].x = vOBBDims.x / 2;
vResults[0].y = 0;
vResults[0].z = 0;
 
vResults[1].x = 0;
vResults[1].y = vOBBDims.y / 2;
vResults[1].z = 0;
vResults[2].x = 0;
vResults[2].y = 0;
vResults[2].z = vOBBDims.z / 2;
}
 
// Get the rotated version of the vectors that go from the object's
// center to the faces
static inline void
GetRotatedFaceVectors(Vector *vResults, const Vector &vOBBDims,
const Rotation &rRot) {
Vector vTmp[3];
GetFaceVectors(vTmp, vOBBDims);
Matrix mRot;
D3DXMatrixRotationQuaternion(&mRot, &rRot);
D3DXVec3TransformCoord(&vResults[0], &vTmp[0], &mRot);
D3DXVec3TransformCoord(&vResults[1], &vTmp[1], &mRot);
D3DXVec3TransformCoord(&vResults[2], &vTmp[2], &mRot);
}
 
// Computes the minimum point and the maximum point for the projection of
// the points of the triangle along vP
void MathUtil::ComputeBounds(float *min, float *max,
const Triangle &triangle,
const Vector &vP) {
float tmp = D3DXVec3Dot(&triangle.p[0], &vP);
*min = tmp;
*max = tmp;
tmp = D3DXVec3Dot(&triangle.p[1], &vP);
if (tmp < *min)
*min = tmp;
if (tmp > *max)
*max = tmp;
tmp = D3DXVec3Dot(&triangle.p[2], &vP);
if (tmp < *min)
*min = tmp;
if (tmp > *max)
*max = tmp;
}
 
// Computes the minimum point and the maximum point for the projection of
// the points of the obb centered at vC with the three vectors from the
// array vB defining the vectors to the faces.
void
MathUtil::ComputeBounds(float *min, float *max, const Vector *vB,
const Vector &vC, const Vector &vP) {
float p = D3DXVec3Dot(&vC, &vP);
float r =
fabs(D3DXVec3Dot(&vB[0], &vP)) +
fabs(D3DXVec3Dot(&vB[1], &vP)) +
fabs(D3DXVec3Dot(&vB[2], &vP));
*min = p - r;
*max = p + r;
}
 
static void
ComputePlane(Plane &p, const Matrix &m, int index, float x, float y, float z) {
Vector temp;
p.a = x * m(0, 0) + y * m(0, 1) + z * m(0, 2);
p.b = x * m(1, 0) + y * m(1, 1) + z * m(1, 2);
p.c = x * m(2, 0) + y * m(2, 1) + z * m(2, 2);
p.d = p.a * m(3, 0) + p.b * m(3, 1) + p.c * m(3, 2);
}
 
static bool
IsBoxInside(const Plane &p, const Vector &min, const Vector &max) {
Vector v;
v.x = ( p.a > 0 ) ? max.x : min.x;
v.y = ( p.b > 0 ) ? max.y : min.y;
v.z = ( p.c > 0 ) ? max.z : min.z;
return (p.a * v.x + p.b * v.y + p.c * v.z >= p.d);
}
 
void
MathUtil::GetFrustrumPlanes(Plane *a_Planes, const Matrix &mView) {
ComputePlane(a_Planes[0], mView, 0, -1, 0, 1);
ComputePlane(a_Planes[1], mView, 1, 1, 0, 1);
ComputePlane(a_Planes[2], mView, 2, 0, 1, 1);
ComputePlane(a_Planes[3], mView, 3, 0, -1, 1);
}
 
bool
MathUtil::IsInFrustrum(const Plane *a_Planes, const Vector &min, const Vector &max) {
return(IsBoxInside(a_Planes[0], min, max) &&
IsBoxInside(a_Planes[1], min, max) &&
IsBoxInside(a_Planes[2], min, max) &&
IsBoxInside(a_Planes[3], min, max));
}
void
MathUtil::GetRotationVectors(const Rotation &rRot,
Vector *vUp, Vector *vRight, Vector *vForward) {
Matrix mRot;
D3DXMatrixRotationQuaternion(&mRot, &rRot);
Vector vBasisUp(0, 1, 0);
Vector vBasisForward(0, 0, 1);
Vector vBasisRight(1, 0, 0);
D3DXVec3TransformCoord(vUp, &vBasisUp, &mRot);
D3DXVec3TransformCoord(vRight, &vBasisRight, &mRot);
D3DXVec3TransformCoord(vForward, &vBasisForward, &mRot);
}
 
//
// This version is slower, but clearer -- it also doesn't handle the time
// based collision checks
//
// bool
// MathUtil::CheckCollision(const Rotation &rRotA, const Rotation rRotB,
// const Vector &vOBBCenterA, const Vector &vOBBCenterB,
// const Vector &vOBBDimsA, const Vector &vOBBDimsB) {
// Vector vL[15]; // separating axis candidates
// Vector vT = vOBBCenterB - vOBBCenterA;
// float ra, rb; // radius of projection
// Vector vA[3], vB[3]; // Basis vectors of OBBs
// int i, j;
// GetRotationVectors(rRotA, &vA[0], &vA[1], &vA[2]);
// GetRotationVectors(rRotB, &vB[0], &vB[1], &vB[2]);
// for (i = 0; i < 3; i++)
// vL[i] = vA[i];
// for (i = 0; i < 3; i++)
// vL[3 + i] = vB[i];
// for (i = 0; i < 3; i++)
// for (j = 0; j < 3; j++)
// D3DXVec3Cross(&vL[6 + 3 * j + i], &vA[i], &vB[j]);
// for (i = 0; i < 15; i++) {
// ra =
// vOBBDimsA[0] * fabs(D3DXVec3Dot(&vA[0], &vL[i])) +
// vOBBDimsA[1] * fabs(D3DXVec3Dot(&vA[1], &vL[i])) +
// vOBBDimsA[2] * fabs(D3DXVec3Dot(&vA[2], &vL[i]));
// rb =
// vOBBDimsB[0] * fabs(D3DXVec3Dot(&vB[0], &vL[i])) +
// vOBBDimsB[1] * fabs(D3DXVec3Dot(&vB[1], &vL[i])) +
// vOBBDimsB[2] * fabs(D3DXVec3Dot(&vB[2], &vL[i]));
// if (fabs(D3DXVec3Dot(&vT, &vL[i]) > (ra + rb)/2))
// return false;
// }
// return true;
// }
 
/* OBB and vPosDeltaA are assumed to be in the frame of reference of
* the OBBTree. The triangles are all in the frame of reference of
* the OBBTree as well. */
void
MathUtil::getPotentialTriangles(vector<int> &triangle_indices,
const OBB &obb, const OBBTreeNode *pNode,
const Vector &vTranslate,
const Vector &vPosDeltaA) {
float portion = 1.0;
Vector vNormal;
if (!CheckCollision(obb, *pNode, vTranslate, vPosDeltaA, &portion, vNormal))
return;
 
// add the node to the list of nodes we collided with for debugging
g_oDebugCollisionNodes.push_back(pNode);
 
// we could collide with the outermost bounding box of this node
if (pNode->m_pChildNode[0] && pNode->m_pChildNode[1]) {
// cerr << "processing child node 0: " << pNode->m_pChildNode[0] << endl;
getPotentialTriangles(triangle_indices, obb, pNode->m_pChildNode[0],
vTranslate, vPosDeltaA);
// cerr << "processing child node 1: " << pNode->m_pChildNode[1] << endl;
getPotentialTriangles(triangle_indices, obb, pNode->m_pChildNode[1],
vTranslate, vPosDeltaA);
// cerr << "done processing child nodes" << endl;
} else if (!pNode->m_pChildNode[0] && !pNode->m_pChildNode[1]) {
// cerr << "NO CHILDREN HERE: " << pNode << ", "
// << pNode->m_oTriangleIndices.size() << endl;
// no children, but our obb was hit.. add our triangles
vector<int>::const_iterator iter;
for (iter = pNode->m_oTriangleIndices.begin();
iter != pNode->m_oTriangleIndices.end(); ++iter)
triangle_indices.push_back(*iter);
} else {
// The node should either have 2 child nodes, or none
assert(!"Encountered OBBTreeNode with one child node.");
}
}
 
/* OBB and vPosDeltaA are assumed to be in the absolute frame of
reference. */
bool
MathUtil::CheckCollision(const OBB &obb, const OBBTree *pOBBTree,
const Vector &vTranslate,
const Vector &vPosDeltaA,
float *portion, Vector &vNormal) {
// All the OBBs in pOBBTree are not adjusted with the object's
// transform matrix, so instead, apply the inverse transform to obb
// and to vPosDeltaA. When we are done, apply the normal transform
// to the vNormal (and renormalize) before we return it to put it
// back in absolute coordinates.
 
OBB obb_trans;
Rotation rRot;
D3DXQuaternionInverse(&rRot, &pOBBTree->m_rRot);
Vector vScale;
vScale.x = 1.0 / pOBBTree->m_vScale.x;
vScale.y = 1.0 / pOBBTree->m_vScale.y;
vScale.z = 1.0 / pOBBTree->m_vScale.z;
obb.make_trans(&obb_trans, rRot, vScale, pOBBTree->m_mInverseTransform);
 
Vector vOBBCenter = obb.m_vPos;
Vector vTranslatedCenter = obb.m_vPos + vTranslate;
Vector vTranslatedMovedCenter = obb.m_vPos + vTranslate + vPosDeltaA;
Vector vNewOBBCenter, vNewTranslatedCenter, vNewTranslatedMovedCenter;
D3DXVec3TransformCoord(&vNewOBBCenter, &vOBBCenter,
&pOBBTree->m_mInverseTransform);
D3DXVec3TransformCoord(&vNewTranslatedCenter, &vTranslatedCenter,
&pOBBTree->m_mInverseTransform);
D3DXVec3TransformCoord(&vNewTranslatedMovedCenter, &vTranslatedMovedCenter,
&pOBBTree->m_mInverseTransform);
Vector vNewTrans = vNewTranslatedCenter - vNewOBBCenter;
Vector vRelVel = vNewTranslatedMovedCenter - vNewTranslatedCenter;
vector<int> triangle_indices;
// get the list of potential triangles in the collision
// later we might want to make this more efficient
 
// For debugging, I want to buld a list of nodes that we collided with
g_oDebugCollisionNodes.erase(g_oDebugCollisionNodes.begin(),
g_oDebugCollisionNodes.end());
 
// use the obbtree to prune the list of triangles that we need to consider
// for collision detection.
getPotentialTriangles(triangle_indices, obb_trans, pOBBTree->m_pRootNode,
vNewTrans, vRelVel);
if (!g_Config.m_bPruneTriangles) // Consider all triangles
triangle_indices = pOBBTree->m_pRootNode->m_oTriangleIndices;
 
// for debugging
const_cast<OBBTree *>(pOBBTree)->m_oPotentialTriangles = triangle_indices;
 
#if CPP_DEBUG
// float pre_y = obb.m_vPos.y - obb.m_vDims.y / 2;
// float post_y = obb.m_vPos.y + vPosDeltaA.y - obb.m_vDims.y / 2;
// if (pre_y > -128 && post_y < -128) {
// if (triangles.size() == 0) {
// cerr << "missed node: ";
// } else {
// cerr << "hit node: ";
// }
// cerr << pOBBTree->m_pRootNode
// << ": " << (OBB)*(pOBBTree->m_pRootNode)
// << " with: " << obb_trans << "; vRelVel = " << vRelVel << endl
// << " or with: " << obb << "; vPosDeltaA = " << vPosDeltaA << endl
// << " and with vNewTrans = " << vNewTrans
// << " and mInverseTransform = " << pOBBTree->m_mInverseTransform
// << endl;
// }
#endif
 
if (triangle_indices.size() == 0)
return false;
 
assert(*portion == 1.0); // this should always be 1, but make sure
// For debugging, I will set up the list of triangles that we collided with.
g_oDebugTriangleIndices.erase(g_oDebugTriangleIndices.begin(),
g_oDebugTriangleIndices.end());
 
Vector vTmp;
bool rc = CheckCollision(obb_trans, vNewTrans, vRelVel, triangle_indices,
pOBBTree->m_oTriangles, portion, vTmp);
if (rc) {
Vector vZero(0, 0, 0);
D3DXVec3TransformCoord(&vZero, &vZero, &pOBBTree->m_mTransform);
D3DXVec3TransformCoord(&vTmp, &vTmp, &pOBBTree->m_mTransform);
vTmp = vTmp - vZero;
D3DXVec3Normalize(&vNormal, &vTmp);
if (*portion > 0)
debug_info << "vNormal = " << vNormal
<< "; portion = " << *portion << endl;
}
 
// Debugging of course.
const_cast<OBBTree *>(pOBBTree)->m_oMissedTriIndices =
g_oDebugTriangleIndices;
 
vector<int>::const_iterator iter;
for (iter = g_oDebugTriangleIndices.begin();
iter != g_oDebugTriangleIndices.end(); ++iter) {
vector<const OBBTreeNode *> nodes;
pOBBTree->m_pRootNode->getNodes(nodes, *iter);
// do something with all those nodes -- i should collide with *all* of them
vector<const OBBTreeNode *>::const_iterator iter2;
vector<const OBBTreeNode *> missed_nodes;
for (iter2 = nodes.begin(); iter2 != nodes.end(); ++iter2) {
bool found = false;
vector<const OBBTreeNode *>::const_iterator iter3;
for (iter3 = g_oDebugCollisionNodes.begin();
iter3 != g_oDebugCollisionNodes.end(); ++iter3)
if (*iter3 == *iter2) {
found = true;
break;
}
if (!found)
missed_nodes.push_back(*iter2);
}
 
const_cast<OBBTree *>(pOBBTree)->m_oMissedNodes = missed_nodes;
 
if (missed_nodes.size() != 0) {
cerr << "should have collided with the following nodes: " << endl;
for (iter2 = missed_nodes.begin();
iter2 != missed_nodes.end(); ++iter2)
cerr << *iter2 << " ";
cerr << endl;
}
}
return rc;
}
 
 
// Here, A is the mover, and if A's colliding portion in projected onto
// the resulting normal vector, A will slide off of B.
bool
MathUtil::CheckCollision(const OBB &obb1, const OBB &obb2,
const Vector &vTranslate,
const Vector &vPosDeltaA,
float *portion, Vector &vNormal) {
return CheckCollision(obb1.m_rRot, obb2.m_rRot,
obb1.m_vPos + vTranslate, obb2.m_vPos,
obb1.m_vDims, obb2.m_vDims,
vPosDeltaA, portion, vNormal);
}
 
static inline bool
debug_return(float debug[16][6], float min1, float max1,
float min2, float max2, float w, int index) {
debug_info << "failed to collide on index: " << index << endl;
for (int i = 0; i < index; ++i)
debug_info << "in check collision2; "
<< "(" << debug[i][0] << ", " << debug[i][1] << "), "
<< "(" << debug[i][2] << ", " << debug[i][3] << "), "
<< "(" << debug[i][4] << ", " << debug[i][5] << ")" << endl;
debug_info << "* (" << min1 << ", " << max1 << "), "
<< "* (" << min2 << ", " << max2 << ") " << w << endl;
return false;
}
 
// Here, A is the mover (an OBB), and if A's colliding portion is projected
// onto the resulting normal vector, A will slide off of the triangle.
bool
MathUtil::CheckCollision(const Rotation &rRot, const Vector &vOBBCenter,
const Vector &vOBBDims, const Vector &vPosDelta,
const Triangle &triangle,
float *portion, Vector &vNormal) {
static const float epsilon = UNITS_PER_METER / 100.0f;
// This Heuristic comes from David Eberly
const Vector &vW = vPosDelta;
float w; // projection of vW;
Vector vA[3]; // Basis vectors of OBB
Vector vB[3]; // Vectors along the edges of the triangle
Vector vN; // Vector normal to planar face of the triangle
float min1, max1, min2, max2;
int i, j;
 
// cerr << "vOBBCenter: " << vOBBCenter << endl;
// cerr << "vOBBDims: " << vOBBDims << endl;
// cerr << "vPosDelta: " << vPosDelta << endl;
// cerr << "Triangle: " << triangle.p[0] << ", "
// << triangle.p[1] << ", " << triangle.p[2] << endl;
GetRotationVectors(rRot, &vA[1], &vA[0], &vA[2]);
// the triangle's three edge vectors
vB[0] = triangle.p[1] - triangle.p[0];
vB[1] = triangle.p[2] - triangle.p[1];
vB[2] = triangle.p[0] - triangle.p[2];
D3DXVec3Normalize(&vB[0], &vB[0]);
D3DXVec3Normalize(&vB[1], &vB[1]);
D3DXVec3Normalize(&vB[2], &vB[2]);
// the triangle's face normal vector
// i should choose a pair of faces with a non-tiny cross product
D3DXVec3Cross(&vB[3], &vB[0], &vB[1]);
D3DXVec3Normalize(&vB[3], &vB[3]);
 
// Compute the vectors to the obb faces
Vector vOBBFaces[3];
GetRotatedFaceVectors(vOBBFaces, vOBBDims, rRot);
 
// This will hold the times that the object collides in each projection
float max_time = 1.0f;
float min_time = 0.0f;
float tmp_time;
int last_index = 0;
 
*portion = 1.0f;
// For A's basis vectors
for (i = 0; i < 3; i++) {
ComputeBounds(&min1, &max1, vOBBFaces, vOBBCenter, vA[i]);
ComputeBounds(&min2, &max2, triangle, vA[i]);
w = D3DXVec3Dot(&vA[i], &vW);
tmp_time = min_time;
if (!CheckTime(&min_time, &max_time, min1, max1, min2, max2, w, epsilon))
return false;
if (min_time > tmp_time)
last_index = i;
}
// For triangle's edge vectors (and face vector)
for (i = 0; i < 4; i++) {
ComputeBounds(&min1, &max1, vOBBFaces, vOBBCenter, vB[i]);
ComputeBounds(&min2, &max2, triangle, vB[i]);
w = D3DXVec3Dot(&vB[i], &vW);
tmp_time = min_time;
if (!CheckTime(&min_time, &max_time, min1, max1, min2, max2, w, epsilon))
return false;
if (min_time > tmp_time)
last_index = 3 + i;
}
Vector vCross;
// For the 9 cross products
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++) {
D3DXVec3Cross(&vCross, &vA[i], &vB[j]);
ComputeBounds(&min1, &max1, vOBBFaces, vOBBCenter, vCross);
ComputeBounds(&min2, &max2, triangle, vCross);
w = D3DXVec3Dot(&vCross, &vW);
tmp_time = min_time;
if (!CheckTime(&min_time, &max_time, min1, max1, min2, max2, w, epsilon))
return false;
if (min_time > tmp_time)
last_index = 7 + 3 * i + j;
}
// Ok, they will hit. Find out what axis (in last_index), and when.
// Find the last axis that we collide on. This is the time we really collide
Vector vTemp;
if (last_index < 3)
vTemp = vA[last_index];
else if (last_index < 7)
vTemp = vB[last_index - 3];
else
D3DXVec3Cross(&vTemp, &vA[(last_index - 7) / 3],
&vB[(last_index - 7) % 3]);
 
D3DXVec3Normalize(&vNormal, &vTemp);
if (D3DXVec3Dot(&vNormal, &vW) > 0)
vNormal *= -1;
*portion = min_time;
// debug_info << "collided on index: " << last_index
// << "; *portion = " << *portion << endl;
return true;
}
 
// Here, A is the mover (an OBB), and if A's colliding portion is projected
// onto the resulting normal vector, A will slide off of the triangle.
bool
MathUtil::CheckCollision(const Rotation &rRotA, const Rotation rRotB,
const Vector &vOBBCenterA, const Vector &vOBBCenterB,
const Vector &vOBBDimsA, const Vector &vOBBDimsB,
const Vector &vPosDeltaA,
float *portion, Vector &vNormal) {
static const float epsilon = UNITS_PER_METER / 100.0f;
// This Heuristic comes from David Eberly
const Vector &vW = vPosDeltaA;
float w; // projection of vW;
Vector vA[3]; // Basis vectors of OBB A
Vector vB[3]; // Basis vectors of OBB B
Vector vN; // Vector normal to obb collided with
float min1, max1, min2, max2;
int i, j;
 
GetRotationVectors(rRotA, &vA[1], &vA[0], &vA[2]);
GetRotationVectors(rRotB, &vB[1], &vB[0], &vB[2]);
 
// Compute the vectors to the obb faces
Vector vOBBFacesA[3], vOBBFacesB[3];
GetRotatedFaceVectors(vOBBFacesA, vOBBDimsA, rRotA);
GetRotatedFaceVectors(vOBBFacesB, vOBBDimsB, rRotB);
 
// This will hold the times that the object collides in each projection
float max_time = 1.0f;
float min_time = 0.0f;
float tmp_time;
int last_index = 0;
 
*portion = 1.0f;
// For A's basis vectors
for (i = 0; i < 3; i++) {
ComputeBounds(&min1, &max1, vOBBFacesA, vOBBCenterA, vA[i]);
ComputeBounds(&min2, &max2, vOBBFacesB, vOBBCenterB, vA[i]);
w = D3DXVec3Dot(&vA[i], &vW);
tmp_time = min_time;
if (!CheckTime(&min_time, &max_time, min1, max1, min2, max2, w, epsilon))
return false;
if (min_time > tmp_time)
last_index = i;
}
// For B's basis vectors
for (i = 0; i < 3; i++) {
ComputeBounds(&min1, &max1, vOBBFacesA, vOBBCenterA, vB[i]);
ComputeBounds(&min2, &max2, vOBBFacesB, vOBBCenterB, vB[i]);
w = D3DXVec3Dot(&vB[i], &vW);
tmp_time = min_time;
if (!CheckTime(&min_time, &max_time, min1, max1, min2, max2, w, epsilon))
return false;
if (min_time > tmp_time)
last_index = i;
}
Vector vCross;
// For the 9 cross products
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++) {
D3DXVec3Cross(&vCross, &vA[i], &vB[j]);
ComputeBounds(&min1, &max1, vOBBFacesA, vOBBCenterA, vCross);
ComputeBounds(&min2, &max2, vOBBFacesB, vOBBCenterB, vCross);
w = D3DXVec3Dot(&vCross, &vW);
tmp_time = min_time;
if (!CheckTime(&min_time, &max_time, min1, max1, min2, max2, w, epsilon))
return false;
if (min_time > tmp_time)
last_index = 7 + 3 * i + j;
}
// Ok, they will hit. Find out what axis (in last_index), and when.
// Find the last axis that we collide on. This is the time we really collide
Vector vTemp;
if (last_index < 3)
vTemp = vA[last_index];
else if (last_index < 6)
vTemp = vB[last_index - 3];
else
D3DXVec3Cross(&vTemp, &vA[(last_index - 6) / 3],
&vB[(last_index - 6) % 3]);
D3DXVec3Normalize(&vNormal, &vTemp);
if (D3DXVec3Dot(&vNormal, &vW) > 0)
vNormal *= -1;
*portion = min_time;
 
// debug_info << "collided on index: " << last_index
// << "; *portion = " << *portion << endl
// << rRotA << rRotB << vOBBCenterA
// << vOBBCenterB << vOBBDimsA << vOBBDimsB << vPosDeltaA << endl;
 
return true;
}
 
// variant that takes triangle indices and a complete list of triangles
bool
MathUtil::CheckCollision(const OBB &obb, const Vector &vTranslate,
const Vector &vPosDelta,
const std::vector<int> &triangle_indices,
const std::vector<Triangle> &triangles,
float *portion, Vector &vNormal) {
return CheckCollision(obb.m_rRot, obb.m_vPos + vTranslate, obb.m_vDims,
vPosDelta, triangle_indices, triangles,
portion, vNormal);
}
 
// variant that takes triangle indices and a complete list of triangles
bool
MathUtil::CheckCollision(const Rotation &rRot, const Vector &vOBBCenter,
const Vector &vOBBDims, const Vector &vPosDelta,
const std::vector<int> &triangle_indices,
const std::vector<Triangle> &triangles,
float *portion, Vector &vNormal) {
float saved = 1.0f;
Vector vTmp;
std::vector<int>::const_iterator iter;
for (iter = triangle_indices.begin();
iter != triangle_indices.end(); ++iter) {
if (CheckCollision(rRot, vOBBCenter, vOBBDims, vPosDelta,
triangles[*iter], portion, vTmp) && *portion < saved) {
// debug_info << "Collided with triangle #" << *iter << endl;
g_oDebugTriangleIndices.push_back(*iter);
vNormal = vTmp;
saved = *portion;
}
}
*portion = saved;
return *portion < 1.0f;
}
 
// variant that just takes a list of triangles
bool
MathUtil::CheckCollision(const OBB &obb, const Vector &vTranslate,
const Vector &vPosDelta,
const std::vector<Triangle> &triangles,
float *portion, Vector &vNormal) {
return CheckCollision(obb.m_rRot, obb.m_vPos + vTranslate, obb.m_vDims,
vPosDelta, triangles, portion, vNormal);
}
 
// variant that just takes a list of triangles
bool
MathUtil::CheckCollision(const Rotation &rRot, const Vector &vOBBCenter,
const Vector &vOBBDims, const Vector &vPosDelta,
const std::vector<Triangle> &triangles,
float *portion, Vector &vNormal) {
float saved = 1.0f;
Vector vTmp;
std::vector<Triangle>::const_iterator iter;
for (iter = triangles.begin(); iter != triangles.end(); ++iter) {
if (CheckCollision(rRot, vOBBCenter, vOBBDims, vPosDelta,
*iter, portion, vTmp) && *portion < saved) {
vNormal = vTmp;
saved = *portion;
}
}
*portion = saved;
return *portion < 1.0f;
}
 
const float MathUtil::EPSILON = 0.000001f;
bool
MathUtil::RayIntersectBox(const Vector &orig, const Vector &dir,
const Vector &min, const Vector &max) {
bool inside = true;
int i;
Vector maxt(-1, -1, -1);
// Find candidate planes.
for (i = 0; i < 3; ++i) {
if (orig[i] < min[i]) {
inside = false;
// Calculate distance to slab
if (dir[i])
maxt[i] = (min[i] - orig[i]) / dir[i];
} else if (orig[i] > max[i]) {
inside = false;
// Calculate distance to slab
if (dir[i])
maxt[i] = (max[i] - orig[i]) / dir[i];
}
}
if (inside)
return true; // origin is inside the box
int max_plane = 0;
if (maxt[1] > maxt[max_plane])
max_plane = 1;
if (maxt[2] > maxt[max_plane])
max_plane = 2;
if (maxt[max_plane] < 0)
return false;
 
for (i = 0; i < 3; ++i) {
float tmp = orig[i] + maxt[max_plane] * dir[i]; // go to slab it hits last
if (tmp < min[i] - EPSILON || tmp > max[i] + EPSILON)
return false; // not in one of the slabs
}
return true;
}
 
// Ironically this method is slower, but oh well
bool
MathUtil::RayIntersectTriangle(const Vector &orig, const Vector &dir,
const Vector &vert0, const Vector &vert1,
const Vector &vert2) {
float tmp[3];
return RayIntersectTriangle(orig, dir, vert0, vert1, vert2,
&tmp[0], &tmp[1], &tmp[2]);
}
#endif
public static Quaternion FromEulerAngles(float yaw, float pitch, float roll) {
double c1 = Math.Cos(yaw / 2);
double s1 = Math.Sin(yaw / 2);
double c2 = Math.Cos(pitch / 2);
double s2 = Math.Sin(pitch / 2);
double c3 = Math.Cos(roll / 2);
double s3 = Math.Sin(roll / 2);
return new Quaternion((float)(c1 * c2 * c3 - s1 * s2 * s3),
(float)(c1 * c2 * s3 + s1 * s2 * c3),
(float)(s1 * c2 * c3 + c1 * s2 * s3),
(float)(c1 * s2 * c3 - s1 * c2 * s3));
}
 
/// <summary>
/// Returns the yaw associated with the quaternion. Note that while the
/// range of the pitch and roll is complete, the range of the yaw is only
/// half of the circle.
/// </summary>
/// <param name="q"></param>
/// <returns>An angle, θ, measured in radians, such that -π/2≤θ≤π/2</returns>
public static float GetYaw(Quaternion q) {
return (float)Math.Asin(-2 * (q.x * q.z - q.w * q.y));
}
 
/// <summary>
/// Returns the yaw associated with the quaternion. This variant
/// allows the yaw any value in the circle. If the roll or pitch
/// are outside the range (-π/2, π/2), this may return a misleading
/// value.
/// </summary>
/// <param name="q"></param>
/// <returns>An angle, θ, measured in radians, such that -π≤θ≤π</returns>
public static float GetFullYaw(Quaternion q) {
Vector3 dir = Vector3.UnitZ;
Vector3 newDir = q * dir;
// map x => y and z => x
return (float)Math.Atan2(newDir.x, newDir.z);
}
 
/// <summary>
/// Get the roll associated with this quaternion.
/// </summary>
/// <param name="q"></param>
/// <returns>An angle, θ, measured in radians, such that -π≤θ≤π.</returns>
public static float GetRoll(Quaternion q) {
return (float)Math.Atan2(2 * (q.x * q.y + q.w * q.z),
q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z);
}
 
/// <summary>
/// Get the pitch associated with this quaternion.
/// </summary>
/// <param name="q"></param>
/// <returns>An angle, θ, measured in radians, such that -π≤θ≤π.</returns>
public static float GetPitch(Quaternion q) {
return (float)Math.Atan2(2 * (q.y * q.z + q.w * q.x),
q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z);
}
 
/// <summary>
/// Calculate whether (and where) a ray intersects a triangle
/// Are vertices clockwise ??
/// </summary>
/// <param name="orig">Origin point of the ray</param>
/// <param name="dir">Direction of the ray</param>
/// <param name="v0">Vertex 1 of the triangle</param>
/// <param name="v1">Vertex 1 of the triangle</param>
/// <param name="v2">Vertex 1 of the triangle</param>
/// <param name="t">iirc, this is the distance to the intersection</param>
/// <param name="u">u coordinate of the intersection on the triangle</param>
/// <param name="v">v coordinate of the intersection on the triangle</param>
/// <returns></returns>
public static bool RayIntersectTriangle(Vector3 orig, Vector3 dir,
Vector3 v0, Vector3 v1, Vector3 v2,
out float t, out float u, out float v)
{
t = 0.0f;
u = 0.0f;
v = 0.0f;
Vector3 tvec, pvec, qvec;
float det, inv_det;
// find vectors for two edges sharing vert0
Vector3 edge1 = v1 - v0;
Vector3 edge2 = v2 - v0;
// begin calculating determinant - also used to calculate U parameter
pvec = dir.Cross(edge2);
// if determinant is near zero, ray lies in plane of triangle
det = edge1.Dot(pvec);
 
if (det > Epsilon) {
// calculate distance from vert0 to ray origin
tvec = orig - v0;
// calculate U parameter and test bounds
u = tvec.Dot(pvec);
if (u < 0.0f || u > det)
return false;
// prepare to test V parameter
qvec = tvec.Cross(edge1);
// calculate V parameter and test bounds
v = dir.Dot(qvec);
if (v < 0.0f || u + v > det)
return false;
#if NO_CULL_BACKFACES
} else if (det < -Epsilon) {
// calculate distance from vert0 to ray origin
tvec = orig - v0;
// calculate U parameter and test bounds
u = tvec.Dot(pvec);
if (u > 0.0f || u < det)
return false;
// prepare to test V parameter
qvec = tvec.Cross(edge1);
// calculate V parameter and test bounds
v = dir.Dot(qvec);
if (v > 0.0f || u + v < det)
return false;
#endif
} else
return false;
 
inv_det = 1.0f / det;
 
// calculate t, scale parameters, ray intersects triangle
t = edge2.Dot(qvec) * inv_det;
u *= inv_det;
v *= inv_det;
 
return true;
}
 
#if CPP_FOO
// returns 0 (partial), -1 (none), or 1 (full -- box1 contains box2)
int
MathUtil::BoundingBoxIntersect(const Vector &vMin1, const Vector &vMax1,
const Vector &vMin2, const Vector &vMax2) {
if (vMin1.x > vMax2.x || vMin1.y > vMax2.y || vMin1.z > vMax2.z ||
vMax1.x < vMin2.x || vMax1.y < vMin2.y || vMax1.z < vMin2.z)
return -1;
else if (vMin1.x < vMin2.x && vMin1.y < vMin2.y && vMin1.z < vMin2.z &&
vMax1.x > vMax2.x && vMax1.y > vMax2.y && vMax1.z > vMax2.z)
return 1;
else
// Is this right?
return 0;
}
 
void
MathUtil::TransformTriangle(Triangle &result, const Matrix &m,
const Triangle &t) {
D3DXVec3TransformCoord(&result.p[0], &t.p[0], &m);
D3DXVec3TransformCoord(&result.p[1], &t.p[1], &m);
D3DXVec3TransformCoord(&result.p[2], &t.p[2], &m);
}
 
void
MathUtil::TransformTriangles(list<Triangle> &result, const Matrix &m,
const list<Triangle> &t_list) {
result.erase(result.begin(), result.end());
Triangle tmp;
list<Triangle>::const_iterator iter;
for (iter = t_list.begin(); iter != t_list.end(); ++iter) {
TransformTriangle(tmp, m, (*iter));
result.push_back(tmp);
}
}
 
bool
MathUtil::TriTriIntersect(const list<Triangle> &t_list1,
const list<Triangle> &t_list2) {
list<Triangle>::const_iterator iter1;
list<Triangle>::const_iterator iter2;
for (iter1 = t_list1.begin(); iter1 != t_list1.end(); ++iter1)
for (iter2 = t_list2.begin(); iter2 != t_list2.end(); ++iter2)
if (TriTriIntersect(*iter1, *iter2))
return true;
return false;
}
 
 
bool
MathUtil::TriTriIntersect(const Triangle &t1, const Triangle &t2) {
return TriTriIntersect(t1.p[0], t1.p[1], t1.p[2], t2.p[0], t2.p[1], t2.p[2]);
}
#endif
 
/// <summary>
/// This algorithm was taken from Tomas Moller
/// Do the triangles specified by (v0, v1, v2) and (u0, u1, u2) intersect?
/// </summary>
/// <param name="v0">Vertex 1 from triangle 1</param>
/// <param name="v1">Vertex 2 from triangle 1</param>
/// <param name="v2">Vertex 3 from triangle 1</param>
/// <param name="u0">Vertex 1 from triangle 2</param>
/// <param name="u1">Vertex 2 from triangle 2</param>
/// <param name="u2">Vertex 3 from triangle 2</param>
/// <returns>true if the triangles instersect</returns>
static bool TriTriIntersect(Vector3 v0, Vector3 v1, Vector3 v2,
Vector3 u0, Vector3 u1, Vector3 u2) {
Vector3 e1, e2, n1, n2, d, du, dv, vp, up;
float d1, d2;
float[] isect1 = new float[2];
float[] isect2 = new float[2];
float du0du1, du0du2, dv0dv1, dv0dv2;
short index;
float b, c, max;
 
// compute plane equation of triangle(v0, v1, v2)
e1 = v1 - v0;
e2 = v2 - v0;
n1 = e1.Cross(e2);
d1 = -n1.Dot(v0);
// plane equation 1: n1 + d1 = 0
 
// put v0, v1, v2 into plane equation 1 to compute distances to the plane
du.x = n1.Dot(u0) + d1;
du.y = n1.Dot(u1) + d1;
du.z = n1.Dot(u2) + d1;
 
// coplanarity robustness check
if (du.x < Epsilon && du.x > -Epsilon)
du.x = 0.0f;
if (du.y < Epsilon && du.y > -Epsilon)
du.y = 0.0f;
if (du.z < Epsilon && du.z > -Epsilon)
du.z = 0.0f;
 
du0du1 = du.x * du.y;
du0du2 = du.x * du.z;
 
if (du0du1 > 0.0f && du0du2 > 0.0f) // same sign on all of them + not equal 0
return false; // no intersection occurs
 
// compute plane of triangle (u0, u1, u2)
e1 = u1 - u0;
e2 = u2 - u0;
n2 = e1.Cross(e2);
d2 = -n2.Dot(u0);
// plane equation 2: n2 + d2 = 0
 
// put u0, u1, u2 into plane equation 1 to compute distances to the plane
dv.x = n2.Dot(v0) + d2;
dv.y = n2.Dot(v1) + d2;
dv.z = n2.Dot(v2) + d2;
 
// coplanarity robustness check
if (dv.x < Epsilon && dv.x > -Epsilon)
dv.x = 0.0f;
if (dv.y < Epsilon && dv.y > -Epsilon)
dv.y = 0.0f;
if (dv.z < Epsilon && dv.z > -Epsilon)
dv.z = 0.0f;
 
dv0dv1 = dv.x * dv.y;
dv0dv2 = dv.x * dv.z;
 
if (dv0dv1 > 0.0f && dv0dv2 > 0.0f) // same sign on all of them + not equal 0
return false; // no intersection occurs
 
// compute direction of intersection line
d = n1.Cross(n2);
 
// compute and index to the largest component of D
max = Math.Abs(d.x);
index = 0;
b = Math.Abs(d.y);
c = Math.Abs(d.z);
if (b > max) {
max = b;
index = 1;
}
if (c > max) {
max = c;
index = 2;
}
 
// this is the simplified projection onto L
vp.x = v0[index];
vp.y = v1[index];
vp.z = v2[index];
 
up.x = u0[index];
up.y = u1[index];
up.z = u2[index];
 
// compute interval for triangle 1
if (!ComputeIntersect(vp, dv, dv0dv1, dv0dv2, ref isect1[0], ref isect1[1])) {
Trace.TraceInformation("Coplanar triangles.. ");
return false; // I can pretend these don't intersect
}
 
// compute interval for triangle 2
if (!ComputeIntersect(up, du, du0du1, du0du2, ref isect2[0], ref isect2[1])) {
Trace.TraceInformation("Coplanar triangles.. ");
return false; // I can pretend these don't intersect
}
 
if (isect1[0] > isect1[1]) {
float tmp = isect1[0];
isect1[0] = isect1[1];
isect1[1] = tmp;
}
 
if (isect2[0] > isect2[1]) {
float tmp = isect2[0];
isect2[0] = isect2[1];
isect2[1] = tmp;
}
 
if (isect1[1] < isect2[0] || isect2[1] < isect1[0])
return false;
return true;
}
 
static bool ComputeIntersect(Vector3 v, Vector3 d, float d0d1, float d0d2,
ref float isect0, ref float isect1) {
if (d0d1 > 0.0f) { // d0d2 <= 0.0
isect0 = v.z + (v.x - v.z) * d.z / (d.z - d.x);
isect1 = v.z + (v.y - v.z) * d.z / (d.z - d.y);
} else if (d0d2 > 0.0f) { // d0d1 <= 0.0
isect0 = v.y + (v.x - v.y) * d.y / (d.y - d.x);
isect1 = v.y + (v.z - v.y) * d.y / (d.y - d.z);
} else if (d.y * d.z > 0.0f || d.x != 0.0f) { // d0d1 <= 0.0 or d.x != 0.0
isect0 = v.x + (v.y - v.x) * d.x / (d.x - d.y);
isect1 = v.x + (v.z - v.x) * d.x / (d.x - d.z);
} else if (d.y != 0.0f) {
isect0 = v.y + (v.x - v.y) * d.y / (d.y - d.x);
isect1 = v.y + (v.z - v.y) * d.y / (d.y - d.z);
} else if (d.z != 0.0f) {
isect0 = v.z + (v.x - v.z) * d.z / (d.z - d.x);
isect1 = v.z + (v.y - v.z) * d.z / (d.z - d.y);
} else { // triangles are coplanar
return false;
}
return true;
}
 
 
#if CPP_FOO
void
MathUtil::ComputeTransform(Matrix *pmTrans, const Rotation &rRot,
const Vector &vScale, const Vector &vPos) {
Matrix mScale;
D3DXMatrixScaling(&mScale, vScale.x, vScale.y, vScale.z);
Matrix mRot;
D3DXMatrixRotationQuaternion(&mRot, &rRot);
Matrix mPos;
D3DXMatrixTranslation(&mPos, vPos.x, vPos.y, vPos.z);
Matrix mTmp;
// Apply rotation, scale, and translation
// it seems like the directx docs are wrong here
D3DXMatrixMultiply(&mTmp, &mRot, &mScale);
D3DXMatrixMultiply(pmTrans, &mTmp, &mPos);
}
#endif
 
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.