shape_sw.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  1. /*************************************************************************/
  2. /* shape_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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 "shape_sw.h"
  30. #include "geometry.h"
  31. #include "sort.h"
  32. #include "quick_hull.h"
  33. #define _POINT_SNAP 0.001953125
  34. #define _EDGE_IS_VALID_SUPPORT_TRESHOLD 0.0002
  35. #define _FACE_IS_VALID_SUPPORT_TRESHOLD 0.9998
  36. void ShapeSW::configure(const AABB& p_aabb) {
  37. aabb=p_aabb;
  38. configured=true;
  39. for (Map<ShapeOwnerSW*,int>::Element *E=owners.front();E;E=E->next()) {
  40. ShapeOwnerSW* co=(ShapeOwnerSW*)E->key();
  41. co->_shape_changed();
  42. }
  43. }
  44. Vector3 ShapeSW::get_support(const Vector3& p_normal) const {
  45. Vector3 res;
  46. int amnt;
  47. get_supports(p_normal,1,&res,amnt);
  48. return res;
  49. }
  50. void ShapeSW::add_owner(ShapeOwnerSW *p_owner) {
  51. Map<ShapeOwnerSW*,int>::Element *E=owners.find(p_owner);
  52. if (E) {
  53. E->get()++;
  54. } else {
  55. owners[p_owner]=1;
  56. }
  57. }
  58. void ShapeSW::remove_owner(ShapeOwnerSW *p_owner){
  59. Map<ShapeOwnerSW*,int>::Element *E=owners.find(p_owner);
  60. ERR_FAIL_COND(!E);
  61. E->get()--;
  62. if (E->get()==0) {
  63. owners.erase(E);
  64. }
  65. }
  66. bool ShapeSW::is_owner(ShapeOwnerSW *p_owner) const{
  67. return owners.has(p_owner);
  68. }
  69. const Map<ShapeOwnerSW*,int>& ShapeSW::get_owners() const{
  70. return owners;
  71. }
  72. ShapeSW::ShapeSW() {
  73. custom_bias=0;
  74. configured=false;
  75. }
  76. ShapeSW::~ShapeSW() {
  77. ERR_FAIL_COND(owners.size());
  78. }
  79. Plane PlaneShapeSW::get_plane() const {
  80. return plane;
  81. }
  82. void PlaneShapeSW::project_range(const Vector3& p_normal, const Transform& p_transform, real_t &r_min, real_t &r_max) const {
  83. // gibberish, a plane is infinity
  84. r_min=-1e7;
  85. r_max=1e7;
  86. }
  87. Vector3 PlaneShapeSW::get_support(const Vector3& p_normal) const {
  88. return p_normal*1e15;
  89. }
  90. bool PlaneShapeSW::intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_result, Vector3 &r_normal) const {
  91. bool inters=plane.intersects_segment(p_begin,p_end,&r_result);
  92. if(inters)
  93. r_normal=plane.normal;
  94. return inters;
  95. }
  96. Vector3 PlaneShapeSW::get_moment_of_inertia(float p_mass) const {
  97. return Vector3(); //wtf
  98. }
  99. void PlaneShapeSW::_setup(const Plane& p_plane) {
  100. plane=p_plane;
  101. configure(AABB(Vector3(-1e4,-1e4,-1e4),Vector3(1e4*2,1e4*2,1e4*2)));
  102. }
  103. void PlaneShapeSW::set_data(const Variant& p_data) {
  104. _setup(p_data);
  105. }
  106. Variant PlaneShapeSW::get_data() const {
  107. return plane;
  108. }
  109. PlaneShapeSW::PlaneShapeSW() {
  110. }
  111. //
  112. float RayShapeSW::get_length() const {
  113. return length;
  114. }
  115. void RayShapeSW::project_range(const Vector3& p_normal, const Transform& p_transform, real_t &r_min, real_t &r_max) const {
  116. // don't think this will be even used
  117. r_min=0;
  118. r_max=1;
  119. }
  120. Vector3 RayShapeSW::get_support(const Vector3& p_normal) const {
  121. if (p_normal.z>0)
  122. return Vector3(0,0,length);
  123. else
  124. return Vector3(0,0,0);
  125. }
  126. void RayShapeSW::get_supports(const Vector3& p_normal,int p_max,Vector3 *r_supports,int & r_amount) const {
  127. if (Math::abs(p_normal.z) < _EDGE_IS_VALID_SUPPORT_TRESHOLD) {
  128. r_amount=2;
  129. r_supports[0]=Vector3(0,0,0);
  130. r_supports[1]=Vector3(0,0,length);
  131. } if (p_normal.z>0) {
  132. r_amount=1;
  133. *r_supports=Vector3(0,0,length);
  134. } else {
  135. r_amount=1;
  136. *r_supports=Vector3(0,0,0);
  137. }
  138. }
  139. bool RayShapeSW::intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_result, Vector3 &r_normal) const {
  140. return false; //simply not possible
  141. }
  142. Vector3 RayShapeSW::get_moment_of_inertia(float p_mass) const {
  143. return Vector3();
  144. }
  145. void RayShapeSW::_setup(float p_length) {
  146. length=p_length;
  147. configure(AABB(Vector3(0,0,0),Vector3(0.1,0.1,length)));
  148. }
  149. void RayShapeSW::set_data(const Variant& p_data) {
  150. _setup(p_data);
  151. }
  152. Variant RayShapeSW::get_data() const {
  153. return length;
  154. }
  155. RayShapeSW::RayShapeSW() {
  156. length=1;
  157. }
  158. /********** SPHERE *************/
  159. real_t SphereShapeSW::get_radius() const {
  160. return radius;
  161. }
  162. void SphereShapeSW::project_range(const Vector3& p_normal, const Transform& p_transform, real_t &r_min, real_t &r_max) const {
  163. float d = p_normal.dot( p_transform.origin );
  164. // figure out scale at point
  165. Vector3 local_normal = p_transform.basis.xform_inv(p_normal);
  166. float scale = local_normal.length();
  167. r_min = d - (radius) * scale;
  168. r_max = d + (radius) * scale;
  169. }
  170. Vector3 SphereShapeSW::get_support(const Vector3& p_normal) const {
  171. return p_normal*radius;
  172. }
  173. void SphereShapeSW::get_supports(const Vector3& p_normal,int p_max,Vector3 *r_supports,int & r_amount) const {
  174. *r_supports=p_normal*radius;
  175. r_amount=1;
  176. }
  177. bool SphereShapeSW::intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_result, Vector3 &r_normal) const {
  178. return Geometry::segment_intersects_sphere(p_begin,p_end,Vector3(),radius,&r_result,&r_normal);
  179. }
  180. Vector3 SphereShapeSW::get_moment_of_inertia(float p_mass) const {
  181. float s = 0.4 * p_mass * radius * radius;
  182. return Vector3(s,s,s);
  183. }
  184. void SphereShapeSW::_setup(real_t p_radius) {
  185. radius=p_radius;
  186. configure(AABB( Vector3(-radius,-radius,-radius), Vector3(radius*2.0,radius*2.0,radius*2.0)));
  187. }
  188. void SphereShapeSW::set_data(const Variant& p_data) {
  189. _setup(p_data);
  190. }
  191. Variant SphereShapeSW::get_data() const {
  192. return radius;
  193. }
  194. SphereShapeSW::SphereShapeSW() {
  195. radius=0;
  196. }
  197. /********** BOX *************/
  198. void BoxShapeSW::project_range(const Vector3& p_normal, const Transform& p_transform, real_t &r_min, real_t &r_max) const {
  199. // no matter the angle, the box is mirrored anyway
  200. Vector3 local_normal=p_transform.basis.xform_inv(p_normal);
  201. float length = local_normal.abs().dot(half_extents);
  202. float distance = p_normal.dot( p_transform.origin );
  203. r_min = distance - length;
  204. r_max = distance + length;
  205. }
  206. Vector3 BoxShapeSW::get_support(const Vector3& p_normal) const {
  207. Vector3 point(
  208. (p_normal.x<0) ? -half_extents.x : half_extents.x,
  209. (p_normal.y<0) ? -half_extents.y : half_extents.y,
  210. (p_normal.z<0) ? -half_extents.z : half_extents.z
  211. );
  212. return point;
  213. }
  214. void BoxShapeSW::get_supports(const Vector3& p_normal,int p_max,Vector3 *r_supports,int & r_amount) const {
  215. static const int next[3]={1,2,0};
  216. static const int next2[3]={2,0,1};
  217. for (int i=0;i<3;i++) {
  218. Vector3 axis;
  219. axis[i]=1.0;
  220. float dot = p_normal.dot( axis );
  221. if ( Math::abs( dot ) > _FACE_IS_VALID_SUPPORT_TRESHOLD ) {
  222. //Vector3 axis_b;
  223. bool neg = dot<0;
  224. r_amount = 4;
  225. Vector3 point;
  226. point[i]=half_extents[i];
  227. int i_n=next[i];
  228. int i_n2=next2[i];
  229. static const float sign[4][2]={
  230. {-1.0, 1.0},
  231. { 1.0, 1.0},
  232. { 1.0,-1.0},
  233. {-1.0,-1.0},
  234. };
  235. for (int j=0;j<4;j++) {
  236. point[i_n]=sign[j][0]*half_extents[i_n];
  237. point[i_n2]=sign[j][1]*half_extents[i_n2];
  238. r_supports[j]=neg?-point:point;
  239. }
  240. if (neg) {
  241. SWAP( r_supports[1], r_supports[2] );
  242. SWAP( r_supports[0], r_supports[3] );
  243. }
  244. return;
  245. }
  246. r_amount=0;
  247. }
  248. for (int i=0;i<3;i++) {
  249. Vector3 axis;
  250. axis[i]=1.0;
  251. if (Math::abs(p_normal.dot(axis))<_EDGE_IS_VALID_SUPPORT_TRESHOLD) {
  252. r_amount= 2;
  253. int i_n=next[i];
  254. int i_n2=next2[i];
  255. Vector3 point=half_extents;
  256. if (p_normal[i_n]<0) {
  257. point[i_n]=-point[i_n];
  258. }
  259. if (p_normal[i_n2]<0) {
  260. point[i_n2]=-point[i_n2];
  261. }
  262. r_supports[0] = point;
  263. point[i]=-point[i];
  264. r_supports[1] = point;
  265. return;
  266. }
  267. }
  268. /* USE POINT */
  269. Vector3 point(
  270. (p_normal.x<0) ? -half_extents.x : half_extents.x,
  271. (p_normal.y<0) ? -half_extents.y : half_extents.y,
  272. (p_normal.z<0) ? -half_extents.z : half_extents.z
  273. );
  274. r_amount=1;
  275. r_supports[0]=point;
  276. }
  277. bool BoxShapeSW::intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_result, Vector3 &r_normal) const {
  278. AABB aabb(-half_extents,half_extents*2.0);
  279. return aabb.intersects_segment(p_begin,p_end,&r_result,&r_normal);
  280. }
  281. Vector3 BoxShapeSW::get_moment_of_inertia(float p_mass) const {
  282. float lx=half_extents.x;
  283. float ly=half_extents.y;
  284. float lz=half_extents.z;
  285. return Vector3( (p_mass/3.0) * (ly*ly + lz*lz), (p_mass/3.0) * (lx*lx + lz*lz), (p_mass/3.0) * (lx*lx + ly*ly) );
  286. }
  287. void BoxShapeSW::_setup(const Vector3& p_half_extents) {
  288. half_extents=p_half_extents.abs();
  289. configure(AABB(-half_extents,half_extents*2));
  290. }
  291. void BoxShapeSW::set_data(const Variant& p_data) {
  292. _setup(p_data);
  293. }
  294. Variant BoxShapeSW::get_data() const {
  295. return half_extents;
  296. }
  297. BoxShapeSW::BoxShapeSW() {
  298. }
  299. /********** CAPSULE *************/
  300. void CapsuleShapeSW::project_range(const Vector3& p_normal, const Transform& p_transform, real_t &r_min, real_t &r_max) const {
  301. Vector3 n=p_transform.basis.xform_inv(p_normal).normalized();
  302. float h = (n.z > 0) ? height : -height;
  303. n *= radius;
  304. n.z += h * 0.5;
  305. r_max=p_normal.dot(p_transform.xform(n));
  306. r_min=p_normal.dot(p_transform.xform(-n));
  307. return;
  308. n = p_transform.basis.xform(n);
  309. float distance = p_normal.dot( p_transform.origin );
  310. float length = Math::abs(p_normal.dot(n));
  311. r_min = distance - length;
  312. r_max = distance + length;
  313. ERR_FAIL_COND( r_max < r_min );
  314. }
  315. Vector3 CapsuleShapeSW::get_support(const Vector3& p_normal) const {
  316. Vector3 n=p_normal;
  317. float h = (n.z > 0) ? height : -height;
  318. n*=radius;
  319. n.z += h*0.5;
  320. return n;
  321. }
  322. void CapsuleShapeSW::get_supports(const Vector3& p_normal,int p_max,Vector3 *r_supports,int & r_amount) const {
  323. Vector3 n=p_normal;
  324. float d = n.z;
  325. if (Math::abs( d )<_EDGE_IS_VALID_SUPPORT_TRESHOLD ) {
  326. // make it flat
  327. n.z=0.0;
  328. n.normalize();
  329. n*=radius;
  330. r_amount=2;
  331. r_supports[0]=n;
  332. r_supports[0].z+=height*0.5;
  333. r_supports[1]=n;
  334. r_supports[1].z-=height*0.5;
  335. } else {
  336. float h = (d > 0) ? height : -height;
  337. n*=radius;
  338. n.z += h*0.5;
  339. r_amount=1;
  340. *r_supports=n;
  341. }
  342. }
  343. bool CapsuleShapeSW::intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_result, Vector3 &r_normal) const {
  344. Vector3 norm=(p_end-p_begin).normalized();
  345. float min_d=1e20;
  346. Vector3 res,n;
  347. bool collision=false;
  348. Vector3 auxres,auxn;
  349. bool collided;
  350. // test against cylinder and spheres :-|
  351. collided = Geometry::segment_intersects_cylinder(p_begin,p_end,height,radius,&auxres,&auxn);
  352. if (collided) {
  353. float d=norm.dot(auxres);
  354. if (d<min_d) {
  355. min_d=d;
  356. res=auxres;
  357. n=auxn;
  358. collision=true;
  359. }
  360. }
  361. collided = Geometry::segment_intersects_sphere(p_begin,p_end,Vector3(0,0,height*0.5),radius,&auxres,&auxn);
  362. if (collided) {
  363. float d=norm.dot(auxres);
  364. if (d<min_d) {
  365. min_d=d;
  366. res=auxres;
  367. n=auxn;
  368. collision=true;
  369. }
  370. }
  371. collided = Geometry::segment_intersects_sphere(p_begin,p_end,Vector3(0,0,height*-0.5),radius,&auxres,&auxn);
  372. if (collided) {
  373. float d=norm.dot(auxres);
  374. if (d<min_d) {
  375. min_d=d;
  376. res=auxres;
  377. n=auxn;
  378. collision=true;
  379. }
  380. }
  381. if (collision) {
  382. r_result=res;
  383. r_normal=n;
  384. }
  385. return collision;
  386. }
  387. Vector3 CapsuleShapeSW::get_moment_of_inertia(float p_mass) const {
  388. // use crappy AABB approximation
  389. Vector3 extents=get_aabb().size*0.5;
  390. return Vector3(
  391. (p_mass/3.0) * (extents.y*extents.y + extents.z*extents.z),
  392. (p_mass/3.0) * (extents.x*extents.x + extents.z*extents.z),
  393. (p_mass/3.0) * (extents.y*extents.y + extents.y*extents.y)
  394. );
  395. }
  396. void CapsuleShapeSW::_setup(real_t p_height,real_t p_radius) {
  397. height=p_height;
  398. radius=p_radius;
  399. configure(AABB(Vector3(-radius,-radius,-height*0.5-radius),Vector3(radius*2,radius*2,height+radius*2.0)));
  400. }
  401. void CapsuleShapeSW::set_data(const Variant& p_data) {
  402. Dictionary d = p_data;
  403. ERR_FAIL_COND(!d.has("radius"));
  404. ERR_FAIL_COND(!d.has("height"));
  405. _setup(d["height"],d["radius"]);
  406. }
  407. Variant CapsuleShapeSW::get_data() const {
  408. Dictionary d;
  409. d["radius"]=radius;
  410. d["height"]=height;
  411. return d;
  412. }
  413. CapsuleShapeSW::CapsuleShapeSW() {
  414. height=radius=0;
  415. }
  416. /********** CONVEX POLYGON *************/
  417. void ConvexPolygonShapeSW::project_range(const Vector3& p_normal, const Transform& p_transform, real_t &r_min, real_t &r_max) const {
  418. int vertex_count=mesh.vertices.size();
  419. if (vertex_count==0)
  420. return;
  421. const Vector3 *vrts=&mesh.vertices[0];
  422. for (int i=0;i<vertex_count;i++) {
  423. float d=p_normal.dot( p_transform.xform( vrts[i] ) );
  424. if (i==0 || d > r_max)
  425. r_max=d;
  426. if (i==0 || d < r_min)
  427. r_min=d;
  428. }
  429. }
  430. Vector3 ConvexPolygonShapeSW::get_support(const Vector3& p_normal) const {
  431. Vector3 n=p_normal;
  432. int vert_support_idx=-1;
  433. float support_max;
  434. int vertex_count=mesh.vertices.size();
  435. if (vertex_count==0)
  436. return Vector3();
  437. const Vector3 *vrts=&mesh.vertices[0];
  438. for (int i=0;i<vertex_count;i++) {
  439. float d=n.dot(vrts[i]);
  440. if (i==0 || d > support_max) {
  441. support_max=d;
  442. vert_support_idx=i;
  443. }
  444. }
  445. return vrts[vert_support_idx];
  446. }
  447. void ConvexPolygonShapeSW::get_supports(const Vector3& p_normal,int p_max,Vector3 *r_supports,int & r_amount) const {
  448. const Geometry::MeshData::Face *faces = mesh.faces.ptr();
  449. int fc = mesh.faces.size();
  450. const Geometry::MeshData::Edge *edges = mesh.edges.ptr();
  451. int ec = mesh.edges.size();
  452. const Vector3 *vertices = mesh.vertices.ptr();
  453. int vc = mesh.vertices.size();
  454. //find vertex first
  455. real_t max;
  456. int vtx;
  457. for (int i=0;i<vc;i++) {
  458. float d=p_normal.dot(vertices[i]);
  459. if (i==0 || d > max) {
  460. max=d;
  461. vtx=i;
  462. }
  463. }
  464. for(int i=0;i<fc;i++) {
  465. if (faces[i].plane.normal.dot(p_normal)>_FACE_IS_VALID_SUPPORT_TRESHOLD) {
  466. int ic = faces[i].indices.size();
  467. const int *ind=faces[i].indices.ptr();
  468. bool valid=false;
  469. for(int j=0;j<ic;j++) {
  470. if (ind[j]==vtx) {
  471. valid=true;
  472. break;
  473. }
  474. }
  475. if (!valid)
  476. continue;
  477. int m = MIN(p_max,ic);
  478. for(int j=0;j<m;j++) {
  479. r_supports[j]=vertices[ind[j]];
  480. }
  481. r_amount=m;
  482. return;
  483. }
  484. }
  485. for(int i=0;i<ec;i++) {
  486. float dot=(vertices[edges[i].a]-vertices[edges[i].b]).normalized().dot(p_normal);
  487. dot=ABS(dot);
  488. if (dot < _EDGE_IS_VALID_SUPPORT_TRESHOLD && (edges[i].a==vtx || edges[i].b==vtx)) {
  489. r_amount=2;
  490. r_supports[0]=vertices[edges[i].a];
  491. r_supports[1]=vertices[edges[i].b];
  492. return;
  493. }
  494. }
  495. r_supports[0]=vertices[vtx];
  496. r_amount=1;
  497. }
  498. bool ConvexPolygonShapeSW::intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_result, Vector3 &r_normal) const {
  499. const Geometry::MeshData::Face *faces = mesh.faces.ptr();
  500. int fc = mesh.faces.size();
  501. const Vector3 *vertices = mesh.vertices.ptr();
  502. int vc = mesh.vertices.size();
  503. Vector3 n = p_end-p_begin;
  504. float min = 1e20;
  505. bool col=false;
  506. for(int i=0;i<fc;i++) {
  507. if (faces[i].plane.normal.dot(n) > 0)
  508. continue; //opposing face
  509. int ic = faces[i].indices.size();
  510. const int *ind=faces[i].indices.ptr();
  511. for(int j=1;j<ic-1;j++) {
  512. Face3 f(vertices[ind[0]],vertices[ind[j]],vertices[ind[j+1]]);
  513. Vector3 result;
  514. if (f.intersects_segment(p_begin,p_end,&result)) {
  515. float d = n.dot(result);
  516. if (d<min) {
  517. min=d;
  518. r_result=result;
  519. r_normal=faces[i].plane.normal;
  520. col=true;
  521. }
  522. break;
  523. }
  524. }
  525. }
  526. return col;
  527. }
  528. Vector3 ConvexPolygonShapeSW::get_moment_of_inertia(float p_mass) const {
  529. // use crappy AABB approximation
  530. Vector3 extents=get_aabb().size*0.5;
  531. return Vector3(
  532. (p_mass/3.0) * (extents.y*extents.y + extents.z*extents.z),
  533. (p_mass/3.0) * (extents.x*extents.x + extents.z*extents.z),
  534. (p_mass/3.0) * (extents.y*extents.y + extents.y*extents.y)
  535. );
  536. }
  537. void ConvexPolygonShapeSW::_setup(const Vector<Vector3>& p_vertices) {
  538. Error err = QuickHull::build(p_vertices,mesh);
  539. AABB _aabb;
  540. for(int i=0;i<mesh.vertices.size();i++) {
  541. if (i==0)
  542. _aabb.pos=mesh.vertices[i];
  543. else
  544. _aabb.expand_to(mesh.vertices[i]);
  545. }
  546. configure(_aabb);
  547. }
  548. void ConvexPolygonShapeSW::set_data(const Variant& p_data) {
  549. _setup(p_data);
  550. }
  551. Variant ConvexPolygonShapeSW::get_data() const {
  552. return mesh.vertices;
  553. }
  554. ConvexPolygonShapeSW::ConvexPolygonShapeSW() {
  555. }
  556. /********** FACE POLYGON *************/
  557. void FaceShapeSW::project_range(const Vector3& p_normal, const Transform& p_transform, real_t &r_min, real_t &r_max) const {
  558. for (int i=0;i<3;i++) {
  559. Vector3 v=p_transform.xform(vertex[i]);
  560. float d=p_normal.dot(v);
  561. if (i==0 || d > r_max)
  562. r_max=d;
  563. if (i==0 || d < r_min)
  564. r_min=d;
  565. }
  566. }
  567. Vector3 FaceShapeSW::get_support(const Vector3& p_normal) const {
  568. Vector3 n=p_normal;
  569. int vert_support_idx=-1;
  570. float support_max;
  571. for (int i=0;i<3;i++) {
  572. //float d=n.dot(vertex[i]);
  573. float d=p_normal.dot(vertex[i]);
  574. if (i==0 || d > support_max) {
  575. support_max=d;
  576. vert_support_idx=i;
  577. }
  578. }
  579. return vertex[vert_support_idx];
  580. }
  581. void FaceShapeSW::get_supports(const Vector3& p_normal,int p_max,Vector3 *r_supports,int & r_amount) const {
  582. Vector3 n=p_normal;
  583. /** TEST FACE AS SUPPORT **/
  584. if (normal.dot(n) > _FACE_IS_VALID_SUPPORT_TRESHOLD) {
  585. r_amount=3;
  586. for (int i=0;i<3;i++) {
  587. r_supports[i]=vertex[i];
  588. }
  589. return;
  590. }
  591. /** FIND SUPPORT VERTEX **/
  592. int vert_support_idx=-1;
  593. float support_max;
  594. for (int i=0;i<3;i++) {
  595. float d=n.dot(vertex[i]);
  596. if (i==0 || d > support_max) {
  597. support_max=d;
  598. vert_support_idx=i;
  599. }
  600. }
  601. /** TEST EDGES AS SUPPORT **/
  602. for (int i=0;i<3;i++) {
  603. int nx=(i+1)%3;
  604. //if (i!=vert_support_idx && nx!=vert_support_idx)
  605. // continue;
  606. // check if edge is valid as a support
  607. float dot=(vertex[i]-vertex[nx]).normalized().dot(n);
  608. dot=ABS(dot);
  609. if (dot < _EDGE_IS_VALID_SUPPORT_TRESHOLD) {
  610. r_amount=2;
  611. r_supports[0]=vertex[i];
  612. r_supports[1]=vertex[nx];
  613. return;
  614. }
  615. }
  616. r_amount=1;
  617. r_supports[0]=vertex[vert_support_idx];
  618. }
  619. bool FaceShapeSW::intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_result, Vector3 &r_normal) const {
  620. bool c=Geometry::segment_intersects_triangle(p_begin,p_end,vertex[0],vertex[1],vertex[2],&r_result);
  621. if (c)
  622. r_normal=Plane(vertex[0],vertex[1],vertex[2]).normal;
  623. return c;
  624. }
  625. Vector3 FaceShapeSW::get_moment_of_inertia(float p_mass) const {
  626. return Vector3(); // Sorry, but i don't think anyone cares, FaceShape!
  627. }
  628. FaceShapeSW::FaceShapeSW() {
  629. configure(AABB());
  630. }
  631. DVector<Vector3> ConcavePolygonShapeSW::get_faces() const {
  632. DVector<Vector3> rfaces;
  633. rfaces.resize(faces.size()*3);
  634. for(int i=0;i<faces.size();i++) {
  635. Face f=faces.get(i);
  636. for(int j=0;j<3;j++) {
  637. rfaces.set(i*3+j, vertices.get( f.indices[j] ) );
  638. }
  639. }
  640. return rfaces;
  641. }
  642. void ConcavePolygonShapeSW::project_range(const Vector3& p_normal, const Transform& p_transform, real_t &r_min, real_t &r_max) const {
  643. int count=vertices.size();
  644. DVector<Vector3>::Read r=vertices.read();
  645. const Vector3 *vptr=r.ptr();
  646. for (int i=0;i<count;i++) {
  647. float d=p_normal.dot( p_transform.xform( vptr[i] ) );
  648. if (i==0 || d > r_max)
  649. r_max=d;
  650. if (i==0 || d < r_min)
  651. r_min=d;
  652. }
  653. }
  654. Vector3 ConcavePolygonShapeSW::get_support(const Vector3& p_normal) const {
  655. int count=vertices.size();
  656. DVector<Vector3>::Read r=vertices.read();
  657. const Vector3 *vptr=r.ptr();
  658. Vector3 n=p_normal;
  659. int vert_support_idx=-1;
  660. float support_max;
  661. for (int i=0;i<count;i++) {
  662. float d=n.dot(vptr[i]);
  663. if (i==0 || d > support_max) {
  664. support_max=d;
  665. vert_support_idx=i;
  666. }
  667. }
  668. return vptr[vert_support_idx];
  669. }
  670. void ConcavePolygonShapeSW::_cull_segment(int p_idx,_SegmentCullParams *p_params) const {
  671. const BVH *bvh=&p_params->bvh[p_idx];
  672. //if (p_params->dir.dot(bvh->aabb.get_support(-p_params->dir))>p_params->min_d)
  673. // return; //test against whole AABB, which isn't very costly
  674. //printf("addr: %p\n",bvh);
  675. if (!bvh->aabb.intersects_segment(p_params->from,p_params->to)) {
  676. return;
  677. }
  678. if (bvh->face_index>=0) {
  679. Vector3 res;
  680. Vector3 vertices[3]={
  681. p_params->vertices[ p_params->faces[ bvh->face_index ].indices[0] ],
  682. p_params->vertices[ p_params->faces[ bvh->face_index ].indices[1] ],
  683. p_params->vertices[ p_params->faces[ bvh->face_index ].indices[2] ]
  684. };
  685. if (Geometry::segment_intersects_triangle(
  686. p_params->from,
  687. p_params->to,
  688. vertices[0],
  689. vertices[1],
  690. vertices[2],
  691. &res)) {
  692. float d=p_params->normal.dot(res) - p_params->normal.dot(p_params->from);
  693. //TODO, seems segmen/triangle intersection is broken :(
  694. if (d>0 && d<p_params->min_d) {
  695. p_params->min_d=d;
  696. p_params->result=res;
  697. p_params->normal=Plane(vertices[0],vertices[1],vertices[2]).normal;
  698. p_params->collisions++;
  699. }
  700. }
  701. } else {
  702. if (bvh->left>=0)
  703. _cull_segment(bvh->left,p_params);
  704. if (bvh->right>=0)
  705. _cull_segment(bvh->right,p_params);
  706. }
  707. }
  708. bool ConcavePolygonShapeSW::intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_result, Vector3 &r_normal) const {
  709. // unlock data
  710. DVector<Face>::Read fr=faces.read();
  711. DVector<Vector3>::Read vr=vertices.read();
  712. DVector<BVH>::Read br=bvh.read();
  713. _SegmentCullParams params;
  714. params.from=p_begin;
  715. params.to=p_end;
  716. params.collisions=0;
  717. params.normal=(p_end-p_begin).normalized();
  718. params.faces=fr.ptr();
  719. params.vertices=vr.ptr();
  720. params.bvh=br.ptr();
  721. params.min_d=1e20;
  722. // cull
  723. _cull_segment(0,&params);
  724. if (params.collisions>0) {
  725. r_result=params.result;
  726. r_normal=params.normal;
  727. return true;
  728. } else {
  729. return false;
  730. }
  731. }
  732. void ConcavePolygonShapeSW::_cull(int p_idx,_CullParams *p_params) const {
  733. const BVH* bvh=&p_params->bvh[p_idx];
  734. if (!p_params->aabb.intersects( bvh->aabb ))
  735. return;
  736. if (bvh->face_index>=0) {
  737. const Face *f=&p_params->faces[ bvh->face_index ];
  738. FaceShapeSW *face=p_params->face;
  739. face->normal=f->normal;
  740. face->vertex[0]=p_params->vertices[f->indices[0]];
  741. face->vertex[1]=p_params->vertices[f->indices[1]];
  742. face->vertex[2]=p_params->vertices[f->indices[2]];
  743. p_params->callback(p_params->userdata,face);
  744. } else {
  745. if (bvh->left>=0) {
  746. _cull(bvh->left,p_params);
  747. }
  748. if (bvh->right>=0) {
  749. _cull(bvh->right,p_params);
  750. }
  751. }
  752. }
  753. void ConcavePolygonShapeSW::cull(const AABB& p_local_aabb,Callback p_callback,void* p_userdata) const {
  754. // make matrix local to concave
  755. AABB local_aabb=p_local_aabb;
  756. // unlock data
  757. DVector<Face>::Read fr=faces.read();
  758. DVector<Vector3>::Read vr=vertices.read();
  759. DVector<BVH>::Read br=bvh.read();
  760. FaceShapeSW face; // use this to send in the callback
  761. _CullParams params;
  762. params.aabb=local_aabb;
  763. params.face=&face;
  764. params.faces=fr.ptr();
  765. params.vertices=vr.ptr();
  766. params.bvh=br.ptr();
  767. params.callback=p_callback;
  768. params.userdata=p_userdata;
  769. // cull
  770. _cull(0,&params);
  771. }
  772. Vector3 ConcavePolygonShapeSW::get_moment_of_inertia(float p_mass) const {
  773. // use crappy AABB approximation
  774. Vector3 extents=get_aabb().size*0.5;
  775. return Vector3(
  776. (p_mass/3.0) * (extents.y*extents.y + extents.z*extents.z),
  777. (p_mass/3.0) * (extents.x*extents.x + extents.z*extents.z),
  778. (p_mass/3.0) * (extents.y*extents.y + extents.y*extents.y)
  779. );
  780. }
  781. struct _VolumeSW_BVH_Element {
  782. AABB aabb;
  783. Vector3 center;
  784. int face_index;
  785. };
  786. struct _VolumeSW_BVH_CompareX {
  787. _FORCE_INLINE_ bool operator ()(const _VolumeSW_BVH_Element& a, const _VolumeSW_BVH_Element& b) const {
  788. return a.center.x<b.center.x;
  789. }
  790. };
  791. struct _VolumeSW_BVH_CompareY {
  792. _FORCE_INLINE_ bool operator ()(const _VolumeSW_BVH_Element& a, const _VolumeSW_BVH_Element& b) const {
  793. return a.center.y<b.center.y;
  794. }
  795. };
  796. struct _VolumeSW_BVH_CompareZ {
  797. _FORCE_INLINE_ bool operator ()(const _VolumeSW_BVH_Element& a, const _VolumeSW_BVH_Element& b) const {
  798. return a.center.z<b.center.z;
  799. }
  800. };
  801. struct _VolumeSW_BVH {
  802. AABB aabb;
  803. _VolumeSW_BVH *left;
  804. _VolumeSW_BVH *right;
  805. int face_index;
  806. };
  807. _VolumeSW_BVH* _volume_sw_build_bvh(_VolumeSW_BVH_Element *p_elements,int p_size,int &count) {
  808. _VolumeSW_BVH* bvh = memnew( _VolumeSW_BVH );
  809. if (p_size==1) {
  810. //leaf
  811. bvh->aabb=p_elements[0].aabb;
  812. bvh->left=NULL;
  813. bvh->right=NULL;
  814. bvh->face_index=p_elements->face_index;
  815. count++;
  816. return bvh;
  817. } else {
  818. bvh->face_index=-1;
  819. }
  820. AABB aabb;
  821. for(int i=0;i<p_size;i++) {
  822. if (i==0)
  823. aabb=p_elements[i].aabb;
  824. else
  825. aabb.merge_with(p_elements[i].aabb);
  826. }
  827. bvh->aabb=aabb;
  828. switch(aabb.get_longest_axis_index()) {
  829. case 0: {
  830. SortArray<_VolumeSW_BVH_Element,_VolumeSW_BVH_CompareX> sort_x;
  831. sort_x.sort(p_elements,p_size);
  832. } break;
  833. case 1: {
  834. SortArray<_VolumeSW_BVH_Element,_VolumeSW_BVH_CompareY> sort_y;
  835. sort_y.sort(p_elements,p_size);
  836. } break;
  837. case 2: {
  838. SortArray<_VolumeSW_BVH_Element,_VolumeSW_BVH_CompareZ> sort_z;
  839. sort_z.sort(p_elements,p_size);
  840. } break;
  841. }
  842. int split=p_size/2;
  843. bvh->left=_volume_sw_build_bvh(p_elements,split,count);
  844. bvh->right=_volume_sw_build_bvh(&p_elements[split],p_size-split,count);
  845. // printf("branch at %p - %i: %i\n",bvh,count,bvh->face_index);
  846. count++;
  847. return bvh;
  848. }
  849. void ConcavePolygonShapeSW::_fill_bvh(_VolumeSW_BVH* p_bvh_tree,BVH* p_bvh_array,int& p_idx) {
  850. int idx=p_idx;
  851. p_bvh_array[idx].aabb=p_bvh_tree->aabb;
  852. p_bvh_array[idx].face_index=p_bvh_tree->face_index;
  853. // printf("%p - %i: %i(%p) -- %p:%p\n",%p_bvh_array[idx],p_idx,p_bvh_array[i]->face_index,&p_bvh_tree->face_index,p_bvh_tree->left,p_bvh_tree->right);
  854. if (p_bvh_tree->left) {
  855. p_bvh_array[idx].left=++p_idx;
  856. _fill_bvh(p_bvh_tree->left,p_bvh_array,p_idx);
  857. } else {
  858. p_bvh_array[p_idx].left=-1;
  859. }
  860. if (p_bvh_tree->right) {
  861. p_bvh_array[idx].right=++p_idx;
  862. _fill_bvh(p_bvh_tree->right,p_bvh_array,p_idx);
  863. } else {
  864. p_bvh_array[p_idx].right=-1;
  865. }
  866. memdelete(p_bvh_tree);
  867. }
  868. void ConcavePolygonShapeSW::_setup(DVector<Vector3> p_faces) {
  869. int src_face_count=p_faces.size();
  870. ERR_FAIL_COND(src_face_count%3);
  871. src_face_count/=3;
  872. DVector<Vector3>::Read r = p_faces.read();
  873. const Vector3 * facesr= r.ptr();
  874. #if 0
  875. Map<Vector3,int> point_map;
  876. List<Face> face_list;
  877. for(int i=0;i<src_face_count;i++) {
  878. Face3 faceaux;
  879. for(int j=0;j<3;j++) {
  880. faceaux.vertex[j]=facesr[i*3+j].snapped(_POINT_SNAP);
  881. //faceaux.vertex[j]=facesr[i*3+j];//facesr[i*3+j].snapped(_POINT_SNAP);
  882. }
  883. ERR_CONTINUE( faceaux.is_degenerate() );
  884. Face face;
  885. for(int j=0;j<3;j++) {
  886. Map<Vector3,int>::Element *E=point_map.find(faceaux.vertex[j]);
  887. if (E) {
  888. face.indices[j]=E->value();
  889. } else {
  890. face.indices[j]=point_map.size();
  891. point_map.insert(faceaux.vertex[j],point_map.size());
  892. }
  893. }
  894. face_list.push_back(face);
  895. }
  896. vertices.resize( point_map.size() );
  897. DVector<Vector3>::Write vw = vertices.write();
  898. Vector3 *verticesw=vw.ptr();
  899. AABB _aabb;
  900. for( Map<Vector3,int>::Element *E=point_map.front();E;E=E->next()) {
  901. if (E==point_map.front()) {
  902. _aabb.pos=E->key();
  903. } else {
  904. _aabb.expand_to(E->key());
  905. }
  906. verticesw[E->value()]=E->key();
  907. }
  908. point_map.clear(); // not needed anymore
  909. faces.resize(face_list.size());
  910. DVector<Face>::Write w = faces.write();
  911. Face *facesw=w.ptr();
  912. int fc=0;
  913. for( List<Face>::Element *E=face_list.front();E;E=E->next()) {
  914. facesw[fc++]=E->get();
  915. }
  916. face_list.clear();
  917. DVector<_VolumeSW_BVH_Element> bvh_array;
  918. bvh_array.resize( fc );
  919. DVector<_VolumeSW_BVH_Element>::Write bvhw = bvh_array.write();
  920. _VolumeSW_BVH_Element *bvh_arrayw=bvhw.ptr();
  921. for(int i=0;i<fc;i++) {
  922. AABB face_aabb;
  923. face_aabb.pos=verticesw[facesw[i].indices[0]];
  924. face_aabb.expand_to( verticesw[facesw[i].indices[1]] );
  925. face_aabb.expand_to( verticesw[facesw[i].indices[2]] );
  926. bvh_arrayw[i].face_index=i;
  927. bvh_arrayw[i].aabb=face_aabb;
  928. bvh_arrayw[i].center=face_aabb.pos+face_aabb.size*0.5;
  929. }
  930. w=DVector<Face>::Write();
  931. vw=DVector<Vector3>::Write();
  932. int count=0;
  933. _VolumeSW_BVH *bvh_tree=_volume_sw_build_bvh(bvh_arrayw,fc,count);
  934. ERR_FAIL_COND(count==0);
  935. bvhw=DVector<_VolumeSW_BVH_Element>::Write();
  936. bvh.resize( count+1 );
  937. DVector<BVH>::Write bvhw2 = bvh.write();
  938. BVH*bvh_arrayw2=bvhw2.ptr();
  939. int idx=0;
  940. _fill_bvh(bvh_tree,bvh_arrayw2,idx);
  941. set_aabb(_aabb);
  942. #else
  943. DVector<_VolumeSW_BVH_Element> bvh_array;
  944. bvh_array.resize( src_face_count );
  945. DVector<_VolumeSW_BVH_Element>::Write bvhw = bvh_array.write();
  946. _VolumeSW_BVH_Element *bvh_arrayw=bvhw.ptr();
  947. faces.resize(src_face_count);
  948. DVector<Face>::Write w = faces.write();
  949. Face *facesw=w.ptr();
  950. vertices.resize( src_face_count*3 );
  951. DVector<Vector3>::Write vw = vertices.write();
  952. Vector3 *verticesw=vw.ptr();
  953. AABB _aabb;
  954. for(int i=0;i<src_face_count;i++) {
  955. Face3 face( facesr[i*3+0], facesr[i*3+1], facesr[i*3+2] );
  956. bvh_arrayw[i].aabb=face.get_aabb();
  957. bvh_arrayw[i].center = bvh_arrayw[i].aabb.pos + bvh_arrayw[i].aabb.size * 0.5;
  958. bvh_arrayw[i].face_index=i;
  959. facesw[i].indices[0]=i*3+0;
  960. facesw[i].indices[1]=i*3+1;
  961. facesw[i].indices[2]=i*3+2;
  962. facesw[i].normal=face.get_plane().normal;
  963. verticesw[i*3+0]=face.vertex[0];
  964. verticesw[i*3+1]=face.vertex[1];
  965. verticesw[i*3+2]=face.vertex[2];
  966. if (i==0)
  967. _aabb=bvh_arrayw[i].aabb;
  968. else
  969. _aabb.merge_with(bvh_arrayw[i].aabb);
  970. }
  971. w=DVector<Face>::Write();
  972. vw=DVector<Vector3>::Write();
  973. int count=0;
  974. _VolumeSW_BVH *bvh_tree=_volume_sw_build_bvh(bvh_arrayw,src_face_count,count);
  975. bvh.resize( count+1 );
  976. DVector<BVH>::Write bvhw2 = bvh.write();
  977. BVH*bvh_arrayw2=bvhw2.ptr();
  978. int idx=0;
  979. _fill_bvh(bvh_tree,bvh_arrayw2,idx);
  980. configure(_aabb); // this type of shape has no margin
  981. #endif
  982. }
  983. void ConcavePolygonShapeSW::set_data(const Variant& p_data) {
  984. _setup(p_data);
  985. }
  986. Variant ConcavePolygonShapeSW::get_data() const {
  987. return get_faces();
  988. }
  989. ConcavePolygonShapeSW::ConcavePolygonShapeSW() {
  990. }
  991. /* HEIGHT MAP SHAPE */
  992. DVector<float> HeightMapShapeSW::get_heights() const {
  993. return heights;
  994. }
  995. int HeightMapShapeSW::get_width() const {
  996. return width;
  997. }
  998. int HeightMapShapeSW::get_depth() const {
  999. return depth;
  1000. }
  1001. float HeightMapShapeSW::get_cell_size() const {
  1002. return cell_size;
  1003. }
  1004. void HeightMapShapeSW::project_range(const Vector3& p_normal, const Transform& p_transform, real_t &r_min, real_t &r_max) const {
  1005. //not very useful, but not very used either
  1006. p_transform.xform(get_aabb()).project_range_in_plane( Plane(p_normal,0),r_min,r_max );
  1007. }
  1008. Vector3 HeightMapShapeSW::get_support(const Vector3& p_normal) const {
  1009. //not very useful, but not very used either
  1010. return get_aabb().get_support(p_normal);
  1011. }
  1012. bool HeightMapShapeSW::intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_point, Vector3 &r_normal) const {
  1013. return false;
  1014. }
  1015. void HeightMapShapeSW::cull(const AABB& p_local_aabb,Callback p_callback,void* p_userdata) const {
  1016. }
  1017. Vector3 HeightMapShapeSW::get_moment_of_inertia(float p_mass) const {
  1018. // use crappy AABB approximation
  1019. Vector3 extents=get_aabb().size*0.5;
  1020. return Vector3(
  1021. (p_mass/3.0) * (extents.y*extents.y + extents.z*extents.z),
  1022. (p_mass/3.0) * (extents.x*extents.x + extents.z*extents.z),
  1023. (p_mass/3.0) * (extents.y*extents.y + extents.y*extents.y)
  1024. );
  1025. }
  1026. void HeightMapShapeSW::_setup(DVector<real_t> p_heights,int p_width,int p_depth,real_t p_cell_size) {
  1027. heights=p_heights;
  1028. width=p_width;
  1029. depth=p_depth;;
  1030. cell_size=p_cell_size;
  1031. DVector<real_t>::Read r = heights. read();
  1032. AABB aabb;
  1033. for(int i=0;i<depth;i++) {
  1034. for(int j=0;j<width;j++) {
  1035. float h = r[i*width+j];
  1036. Vector3 pos( j*cell_size, h, i*cell_size );
  1037. if (i==0 || j==0)
  1038. aabb.pos=pos;
  1039. else
  1040. aabb.expand_to(pos);
  1041. }
  1042. }
  1043. configure(aabb);
  1044. }
  1045. void HeightMapShapeSW::set_data(const Variant& p_data) {
  1046. ERR_FAIL_COND( p_data.get_type()!=Variant::DICTIONARY );
  1047. Dictionary d=p_data;
  1048. ERR_FAIL_COND( !d.has("width") );
  1049. ERR_FAIL_COND( !d.has("depth") );
  1050. ERR_FAIL_COND( !d.has("cell_size") );
  1051. ERR_FAIL_COND( !d.has("heights") );
  1052. int width=d["width"];
  1053. int depth=d["depth"];
  1054. float cell_size=d["cell_size"];
  1055. DVector<float> heights=d["heights"];
  1056. ERR_FAIL_COND( width<= 0);
  1057. ERR_FAIL_COND( depth<= 0);
  1058. ERR_FAIL_COND( cell_size<= CMP_EPSILON);
  1059. ERR_FAIL_COND( heights.size() != (width*depth) );
  1060. _setup(heights, width, depth, cell_size );
  1061. }
  1062. Variant HeightMapShapeSW::get_data() const {
  1063. ERR_FAIL_V(Variant());
  1064. }
  1065. HeightMapShapeSW::HeightMapShapeSW() {
  1066. width=0;
  1067. depth=0;
  1068. cell_size=0;
  1069. }