Sign in to follow this  
LeftyGuitar

Chipmunk Physics Ball Won't Bounce

Recommended Posts

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();
    }
}
Edited by LeftyGuitar

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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);
}

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this