test_misc.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*************************************************************************/
  2. /* test_misc.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "test_misc.h"
  30. #include "servers/visual_server.h"
  31. #include "os/main_loop.h"
  32. #include "math_funcs.h"
  33. #include "print_string.h"
  34. namespace TestMisc {
  35. struct ConvexTestResult
  36. {
  37. Vector3 edgeA[2];
  38. Vector3 edgeB[2];
  39. bool valid;
  40. Vector3 contactA;
  41. Vector3 contactB;
  42. Vector3 contactNormal;
  43. float depth;
  44. /*
  45. Vector3 contactA;
  46. Vector3 contactB;
  47. Vector3 contactNormal;
  48. Vector3 contactX;
  49. Vector3 contactY;
  50. Vector3 edgeA[2];
  51. Vector3 edgeB[2];
  52. float depth;
  53. bool valid;
  54. bool isEdgeEdge;
  55. bool needTransform;
  56. neBool ComputerEdgeContactPoint(ConvexTestResult & res);
  57. neBool ComputerEdgeContactPoint2(float & au, float & bu);
  58. void Reverse()
  59. {
  60. neSwap(contactA, contactB);
  61. contactNormal *= -1.0f;
  62. }*/
  63. bool ComputerEdgeContactPoint2(float & au, float & bu);
  64. };
  65. bool ConvexTestResult::ComputerEdgeContactPoint2(float & au, float & bu)
  66. {
  67. float d1343, d4321, d1321, d4343, d2121;
  68. float numer, denom;
  69. Vector3 p13;
  70. Vector3 p43;
  71. Vector3 p21;
  72. Vector3 diff;
  73. p13 = (edgeA[0]) - (edgeB[0]);
  74. p43 = (edgeB[1]) - (edgeB[0]);
  75. if ( p43.length_squared() < CMP_EPSILON2 )
  76. {
  77. valid = false;
  78. goto ComputerEdgeContactPoint2_Exit;
  79. }
  80. p21 = (edgeA[1]) - (edgeA[0]);
  81. if ( p21.length_squared()<CMP_EPSILON2 )
  82. {
  83. valid = false;
  84. goto ComputerEdgeContactPoint2_Exit;
  85. }
  86. d1343 = p13.dot(p43);
  87. d4321 = p43.dot(p21);
  88. d1321 = p13.dot(p21);
  89. d4343 = p43.dot(p43);
  90. d2121 = p21.dot(p21);
  91. denom = d2121 * d4343 - d4321 * d4321;
  92. if (ABS(denom) < CMP_EPSILON)
  93. {
  94. valid = false;
  95. goto ComputerEdgeContactPoint2_Exit;
  96. }
  97. numer = d1343 * d4321 - d1321 * d4343;
  98. au = numer / denom;
  99. bu = (d1343 + d4321 * (au)) / d4343;
  100. if (au < 0.0f || au >= 1.0f)
  101. {
  102. valid = false;
  103. }
  104. else if (bu < 0.0f || bu >= 1.0f)
  105. {
  106. valid = false;
  107. }
  108. else
  109. {
  110. valid = true;
  111. }
  112. {
  113. Vector3 tmpv;
  114. tmpv = p21 * au;
  115. contactA = (edgeA[0]) + tmpv;
  116. tmpv = p43 * bu;
  117. contactB = (edgeB[0]) + tmpv;
  118. }
  119. diff = contactA - contactB;
  120. depth = Math::sqrt(diff.dot(diff));
  121. return true;
  122. ComputerEdgeContactPoint2_Exit:
  123. return false;
  124. }
  125. struct neCollisionResult {
  126. float depth;
  127. bool penetrate;
  128. Matrix3 collisionFrame;
  129. Vector3 contactA;
  130. Vector3 contactB;
  131. };
  132. struct TConvex {
  133. float radius;
  134. float half_height;
  135. float CylinderRadius() const { return radius; }
  136. float CylinderHalfHeight() const { return half_height; }
  137. };
  138. float GetDistanceFromLine2(Vector3 v, Vector3 & project, const Vector3 & pointA, const Vector3 & pointB)
  139. {
  140. Vector3 ba = pointB - pointA;
  141. float len = ba.length();
  142. if (len<CMP_EPSILON)
  143. ba=Vector3();
  144. else
  145. ba *= 1.0f / len;
  146. Vector3 pa = v - pointA;
  147. float k = pa.dot(ba);
  148. project = pointA + ba * k;
  149. Vector3 diff = v - project;
  150. return diff.length();
  151. }
  152. void TestCylinderVertEdge(neCollisionResult & result, Vector3 & edgeA1, Vector3 & edgeA2, Vector3 & vertB,
  153. TConvex & cA, TConvex & cB, Transform & transA, Transform & transB, bool flip)
  154. {
  155. Vector3 project;
  156. float dist = GetDistanceFromLine2(vertB,project, edgeA1, edgeA2);
  157. float depth = cA.CylinderRadius() + cB.CylinderRadius() - dist;
  158. if (depth <= 0.0f)
  159. return;
  160. if (depth <= result.depth)
  161. return;
  162. result.penetrate = true;
  163. result.depth = depth;
  164. if (!flip)
  165. {
  166. result.collisionFrame.set_axis(2,(project - vertB).normalized());
  167. result.contactA = project - result.collisionFrame.get_axis(2) * cA.CylinderRadius();
  168. result.contactB = vertB + result.collisionFrame.get_axis(2) * cB.CylinderRadius();
  169. }
  170. else
  171. {
  172. result.collisionFrame.set_axis(2,(vertB - project).normalized());
  173. result.contactA = vertB - result.collisionFrame.get_axis(2) * cB.CylinderRadius();
  174. result.contactB = project + result.collisionFrame.get_axis(2) * cA.CylinderRadius();
  175. }
  176. }
  177. void TestCylinderVertVert(neCollisionResult & result, Vector3 & vertA, Vector3 & vertB,
  178. TConvex & cA, TConvex & cB, Transform & transA, Transform & transB)
  179. {
  180. Vector3 diff = vertA - vertB;
  181. float dist = diff.length();
  182. float depth = cA.CylinderRadius() + cB.CylinderRadius() - dist;
  183. if (depth <= 0.0f)
  184. return;
  185. if (depth <= result.depth)
  186. return;
  187. result.penetrate = true;
  188. result.depth = depth;
  189. result.collisionFrame.set_axis(2, diff * (1.0f / dist));
  190. result.contactA = vertA - result.collisionFrame.get_axis(2) * cA.CylinderRadius();
  191. result.contactB = vertB + result.collisionFrame.get_axis(2) * cB.CylinderRadius();
  192. }
  193. void Cylinder2CylinderTest(neCollisionResult & result, TConvex & cA, Transform & transA, TConvex & cB, Transform & transB)
  194. {
  195. result.penetrate = false;
  196. Vector3 dir = transA.basis.get_axis(1).cross(transB.basis.get_axis(1));
  197. float len = dir.length();
  198. // bool isParallel = len<CMP_EPSILON;
  199. // int doVertCheck = 0;
  200. ConvexTestResult cr;
  201. cr.edgeA[0] = transA.origin + transA.basis.get_axis(1) * cA.CylinderHalfHeight();
  202. cr.edgeA[1] = transA.origin - transA.basis.get_axis(1) * cA.CylinderHalfHeight();
  203. cr.edgeB[0] = transB.origin + transB.basis.get_axis(1) * cB.CylinderHalfHeight();
  204. cr.edgeB[1] = transB.origin - transB.basis.get_axis(1) * cB.CylinderHalfHeight();
  205. // float dot = transA.basis.get_axis(1).dot(transB.basis.get_axis(1));
  206. if (len>CMP_EPSILON)
  207. {
  208. float au, bu;
  209. cr.ComputerEdgeContactPoint2(au, bu);
  210. if (cr.valid)
  211. {
  212. float depth = cA.CylinderRadius() + cB.CylinderRadius() - cr.depth;
  213. if (depth <= 0.0f)
  214. return;
  215. result.depth = depth;
  216. result.penetrate = true;
  217. result.collisionFrame.set_axis(2, (cr.contactA - cr.contactB)*(1.0f / cr.depth));
  218. result.contactA = cr.contactA - result.collisionFrame.get_axis(2) * cA.CylinderRadius();
  219. result.contactB = cr.contactB + result.collisionFrame.get_axis(2) * cB.CylinderRadius();
  220. return;
  221. }
  222. }
  223. result.depth = -1.0e6f;
  224. int i;
  225. for (i = 0; i < 2; i++)
  226. {
  227. //project onto edge b
  228. Vector3 diff = cr.edgeA[i] - cr.edgeB[1];
  229. float dot = diff.dot(transB.basis.get_axis(1));
  230. if (dot < 0.0f)
  231. {
  232. TestCylinderVertVert(result, cr.edgeA[i], cr.edgeB[1], cA, cB, transA, transB);
  233. }
  234. else if (dot > (2.0f * cB.CylinderHalfHeight()))
  235. {
  236. TestCylinderVertVert(result, cr.edgeA[i], cr.edgeB[0], cA, cB, transA, transB);
  237. }
  238. else
  239. {
  240. TestCylinderVertEdge(result, cr.edgeB[0], cr.edgeB[1], cr.edgeA[i], cB, cA, transB, transA, true);
  241. }
  242. }
  243. for (i = 0; i < 2; i++)
  244. {
  245. //project onto edge b
  246. Vector3 diff = cr.edgeB[i] - cr.edgeA[1];
  247. float dot = diff.dot(transA.basis.get_axis(1));
  248. if (dot < 0.0f)
  249. {
  250. TestCylinderVertVert(result, cr.edgeB[i], cr.edgeA[1], cA, cB, transA, transB);
  251. }
  252. else if (dot > (2.0f * cB.CylinderHalfHeight()))
  253. {
  254. TestCylinderVertVert(result, cr.edgeB[i], cr.edgeA[0], cA, cB, transA, transB);
  255. }
  256. else
  257. {
  258. TestCylinderVertEdge(result, cr.edgeA[0], cr.edgeA[1], cr.edgeB[i], cA, cB, transA, transB, false);
  259. }
  260. }
  261. }
  262. class TestMainLoop : public MainLoop {
  263. RID meshA;
  264. RID meshB;
  265. RID poly;
  266. RID instance;
  267. RID camera;
  268. RID viewport;
  269. RID boxA;
  270. RID boxB;
  271. RID scenario;
  272. Transform rot_a;
  273. Transform rot_b;
  274. bool quit;
  275. public:
  276. virtual void input_event(const InputEvent& p_event) {
  277. if (p_event.type==InputEvent::MOUSE_MOTION && p_event.mouse_motion.button_mask&BUTTON_MASK_LEFT) {
  278. rot_b.origin.y+=-p_event.mouse_motion.relative_y/100.0;
  279. rot_b.origin.x+=p_event.mouse_motion.relative_x/100.0;
  280. }
  281. if (p_event.type==InputEvent::MOUSE_MOTION && p_event.mouse_motion.button_mask&BUTTON_MASK_MIDDLE) {
  282. //rot_b.origin.x+=-p_event.mouse_motion.relative_y/100.0;
  283. rot_b.origin.z+=p_event.mouse_motion.relative_x/100.0;
  284. }
  285. if (p_event.type==InputEvent::MOUSE_MOTION && p_event.mouse_motion.button_mask&BUTTON_MASK_RIGHT) {
  286. float rot_x=-p_event.mouse_motion.relative_y/100.0;
  287. float rot_y=p_event.mouse_motion.relative_x/100.0;
  288. rot_b.basis = rot_b.basis * Matrix3(Vector3(1,0,0),rot_x) * Matrix3(Vector3(0,1,0),rot_y);
  289. }
  290. }
  291. virtual void request_quit() {
  292. quit=true;
  293. }
  294. virtual void init() {
  295. VisualServer *vs=VisualServer::get_singleton();
  296. camera = vs->camera_create();
  297. viewport = vs->viewport_create();
  298. vs->viewport_attach_to_screen(viewport);
  299. vs->viewport_attach_camera( viewport, camera );
  300. vs->camera_set_transform(camera, Transform( Matrix3(), Vector3(0,0,3 ) ) );
  301. /* CONVEX SHAPE */
  302. DVector<Plane> cylinder_planes = Geometry::build_cylinder_planes(0.5,2,9,Vector3::AXIS_Y);
  303. RID cylinder_material = vs->fixed_material_create();
  304. vs->fixed_material_set_param( cylinder_material, VisualServer::FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.8,0.2,0.9));
  305. vs->material_set_flag( cylinder_material, VisualServer::MATERIAL_FLAG_ONTOP,true);
  306. //vs->material_set_flag( cylinder_material, VisualServer::MATERIAL_FLAG_WIREFRAME,true);
  307. vs->material_set_flag( cylinder_material, VisualServer::MATERIAL_FLAG_DOUBLE_SIDED,true);
  308. vs->material_set_flag( cylinder_material, VisualServer::MATERIAL_FLAG_UNSHADED,true);
  309. RID cylinder_mesh = vs->mesh_create();
  310. Geometry::MeshData cylinder_data = Geometry::build_convex_mesh(cylinder_planes);
  311. vs->mesh_add_surface_from_mesh_data(cylinder_mesh,cylinder_data);
  312. vs->mesh_surface_set_material( cylinder_mesh, 0, cylinder_material );
  313. meshA=vs->instance_create2(cylinder_mesh,scenario);
  314. meshB=vs->instance_create2(cylinder_mesh,scenario);
  315. boxA=vs->instance_create2(vs->get_test_cube(),scenario);
  316. boxB=vs->instance_create2(vs->get_test_cube(),scenario);
  317. /*
  318. RID lightaux = vs->light_create( VisualServer::LIGHT_OMNI );
  319. vs->light_set_var( lightaux, VisualServer::LIGHT_VAR_RADIUS, 80 );
  320. vs->light_set_var( lightaux, VisualServer::LIGHT_VAR_ATTENUATION, 1 );
  321. vs->light_set_var( lightaux, VisualServer::LIGHT_VAR_ENERGY, 1.5 );
  322. light = vs->instance_create2( lightaux );
  323. */
  324. RID lightaux = vs->light_create( VisualServer::LIGHT_DIRECTIONAL );
  325. //vs->light_set_color( lightaux, VisualServer::LIGHT_COLOR_AMBIENT, Color(0.0,0.0,0.0) );
  326. //vs->light_set_shadow( lightaux, true );
  327. RID light = vs->instance_create2( lightaux,scenario );
  328. //rot_a=Transform(Matrix3(Vector3(1,0,0),Math_PI/2.0),Vector3());
  329. rot_b=Transform(Matrix3(),Vector3(2,0,0));
  330. //rot_x=0;
  331. //rot_y=0;
  332. quit=false;
  333. }
  334. virtual bool idle(float p_time) {
  335. VisualServer *vs=VisualServer::get_singleton();
  336. vs->instance_set_transform(meshA,rot_a);
  337. vs->instance_set_transform(meshB,rot_b);
  338. neCollisionResult res;
  339. TConvex a;
  340. a.radius=0.5;
  341. a.half_height=1;
  342. Cylinder2CylinderTest(res,a,rot_a,a,rot_b);
  343. if (res.penetrate) {
  344. Matrix3 scale;
  345. scale.scale(Vector3(0.1,0.1,0.1));
  346. vs->instance_set_transform(boxA,Transform(scale,res.contactA));
  347. vs->instance_set_transform(boxB,Transform(scale,res.contactB));
  348. print_line("depth: "+rtos(res.depth));
  349. } else {
  350. Matrix3 scale;
  351. scale.scale(Vector3());
  352. vs->instance_set_transform(boxA,Transform(scale,res.contactA));
  353. vs->instance_set_transform(boxB,Transform(scale,res.contactB));
  354. }
  355. print_line("collided: "+itos(res.penetrate));
  356. return false;
  357. }
  358. virtual bool iteration(float p_time) {
  359. return quit;
  360. }
  361. virtual void finish() {
  362. }
  363. };
  364. MainLoop* test() {
  365. return memnew( TestMainLoop );
  366. }
  367. }