Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/Demo/Player.c b/Demo/Player.c
- index 87e4dd8..7451bbd 100644
- --- a/Demo/Player.c
- +++ b/Demo/Player.c
- @@ -22,6 +22,19 @@
- #include <stdlib.h>
- #include <math.h>
- +#ifdef __APPLE__
- + #include "OpenGL/gl.h"
- + #include "OpenGL/glu.h"
- + #include <GLUT/glut.h>
- +#else
- +#ifdef WIN32
- + #include <windows.h>
- +#endif
- + #include <GL/gl.h>
- + #include <GL/glu.h>
- + #include <GL/glut.h>
- +#endif
- +
- // TODO
- #include "chipmunk_private.h"
- #include "ChipmunkDemo.h"
- @@ -47,6 +60,32 @@ static cpShape *playerShape = NULL;
- static cpFloat remainingBoost = 0;
- static cpBool grounded = cpFalse;
- static cpBool lastJumpState = cpFalse;
- +static int refCount = 0;
- +
- +static int playerGroundCollisionBegin(cpArbiter *arb, cpSpace *space, void *unused)
- +{
- + refCount++;
- + printf("collsion, refcount=%d\n", refCount);
- + fflush(0);
- +
- + cpVect normal = cpArbiterGetNormal(arb,0);
- + cpFloat angle = cpvtoangle(cpvperp(normal));
- +
- + cpBodySetAngle(playerBody, angle);
- + cpSpaceReindexShapesForBody(space, playerBody);
- +
- + // Continue normal chipmunk processing
- + return 1;
- +}
- +
- +static void playerGroundCollisionSeparate(cpArbiter *arb, cpSpace *space, void *unused)
- +{
- + refCount--;
- + printf("separate, refcount=%d\n", refCount);
- + fflush(0);
- + // Continue normal chipmunk processing
- + return;
- +}
- static void
- SelectPlayerGroundNormal(cpBody *body, cpArbiter *arb, cpVect *groundNormal){
- @@ -79,7 +118,10 @@ playerUpdateVelocity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
- // Update the surface velocity and friction
- cpVect surface_v = cpv(target_vx, 0.0);
- - playerShape->surface_v = surface_v;
- + cpVect gravityvect = cpvmult(cpvrperp(cpvforangle(cpBodyGetAngle(playerBody))), 50); // just a small force to keep the character in contact with the ground
- + playerShape->surface_v = cpv(
- + (surface_v.x + gravityvect.x)/2.0,
- + (surface_v.y + gravityvect.y) / 2.0);
- playerShape->u = (grounded ? PLAYER_GROUND_ACCEL/GRAVITY : 0.0);
- // Apply air control if not grounded
- @@ -87,6 +129,7 @@ playerUpdateVelocity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
- // Smoothly accelerate the velocity
- playerBody->v.x = cpflerpconst(playerBody->v.x, target_vx, PLAYER_AIR_ACCEL*dt);
- }
- +
- body->v.y = cpfclamp(body->v.y, -FALL_VELOCITY, INFINITY);
- }
- @@ -146,7 +189,7 @@ init(void)
- shape->layers = NOT_GRABABLE_MASK;
- // Set up the player
- - cpFloat radius = 25.0f;
- + cpFloat radius = 10.0f;
- body = cpSpaceAddBody(space, cpBodyNew(1.0f, INFINITY));
- body->p = cpv(0, -200);
- body->velocity_func = playerUpdateVelocity;
- @@ -156,18 +199,86 @@ init(void)
- shape->e = 0.0f; shape->u = 0.0f;
- shape->collision_type = 1;
- playerShape = shape;
- +
- + cpShape *pshapehead;
- + pshapehead = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpv(0,20)));
- + pshapehead->e = 0.0f; pshapehead->u = 0.0f;
- + pshapehead->collision_type = 1;
- // Add some boxes to jump on
- for(int i=0; i<6; i++){
- for(int j=0; j<3; j++){
- body = cpSpaceAddBody(space, cpBodyNew(4.0f, INFINITY));
- - body->p = cpv(100 + j*60, -200 + i*60);
- -
- - shape = cpSpaceAddShape(space, cpBoxShapeNew(body, 50, 50));
- + float x = 100 + j*60;
- + float y = -200 + i*60;
- + body->p = cpv(x, y);
- +
- + cpVect verts1[] = {
- + cpv(-10, -10),
- + cpv(-10, 10),
- + cpv(10, -10),
- + };
- +
- + shape = cpPolyShapeNew(body, 3, verts1, cpvzero);
- + //shape = cpSpaceAddShape(space, cpBoxShapeNew(body, 10, 10));
- shape->e = 0.0f; shape->u = 0.7f;
- + shape->collision_type = 0;
- + cpSpaceAddShape(space, shape);
- +
- +
- + cpShape *shape2;
- +
- +
- + cpVect verts[] = {
- + cpv(-10, 10),
- + cpv(10, -6),
- + cpv(10, -10),
- + };
- +
- + shape2 = cpPolyShapeNew(body, 3, verts, cpvzero);
- + shape2->e = 0.0f; shape2->u = 0.7f;
- + shape2->collision_type = 0;
- + cpSpaceAddShape(space, shape2);
- +
- +
- + cpShape *shape3;
- + cpVect verts3[] = {
- + cpv(-30, -10),
- + cpv(-30, 15),
- + cpv(-10, 10),
- + };
- +
- + shape3 = cpPolyShapeNew(body, 3, verts3, cpvzero);
- + shape3->e = 0.0f; shape3->u = 0.7f;
- + shape3->collision_type = 0;
- + cpSpaceAddShape(space, shape3);
- +
- +
- + cpShape *shape4;
- + cpVect verts4[] = {
- + cpv(-30, -10),
- + cpv(-10, 10),
- + cpv(-10, -10),
- + };
- +
- + shape4 = cpPolyShapeNew(body, 3, verts4, cpvzero);
- + shape4->e = 0.0f; shape4->u = 0.7f;
- + shape4->collision_type = 0;
- + cpSpaceAddShape(space, shape4);
- +
- }
- }
- -
- +
- + cpSpaceAddCollisionHandler(
- + space,
- + 1,
- + 0,
- + playerGroundCollisionBegin,
- + NULL,
- + NULL,
- + playerGroundCollisionSeparate,
- + NULL);
- +
- return space;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement