Hello,
I am trying to make the ball fall down the screen and bounce once it hits the ground. However the ball does not move at all.
#include "raylib.h"
#include "chipmunk/chipmunk.h"
const int WIDTH = 1024;
const int HEIGHT = 720;
cpSpace* space;
cpBody* groundBody;
cpShape* groundShape;
Vector2 groundSize = { 1000,10 };
cpVect groundPos;
cpBody* ballBody;
cpShape* ballShape;
float ballRad = 10.0f; //Radius of ball
cpVect ballPos;
bool Paused = false;
int main()
{
InitWindow(WIDTH, HEIGHT, "Raylib & Chipmunk Physics");
SetTargetFPS(60);
//Setup chipmunk physics world
cpVect gravity = cpv(0, -100);
space = cpSpaceNew();
cpSpaceGetGravity(space);
//setup and add ground body to world, ground is static (doesn't move)
groundBody = cpSpaceAddBody(space, cpBodyNew(0, 0));
groundShape = cpSpaceAddShape(space, cpBoxShapeNew(groundBody, groundSize.x, groundSize.y, 0));
cpShapeSetFriction(groundShape, 0.1f);
cpShapeSetMass(groundShape, 8);
//set position of ground and get ground position
groundPos.x = 1;
groundPos.y = 700;
cpBodySetPosition(groundBody, groundPos);
groundPos = cpBodyGetPosition(groundBody);
ballBody = cpSpaceAddBody(space, cpBodyNew(0, 0));
ballShape = cpSpaceAddShape(space, cpCircleShapeNew(ballBody, ballRad, ballPos));
cpShapeSetFriction(ballShape, 0.5f);
cpShapeSetMass(ballShape, 8);
cpShapeSetElasticity(ballShape, 0.5f);
ballPos.x = 10;
ballPos.y = 10;
cpBodySetPosition(ballBody, ballPos);
cpMomentForCircle(1, 0, ballRad, cpvzero);
while (!WindowShouldClose())
{
if (IsKeyPressed(KEY_P)) Paused = !Paused;
cpSpaceStep(space, 1.0 / 60.0f); //step the simulation
cpVect pos = cpBodyGetPosition(ballBody);
cpVect vel = cpBodyGetVelocity(ballBody);
BeginDrawing();
DrawRectangle(groundPos.x, groundPos.y, groundSize.x, groundSize.y, BROWN);
DrawCircle(ballPos.x, ballPos.y, ballRad, RED);
EndDrawing();
}
cpShapeFree(ballShape);
cpBodyFree(ballBody);
cpShapeFree(groundShape);
cpBodyFree(groundBody);
cpSpaceFree(space);
CloseWindow();
return 0;
}