Chipmunk Physics Ball Won't Bounce

Started by
3 comments, last by LeftyGuitar 7 years, 1 month ago

Hello,

I cannot get the ball to bounce off the ground in this simulation. I am using SFML to render the graphics and Chipmunk Physics for the physics. When the physics simulation is ran, all it does is the ball drops to the ground without bouncing off of the ground.


const int MAX_WIDTH = 800;
const int MAX_HEIGHT = 600;
const int MAX_DEPTH = 32;

bool GameRunning = true;
sf::RenderWindow GameWin;
sf::Event event;

sf::RectangleShape ground;
sf::CircleShape ball;

bool Paused = true;

cpVect gravity;
cpSpace* space;

cpShape* groundShape;
cpBody* groundBody;

cpFloat radius = 10;
cpFloat mass = 1;

cpFloat moment;

cpBody* ballBody;
cpShape* ballShape;

cpFloat timeStep;

GameWin.create(sf::VideoMode(MAX_WIDTH, MAX_HEIGHT, MAX_DEPTH), "Physics Test - [Chipmunk Physics & SFML]");

	ground.setPosition(sf::Vector2f(0, 540));
	ground.setSize(sf::Vector2f(800, 560));
	ground.setFillColor(sf::Color(128, 128, 128));

	ball.setPosition(sf::Vector2f(0, 15));
	ball.setRadius(radius);
	ball.setFillColor(sf::Color(255, 0, 0));

	gravity = cpv(0, 50);

	space = cpSpaceNew();
	cpSpaceSetGravity(space, gravity);

	groundShape = cpSegmentShapeNew(space->staticBody, cpv(-ground.getPosition().x, ground.getPosition().y), cpv(-ground.getPosition().x, ground.getPosition().y), 0);
	cpShapeSetFriction(groundShape, 1);
	cpSpaceAddShape(space, groundShape);

	groundBody = cpBodyNew(0, 560);

	cpVect groundPos = cpBodyGetPosition(groundBody);

	cpBodySetPosition(groundBody,groundPos);

	groundBody->userData = &ground;

	cpFloat moment = cpMomentForCircle(mass, 1, radius, cpvzero);

	ballBody = cpSpaceAddBody(space, cpBodyNew(mass, moment));
	cpBodySetPosition(ballBody, cpv(ball.getPosition().x, ball.getPosition().y));
	ballShape = cpSpaceAddShape(space, cpCircleShapeNew(ballBody, radius, cpvzero));
	cpShapeSetFriction(ballShape, 1.0);
	cpShapeSetElasticity(ballShape, 0.5);

	ballBody->userData = &ball;

	timeStep = 1.0 / 600.0f;

while (GameRunning)
	{
		while (GameWin.pollEvent(event))
		{
			if (event.type == sf::Event::Closed)
			{
				GameWin.close();
				GameRunning = false;
			}

			if (event.type == sf::Event::KeyPressed)
			{
				if (event.key.code == sf::Keyboard::Escape)
				{
					GameRunning = false;
					GameWin.close();
				}

				if (event.key.code == sf::Keyboard::P && Paused == true)
				{
					Paused = false;
				}
				else if (event.key.code == sf::Keyboard::P && Paused == false)
				{
					Paused = true;
				}
			}
		}

		if (!Paused)
		{
			cpVect pos = cpBodyGetPosition(ballBody);
			cpVect vel = cpBodyGetVelocity(ballBody);
			cpFloat angle = cpBodyGetAngle(ballBody);

			ball.setPosition(pos.x, pos.y);
			ball.setRotation(angle);

			cpSpaceStep(space, timeStep);
	
		}
        DrawGame();
    }
}
Advertisement
I cannot get the ball to bounce off the ground in this simulation. I am using SFML to render the graphics and Chipmunk Physics for the physics

That's very interesting. Or frustrating.

Do you have a question? It ALMOST looks like you are asking for help with something specific, but you forgot to include a list of what you already tried, why that didn't work, and an actual question.

I'm trying to make it so that the ball will bounce when it hits the ground. It doesen't bounce when it hits the ground, it just stays there, I'm trying to figure out why the ball isn't interacting with the ground? I have posted the full source code this time.


#include <iostream>

#include <SFML\System.hpp>
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>

#include <chipmunk\chipmunk.h>
#include <chipmunk\chipmunk_private.h>

