Static object of infinite mass still falls?

Official forum for the Chipmunk2D Physics Library.
Post Reply
ellisk
Posts: 1
Joined: Sat Aug 29, 2009 3:13 pm
Contact:

Static object of infinite mass still falls?

Post by ellisk »

Hello,

I am trying to learn Chipmunk. I have a circular object which falls (under gravity) until it hits the ground. Unfortunately, this never occurs as the ground is falling, too! I am not using graphics, just printf's. Could someone point out where I go wrong?

Code: Select all

#include "chipmunk.h"
#include <stdio.h>

#define STEPS 150
#define TDELTA 0.1

#define GRAVITY -9.8

int main(int argc, char **argv)
{
  cpInitChipmunk();
  
  cpSpace *space = cpSpaceNew();
  space->gravity = cpv(0.0, GRAVITY);

  cpBody *body = cpBodyNew(2, INFINITY);
  body->p = cpv(0,0);
  cpSpaceAddBody(space,body);
  
  cpShape *shape = cpCircleShapeNew(body, 3, cpvzero);
  shape->e = 0.5;
  shape->u = 0.8;
  shape->data = 0;
  shape->collision_type = 1;
  cpSpaceAddShape(space,shape);

  cpBody *botB = cpBodyNew(INFINITY,INFINITY);
  botB->p = cpv(0,-50);
  cpSpaceAddBody(space,botB);
  cpShape *botS = cpSegmentShapeNew(botB,cpv(-50,0),cpv(50,0),1);
  botS->e = 0.5; botS->u = 0.1; botS->collision_type = 0;
  cpSpaceAddStaticShape(space,botS);

  for (int i = 0; i < STEPS; i++) {
    cpSpaceStep(space, TDELTA);
    printf("(%g,%g)\t(%g,%g)\n",
	   body->p.x, body->p.y,
	   botB->p.x, botB->p.y);
  }
}
The right coordinates are of the floor, the left ones are of the ball. As you can see, both are falling over time (this does not happen when I eliminate gravity).

I get the following warnings:

Code: Select all

main.c: In function ‘main’:
main.c:16: warning: floating constant exceeds range of ‘double’
main.c:27: warning: floating constant exceeds range of ‘double’
main.c:27: warning: floating constant exceeds range of ‘double’
It seems that GCC is not liking INFINITY, which occurs on lines 16 and 27 (twice on 27).

Could someone please point out my error? I have a suspicion it involves my use of INFINITY. If it at all matters, I am using a 64 bit machine.

Thank you,
Kevin
maximile
Posts: 157
Joined: Mon Aug 20, 2007 12:53 pm
Location: London, UK
Contact:

Re: Static object of infinite mass still falls?

Post by maximile »

Not sure about the infinity stuff, but don't add the static body to the space - just the shape. Any body added to the space will be affected by gravity.
Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests