我正在尝试在 Chipmunk2d 中测试两个碰撞对象(玩家矩形和地面),但玩家矩形只是从地面掉落。我在库中找不到太多文档,而且对于物理学,我真的不需要库,而纯粹是为了碰撞。 本质上我想要的是与玩家发生碰撞并获得回调触发器的地面碰撞。
A:我不确定如何测试碰撞
B:我不确定如何注册回调
C:我不相信玩家矩形应该从地板上掉下来
#include <stdio.h>
#include <stdint.h>
#include <chipmunk/chipmunk.h>
int main(void)
{
cpVect gravity = cpv(0, 100);
cpSpace *space = cpSpaceNew();
cpSpaceSetGravity(space, gravity);
// Create player body and shape
cpFloat playerMass = 10.0f;
cpFloat playerWidth = 50.0f;
cpFloat playerHeight = 50.0f;
cpFloat playerMoment = cpMomentForBox(playerMass, playerWidth, playerHeight);
cpBody *playerBody = cpSpaceAddBody(space, cpBodyNew(playerMass, playerMoment));
cpVect playerPos = cpv(100, 100);
cpBodySetPosition(playerBody, playerPos);
cpShape *playerShape = cpSpaceAddShape(space, cpBoxShapeNew(playerBody, playerWidth, playerHeight, 0));
cpShapeSetCollisionType(playerShape, PLAYER_COLLISION_TYPE);
cpShapeSetFilter(playerShape, cpShapeFilterNew(PLAYER_COLLISION_TYPE, GROUND_COLLISION_TYPE, CP_NO_GROUP));
// Create ground body and shape
cpFloat groundWidth = 600.0f;
cpFloat groundHeight = 10.0f;
cpBody *groundBody = cpSpaceAddBody(space, cpBodyNewStatic());
cpVect groundPos = cpv(0 ,600);
cpBodySetPosition(groundBody, groundPos);
cpShape *groundShape = cpSpaceAddShape(space, cpBoxShapeNew(groundBody, groundWidth, groundHeight, 0));
cpShapeSetCollisionType(groundShape, GROUND_COLLISION_TYPE);
cpShapeSetFilter(groundShape, cpShapeFilterNew(GROUND_COLLISION_TYPE, PLAYER_COLLISION_TYPE, CP_NO_GROUP));
for(int n = 0; n < 200; ++n)
{
cpSpaceStep(space, 1/60.0f);
cpVect pos = cpBodyGetPosition(playerBody);
cpVect ground = cpBodyGetPosition(groundBody);
fprintf(stderr,"Player Y :%f\tGround Y :%f\n",pos.y,ground.y);
}
return 0;
}
在模拟中,玩家从地板上掉下来
Player Y :398.083364 Ground Y :600.000000
Player Y :402.166698 Ground Y :600.000000
Player Y :406.277810 Ground Y :600.000000
Player Y :410.416699 Ground Y :600.000000
Player Y :414.583366 Ground Y :600.000000
Player Y :418.777811 Ground Y :600.000000
Player Y :423.000034 Ground Y :600.000000
Player Y :427.250034 Ground Y :600.000000
Player Y :431.527812 Ground Y :600.000000
Player Y :435.833368 Ground Y :600.000000
Player Y :440.166702 Ground Y :600.000000
Player Y :444.527814 Ground Y :600.000000
Player Y :448.916703 Ground Y :600.000000
Player Y :453.333370 Ground Y :600.000000
Player Y :457.777815 Ground Y :600.000000
Player Y :462.250038 Ground Y :600.000000
Player Y :466.750038 Ground Y :600.000000
Player Y :471.277817 Ground Y :600.000000
Player Y :475.833373 Ground Y :600.000000
Player Y :480.416706 Ground Y :600.000000
Player Y :485.027818 Ground Y :600.000000
Player Y :489.666707 Ground Y :600.000000
Player Y :494.333374 Ground Y :600.000000
Player Y :499.027819 Ground Y :600.000000
Player Y :503.750042 Ground Y :600.000000
Player Y :508.500043 Ground Y :600.000000
Player Y :513.277821 Ground Y :600.000000
Player Y :518.083377 Ground Y :600.000000
Player Y :522.916711 Ground Y :600.000000
Player Y :527.777822 Ground Y :600.000000
Player Y :532.666712 Ground Y :600.000000
Player Y :537.583379 Ground Y :600.000000
Player Y :542.527824 Ground Y :600.000000
Player Y :547.500047 Ground Y :600.000000
Player Y :552.500047 Ground Y :600.000000
Player Y :557.527826 Ground Y :600.000000
Player Y :562.583382 Ground Y :600.000000
Player Y :567.666715 Ground Y :600.000000
Player Y :572.777827 Ground Y :600.000000
Player Y :577.916717 Ground Y :600.000000
Player Y :583.083384 Ground Y :600.000000
Player Y :588.277829 Ground Y :600.000000
Player Y :593.500051 Ground Y :600.000000
Player Y :598.750052 Ground Y :600.000000
Player Y :604.027830 Ground Y :600.000000
Player Y :609.333386 Ground Y :600.000000
Player Y :614.666720 Ground Y :600.000000
Player Y :620.027832 Ground Y :600.000000
Player Y :625.416721 Ground Y :600.000000
Player Y :630.833389 Ground Y :600.000000
Player Y :636.277834 Ground Y :600.000000
Player Y :641.750057 Ground Y :600.000000
Player Y :647.250057 Ground Y :600.000000
Player Y :652.777835 Ground Y :600.000000