using namespace std;

void Init();
void GameLoop();
void DrawGame();
void Shutdown();

const int MAX_WIDTH = 800;
const int MAX_HEIGHT = 600;
const int MAX_DEPTH = 32;

bool GameRunning = true;
sf::RenderWindow GameWin;
sf::Event event;

sf::RectangleShape ground;
sf::CircleShape ball;

bool Paused = true;

cpVect gravity;
cpSpace* space;

cpShape* groundShape;
cpBody* groundBody;

cpFloat radius = 10;
cpFloat mass = 1;

cpFloat moment;

cpBody* ballBody;
cpShape* ballShape;

cpFloat timeStep;

int main(int argc, char* argv[])
{
	Init();

	GameLoop();

	Shutdown();

	return 0;
}

void Init()
{
	GameWin.create(sf::VideoMode(MAX_WIDTH, MAX_HEIGHT, MAX_DEPTH), "Physics Test - [Chipmunk Physics & SFML]");

	ground.setPosition(sf::Vector2f(0, 540));
	ground.setSize(sf::Vector2f(800, 560));
	ground.setFillColor(sf::Color(128, 128, 128));

	ball.setPosition(sf::Vector2f(0, 15));
	ball.setRadius(radius);
	ball.setFillColor(sf::Color(255, 0, 0));

	gravity = cpv(0, 50);

	space = cpSpaceNew();
	cpSpaceSetGravity(space, gravity);

	groundShape = cpSegmentShapeNew(space->staticBody, cpv(-ground.getPosition().x, ground.getPosition().y), cpv(-ground.getPosition().x, ground.getPosition().y), 0);
	cpShapeSetFriction(groundShape, 1);
	cpSpaceAddShape(space, groundShape);

	groundBody = cpBodyNew(0, 560);

	cpVect groundPos = cpBodyGetPosition(groundBody);

	cpBodySetPosition(groundBody,groundPos);

	groundBody->userData = &ground;

	cpFloat moment = cpMomentForCircle(mass, 1, radius, cpvzero);

	ballBody = cpSpaceAddBody(space, cpBodyNew(mass, moment));
	cpBodySetPosition(ballBody, cpv(ball.getPosition().x, ball.getPosition().y));
	ballShape = cpSpaceAddShape(space, cpCircleShapeNew(ballBody, radius, cpvzero));
	cpShapeSetFriction(ballShape, 1.0);
	cpShapeSetElasticity(ballShape, 0.5);

	ballBody->userData = &ball;

	timeStep = 1.0 / 600.0f;
}

void GameLoop()
{
	while (GameRunning)
	{
		while (GameWin.pollEvent(event))
		{
			if (event.type == sf::Event::Closed)
			{
				GameWin.close();
				GameRunning = false;
			}

			if (event.type == sf::Event::KeyPressed)
			{
				if (event.key.code == sf::Keyboard::Escape)
				{
					GameRunning = false;
					GameWin.close();
				}

				if (event.key.code == sf::Keyboard::P && Paused == true)
				{
					Paused = false;
				}
				else if (event.key.code == sf::Keyboard::P && Paused == false)
				{
					Paused = true;
				}
			}
		}

		if (!Paused)
		{
			cpVect pos = cpBodyGetPosition(ballBody);
			cpVect vel = cpBodyGetVelocity(ballBody);
			cpFloat angle = cpBodyGetAngle(ballBody);

			ball.setPosition(pos.x, pos.y);
			ball.setRotation(angle);

			cpSpaceStep(space, timeStep);
	
		}

		DrawGame();
	}
}

void DrawGame()
{
	GameWin.clear();

	GameWin.draw(ball);

	GameWin.draw(ground);

	GameWin.display();
}

void Shutdown()
{
	cpShapeFree(ballShape);
	cpShapeFree(groundShape);

	cpBodyFree(groundBody);
	cpBodyFree(ballBody);
}

This is a great question for the chipmunk physics forum as it's specific to that system and you'll be much more likely to get an educated response than asking in a general game programming forum.

Those guys are nice over there, good luck!

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

I just solved this problem. I apparently had to add elasticity to the ground shape in order for the ball shape to bounce off of it.


cpShapeSetElasticity(groundShape, 0.8);

Adding this line of code solved my problem.

This topic is closed to new replies.

Advertisement