Advertisement
Guest User

dangling_contact_graph_pointers.diff

a guest
Nov 11th, 2011
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 4.67 KB | None | 0 0
  1. diff --git a/Demo/Player.c b/Demo/Player.c
  2. index 87e4dd8..7451bbd 100644
  3. --- a/Demo/Player.c
  4. +++ b/Demo/Player.c
  5. @@ -22,6 +22,19 @@
  6.  #include <stdlib.h>
  7.  #include <math.h>
  8.  
  9. +#ifdef __APPLE__
  10. +   #include "OpenGL/gl.h"
  11. +   #include "OpenGL/glu.h"
  12. +   #include <GLUT/glut.h>
  13. +#else
  14. +#ifdef WIN32
  15. +   #include <windows.h>
  16. +#endif
  17. +   #include <GL/gl.h>
  18. +   #include <GL/glu.h>
  19. +   #include <GL/glut.h>
  20. +#endif
  21. +
  22.  // TODO
  23.  #include "chipmunk_private.h"
  24.  #include "ChipmunkDemo.h"
  25. @@ -47,6 +60,32 @@ static cpShape *playerShape = NULL;
  26.  static cpFloat remainingBoost = 0;
  27.  static cpBool grounded = cpFalse;
  28.  static cpBool lastJumpState = cpFalse;
  29. +static int refCount = 0;
  30. +
  31. +static int playerGroundCollisionBegin(cpArbiter *arb, cpSpace *space, void *unused)
  32. +{
  33. +   refCount++;
  34. +   printf("collsion, refcount=%d\n", refCount);
  35. +   fflush(0);
  36. +
  37. +   cpVect normal = cpArbiterGetNormal(arb,0);
  38. +   cpFloat angle = cpvtoangle(cpvperp(normal));
  39. +
  40. +   cpBodySetAngle(playerBody, angle);
  41. +   cpSpaceReindexShapesForBody(space, playerBody);
  42. +
  43. +        // Continue normal chipmunk processing
  44. +        return 1;
  45. +}
  46. +
  47. +static void playerGroundCollisionSeparate(cpArbiter *arb, cpSpace *space, void *unused)
  48. +{
  49. +   refCount--;
  50. +   printf("separate, refcount=%d\n", refCount);
  51. +   fflush(0);
  52. +        // Continue normal chipmunk processing
  53. +        return;
  54. +}
  55.  
  56.  static void
  57.  SelectPlayerGroundNormal(cpBody *body, cpArbiter *arb, cpVect *groundNormal){
  58. @@ -79,7 +118,10 @@ playerUpdateVelocity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
  59.    
  60.     // Update the surface velocity and friction
  61.     cpVect surface_v = cpv(target_vx, 0.0);
  62. -   playerShape->surface_v = surface_v;
  63. +   cpVect gravityvect = cpvmult(cpvrperp(cpvforangle(cpBodyGetAngle(playerBody))), 50); // just a small force to keep the character in contact with the ground
  64. +   playerShape->surface_v = cpv(
  65. +                       (surface_v.x + gravityvect.x)/2.0,
  66. +                       (surface_v.y + gravityvect.y) / 2.0);
  67.     playerShape->u = (grounded ? PLAYER_GROUND_ACCEL/GRAVITY : 0.0);
  68.    
  69.     // Apply air control if not grounded
  70. @@ -87,6 +129,7 @@ playerUpdateVelocity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
  71.         // Smoothly accelerate the velocity
  72.         playerBody->v.x = cpflerpconst(playerBody->v.x, target_vx, PLAYER_AIR_ACCEL*dt);
  73.     }
  74. +
  75.    
  76.     body->v.y = cpfclamp(body->v.y, -FALL_VELOCITY, INFINITY);
  77.  }
  78. @@ -146,7 +189,7 @@ init(void)
  79.     shape->layers = NOT_GRABABLE_MASK;
  80.    
  81.     // Set up the player
  82. -   cpFloat radius = 25.0f;
  83. +   cpFloat radius = 10.0f;
  84.     body = cpSpaceAddBody(space, cpBodyNew(1.0f, INFINITY));
  85.     body->p = cpv(0, -200);
  86.     body->velocity_func = playerUpdateVelocity;
  87. @@ -156,18 +199,86 @@ init(void)
  88.     shape->e = 0.0f; shape->u = 0.0f;
  89.     shape->collision_type = 1;
  90.     playerShape = shape;
  91. +
  92. +   cpShape *pshapehead;
  93. +   pshapehead = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpv(0,20)));
  94. +   pshapehead->e = 0.0f; pshapehead->u = 0.0f;
  95. +   pshapehead->collision_type = 1;
  96.    
  97.     // Add some boxes to jump on
  98.     for(int i=0; i<6; i++){
  99.         for(int j=0; j<3; j++){
  100.             body = cpSpaceAddBody(space, cpBodyNew(4.0f, INFINITY));
  101. -           body->p = cpv(100 + j*60, -200 + i*60);
  102. -          
  103. -           shape = cpSpaceAddShape(space, cpBoxShapeNew(body, 50, 50));
  104. +           float x = 100 + j*60;
  105. +           float y = -200 + i*60;
  106. +           body->p = cpv(x, y);
  107. +      
  108. +           cpVect verts1[] = {
  109. +               cpv(-10, -10),
  110. +               cpv(-10, 10),
  111. +               cpv(10, -10),
  112. +           };
  113. +
  114. +           shape = cpPolyShapeNew(body, 3, verts1, cpvzero);
  115. +           //shape = cpSpaceAddShape(space, cpBoxShapeNew(body, 10, 10));
  116.             shape->e = 0.0f; shape->u = 0.7f;
  117. +           shape->collision_type = 0;
  118. +           cpSpaceAddShape(space, shape);
  119. +
  120. +
  121. +           cpShape *shape2;
  122. +
  123. +
  124. +           cpVect verts[] = {
  125. +               cpv(-10, 10),
  126. +               cpv(10, -6),
  127. +               cpv(10, -10),
  128. +           };
  129. +
  130. +           shape2 = cpPolyShapeNew(body, 3, verts, cpvzero);
  131. +           shape2->e = 0.0f; shape2->u = 0.7f;
  132. +           shape2->collision_type = 0;
  133. +           cpSpaceAddShape(space, shape2);
  134. +
  135. +
  136. +           cpShape *shape3;
  137. +           cpVect verts3[] = {
  138. +               cpv(-30, -10),
  139. +               cpv(-30, 15),
  140. +               cpv(-10, 10),
  141. +           };
  142. +
  143. +           shape3 = cpPolyShapeNew(body, 3, verts3, cpvzero);
  144. +           shape3->e = 0.0f; shape3->u = 0.7f;
  145. +           shape3->collision_type = 0;
  146. +           cpSpaceAddShape(space, shape3);
  147. +
  148. +
  149. +           cpShape *shape4;
  150. +           cpVect verts4[] = {
  151. +               cpv(-30, -10),
  152. +               cpv(-10, 10),
  153. +               cpv(-10, -10),
  154. +           };
  155. +
  156. +           shape4 = cpPolyShapeNew(body, 3, verts4, cpvzero);
  157. +           shape4->e = 0.0f; shape4->u = 0.7f;
  158. +           shape4->collision_type = 0;
  159. +           cpSpaceAddShape(space, shape4);
  160. +
  161.         }
  162.     }
  163. -  
  164. +
  165. +       cpSpaceAddCollisionHandler(
  166. +               space,
  167. +               1,
  168. +               0,
  169. +               playerGroundCollisionBegin,
  170. +               NULL,
  171. +               NULL,
  172. +               playerGroundCollisionSeparate,
  173. +               NULL);
  174. +
  175.     return space;
  176.  }
  177.  
  178.  
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement