Guest User

Untitled

a guest
Nov 24th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. //
  2. // ObjectPicker.m
  3. // Deneme
  4. //
  5. // Created by İbrahim Demir on 11/21/13.
  6. //
  7. //
  8.  
  9. #import "ObjectPicker.h"
  10.  
  11. @implementation ObjectPicker
  12.  
  13. + (NGLMesh *) getTouchingMesh : (CGPoint) touchingPoint : (NGLCamera *) camera : (NGLGroup3D *) objects
  14. {
  15. CGPoint touchIn3D;
  16. touchIn3D.x = (touchingPoint.x / [UIScreen mainScreen].bounds.size.width) * 2.0f - 1.0f;
  17. touchIn3D.y = (touchingPoint.y / [UIScreen mainScreen].bounds.size.height) * -2.0f + 1.0f;
  18. NGLmat4 *toWorldNotInversed = [camera matrixViewProjection];
  19. NGLmat4 *toWorld = calloc(1,sizeof(NGLmat4));
  20.  
  21. nglMatrixDescribe(*toWorldNotInversed);
  22.  
  23. nglMatrixInverse(*toWorldNotInversed,*toWorld);
  24.  
  25. nglMatrixDescribe(*toWorld);
  26.  
  27. NGLvec4 from, to;
  28. from.x = (*toWorld)[0] * touchIn3D.x + (*toWorld)[1] * touchIn3D.y - (*toWorld)[2] + (*toWorld)[3];
  29. from.y = (*toWorld)[4] * touchIn3D.x + (*toWorld)[5] * touchIn3D.y - (*toWorld)[6] + (*toWorld)[7];
  30. from.z = (*toWorld)[8] * touchIn3D.x + (*toWorld)[9] * touchIn3D.y - (*toWorld)[10] + (*toWorld)[11];
  31. from.w = (*toWorld)[12] * touchIn3D.x + (*toWorld)[13] * touchIn3D.y - (*toWorld)[14] + (*toWorld)[15];
  32.  
  33.  
  34. to.x = (*toWorld)[0] * touchIn3D.x + (*toWorld)[1] * touchIn3D.y + (*toWorld)[2] + (*toWorld)[3];
  35. to.y = (*toWorld)[4] * touchIn3D.x + (*toWorld)[5] * touchIn3D.y + (*toWorld)[6] + (*toWorld)[7];
  36. to.z = (*toWorld)[8] * touchIn3D.x + (*toWorld)[9] * touchIn3D.y + (*toWorld)[10] + (*toWorld)[11];
  37. to.w = (*toWorld)[12] * touchIn3D.x + (*toWorld)[13] * touchIn3D.y + (*toWorld)[14] + (*toWorld)[15];
  38.  
  39. //NSLog(@"%f %f", touchIn3D.x, touchIn3D.y);
  40. NSLog(@"from: %f %f %f %f", from.x, from.y, from.z, from.w);
  41. NSLog(@"to : %f %f %f %f", to.x, to.y, to.z, to.w);
  42.  
  43. int clickedObject = -1;
  44. float minDist = 99999.0f;
  45.  
  46. for(int i=0; i<objects.count; i++){
  47. float t1;
  48. NGLvec3 direction = (NGLvec3) [self subVec4: to : from];
  49. NGLvec3 from3d;
  50. NGLObject3D *selectedObject = [objects objectWithTag:i];
  51. from3d.x = from.x - [selectedObject position]->x;
  52. from3d.y = from.y - [selectedObject position]->y;
  53. from3d.z = from.z - [selectedObject position]->z;
  54. float r = [self calculateRadius:selectedObject];
  55. if([self hasIntersection:from3d :direction : r :t1]){
  56.  
  57. if(t1<minDist){
  58. minDist = t1;
  59. clickedObject = i;
  60. }
  61. }
  62. }
  63.  
  64. //NSLog(@"---%d----", clickedObject);
  65.  
  66.  
  67. NGLMesh * mesh = nil;
  68. return mesh;
  69.  
  70.  
  71. }
  72.  
  73. + (float) calculateRadius : (NGLObject3D *) object
  74. {
  75. float x = object.boundingBox.aligned.max.x-object.boundingBox.aligned.min.x;
  76. float y = object.boundingBox.aligned.max.y-object.boundingBox.aligned.min.y;
  77. return sqrt(x*x+y*y)/2;
  78. }
  79.  
  80. + (BOOL) hasIntersection : (NGLvec3) from : (NGLvec3) direction : (float) r : (int) t1
  81. {
  82. float A = [self dotProductself:direction];
  83. float B = 2.0f * [self dotProduct:from:direction];
  84. float C = [self dotProductself:from] - r*r;
  85.  
  86. float dis = B * B - 4.0f * A * C;
  87.  
  88. //NSLog(@"dis: %f",dis);
  89. if (dis < 0.0f)
  90. return false;
  91. float S = sqrt(dis);
  92.  
  93. t1 = (-B - S) / (2.0f * A);
  94.  
  95. NSLog(@"t1: %d",t1);
  96.  
  97. return TRUE;
  98. }
  99.  
  100. + (float) dotProductself : (NGLvec3) vec
  101. {
  102. return vec.x*vec.x+vec.y*vec.y+vec.z*vec.z;
  103. }
  104.  
  105.  
  106. + (float) dotProduct : (NGLvec3) vec1: (NGLvec3) vec2
  107. {
  108. return vec1.x*vec2.x+vec1.y*vec2.y+vec1.z*vec2.z;
  109. }
  110.  
  111.  
  112. + (NGLvec3) subVec3 : (NGLvec3) vec1 : (NGLvec3) vec2
  113. {
  114. NGLvec3 result;
  115. result.x = vec1.x-vec2.x;
  116. result.y = vec1.y-vec2.y;
  117. result.z = vec1.z-vec2.z;
  118. return result;
  119. }
  120.  
  121.  
  122. + (NGLvec3) subVec4 : (NGLvec4) vec1 : (NGLvec4) vec2
  123. {
  124. NGLvec3 result;
  125. result.x = vec1.x-vec2.x;
  126. result.y = vec1.y-vec2.y;
  127. result.z = vec1.z-vec2.z;
  128. return result;
  129. }
  130.  
  131.  
  132. @end
Advertisement
Add Comment
Please, Sign In to add comment