axmol/tests/cpp-tests/Source/ChipmunkTestBed/demo/Player.c

181 lines
5.9 KiB
C
Raw Normal View History

/* Copyright (c) 2007 Scott Lembcke
2021-06-30 09:52:54 +08:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
2021-06-30 09:52:54 +08:00
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
2021-06-30 09:52:54 +08:00
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
2021-06-30 09:52:54 +08:00
#include "chipmunk/chipmunk_private.h"
#include "ChipmunkDemo.h"
#define PLAYER_VELOCITY 500.0
#define PLAYER_GROUND_ACCEL_TIME 0.1
2021-12-28 16:06:23 +08:00
#define PLAYER_GROUND_ACCEL (PLAYER_VELOCITY / PLAYER_GROUND_ACCEL_TIME)
#define PLAYER_AIR_ACCEL_TIME 0.25
2021-12-28 16:06:23 +08:00
#define PLAYER_AIR_ACCEL (PLAYER_VELOCITY / PLAYER_AIR_ACCEL_TIME)
2021-12-28 16:06:23 +08:00
#define JUMP_HEIGHT 50.0
#define JUMP_BOOST_HEIGHT 55.0
2021-12-28 16:06:23 +08:00
#define FALL_VELOCITY 900.0
#define GRAVITY 2000.0
2021-12-28 16:06:23 +08:00
static cpBody* playerBody = NULL;
2021-06-30 09:52:54 +08:00
static cpShape* playerShape = NULL;
static cpFloat remainingBoost = 0;
2021-12-28 16:06:23 +08:00
static cpBool grounded = cpFalse;
static cpBool lastJumpState = cpFalse;
2021-06-30 09:52:54 +08:00
2021-12-28 16:06:23 +08:00
static void SelectPlayerGroundNormal(cpBody* body, cpArbiter* arb, cpVect* groundNormal)
{
cpVect n = cpvneg(cpArbiterGetNormal(arb));
2021-06-30 09:52:54 +08:00
2021-12-28 16:06:23 +08:00
if (n.y > groundNormal->y)
{
(*groundNormal) = n;
}
}
2021-12-28 16:06:23 +08:00
static void playerUpdateVelocity(cpBody* body, cpVect gravity, cpFloat damping, cpFloat dt)
{
int jumpState = (ChipmunkDemoKeyboard.y > 0.0f);
2021-06-30 09:52:54 +08:00
2021-12-28 16:06:23 +08:00
// Grab the grounding normal from last frame
cpVect groundNormal = cpvzero;
cpBodyEachArbiter(playerBody, (cpBodyArbiterIteratorFunc)SelectPlayerGroundNormal, &groundNormal);
2021-06-30 09:52:54 +08:00
2021-12-28 16:06:23 +08:00
grounded = (groundNormal.y > 0.0);
if (groundNormal.y < 0.0f)
remainingBoost = 0.0f;
2021-06-30 09:52:54 +08:00
2021-12-28 16:06:23 +08:00
// Do a normal-ish update
cpBool boost = (jumpState && remainingBoost > 0.0f);
cpVect g = (boost ? cpvzero : gravity);
cpBodyUpdateVelocity(body, g, damping, dt);
2021-06-30 09:52:54 +08:00
2021-12-28 16:06:23 +08:00
// Target horizontal speed for air/ground control
cpFloat target_vx = PLAYER_VELOCITY * ChipmunkDemoKeyboard.x;
2021-06-30 09:52:54 +08:00
2021-12-28 16:06:23 +08:00
// Update the surface velocity and friction
// Note that the "feet" move in the opposite direction of the player.
cpVect surface_v = cpv(-target_vx, 0.0);
playerShape->surfaceV = surface_v;
playerShape->u = (grounded ? PLAYER_GROUND_ACCEL / GRAVITY : 0.0);
2021-06-30 09:52:54 +08:00
2021-12-28 16:06:23 +08:00
// Apply air control if not grounded
if (!grounded)
{
// Smoothly accelerate the velocity
playerBody->v.x = cpflerpconst(playerBody->v.x, target_vx, PLAYER_AIR_ACCEL * dt);
}
2021-06-30 09:52:54 +08:00
2021-12-28 16:06:23 +08:00
body->v.y = cpfclamp(body->v.y, -FALL_VELOCITY, INFINITY);
}
2021-12-28 16:06:23 +08:00
static void update(cpSpace* space, double dt)
{
int jumpState = (ChipmunkDemoKeyboard.y > 0.0f);
2021-06-30 09:52:54 +08:00
2021-12-28 16:06:23 +08:00
// If the jump key was just pressed this frame, jump!
if (jumpState && !lastJumpState && grounded)
{
cpFloat jump_v = cpfsqrt(2.0 * JUMP_HEIGHT * GRAVITY);
playerBody->v = cpvadd(playerBody->v, cpv(0.0, jump_v));
2021-06-30 09:52:54 +08:00
2021-12-28 16:06:23 +08:00
remainingBoost = JUMP_BOOST_HEIGHT / jump_v;
}
2021-06-30 09:52:54 +08:00
2021-12-28 16:06:23 +08:00
// Step the space
cpSpaceStep(space, dt);
2021-06-30 09:52:54 +08:00
2021-12-28 16:06:23 +08:00
remainingBoost -= dt;
lastJumpState = jumpState;
}
2021-12-28 16:06:23 +08:00
static cpSpace* init(void)
{
cpSpace* space = cpSpaceNew();
space->iterations = 10;
space->gravity = cpv(0, -GRAVITY);
// space->sleepTimeThreshold = 1000;
cpBody *body, *staticBody = cpSpaceGetStaticBody(space);
cpShape* shape;
// Create segments around the edge of the screen.
shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320, -240), cpv(-320, 240), 0.0f));
shape->e = 1.0f;
shape->u = 1.0f;
cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320, -240), cpv(320, 240), 0.0f));
shape->e = 1.0f;
shape->u = 1.0f;
cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320, -240), cpv(320, -240), 0.0f));
shape->e = 1.0f;
shape->u = 1.0f;
cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320, 240), cpv(320, 240), 0.0f));
shape->e = 1.0f;
shape->u = 1.0f;
cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
// Set up the player
body = cpSpaceAddBody(space, cpBodyNew(1.0f, INFINITY));
body->p = cpv(0, -200);
body->velocity_func = playerUpdateVelocity;
playerBody = body;
shape = cpSpaceAddShape(space, cpBoxShapeNew2(body, cpBBNew(-15.0, -27.5, 15.0, 27.5), 1.0));
// shape = cpSpaceAddShape(space, cpSegmentShapeNew(playerBody, cpvzero, cpv(0, radius), radius));
shape->e = 0.0f;
shape->u = 0.0f;
shape->type = 1;
playerShape = shape;
// 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, 0.0));
shape->e = 0.0f;
shape->u = 0.7f;
}
}
return space;
}
2021-12-28 16:06:23 +08:00
static void destroy(cpSpace* space)
{
ChipmunkDemoFreeSpaceChildren(space);
cpSpaceFree(space);
}
ChipmunkDemo PlatformerPlayer = {
2021-12-28 16:06:23 +08:00
"Platformer Player Controls", 1.0 / 180.0, init, update, ChipmunkDemoDefaultDrawImpl, destroy,
};