just started trying to learn, and now im stuck

Official forum for the Chipmunk2D Physics Library.
Post Reply
nobody
Posts: 3
Joined: Thu Jan 06, 2011 2:48 am
Contact:

just started trying to learn, and now im stuck

Post by nobody »

Hello I've just started trying to learn how to use chipmunk.
I've been looking at the demos to try and learn but I've gotten stuck

all I'm trying to do is have a square start at the top of my screen and fall to the bottom.
But its not really working out so great.

I make the space then i make the floor as segment and the square as a poly (of course)
but all that happens is the square stays where it is

the main

Code: Select all

int main(){
	
	startsdl();
	startgl();
	cpInitChipmunk();
	
	cpSpace *space = cpSpaceNew();
	cpSpaceInit(space);
	space->iterations = 10;
	//cpSpaceResizeStaticHash(space, 30.0f, 1000);
	//cpSpaceResizeActiveHash(space, 30.0f, 1000);
	space->gravity = cpv(0, -25);
	
	
	cpShape *ground = cpSegmentShapeNew(&space->staticBody, cpv(0,25), cpv(1377,25), 5);
	ground->e = 1; ground->u = 1;
	//cpSpaceAddShape(space, ground);
	//ground->layers = NOT_GRABABLE_MASK;
	
	
	cpVect verts[] = {
		cpv(0 , 50),
		cpv(50, 50),
		cpv(50, 0),
		cpv(0, 0),
	};
	
	cpBody *body = cpSpaceAddBody(space, cpBodyNew(1, /*cpMomentForPoly(1, 4, verts, cpv(675,525))*/ INFINITY));
	//body->p = cpv(675, 525);
	body->v= cpv(0, -5);
		
	cpShape *shape = cpSpaceAddShape(space, cpPolyShapeNew(body, 4, verts, cpv(675,525)));
	shape->e = 0.0f; shape->u = 0.7f; shape->collision_type = 1;
				
		
	while(1){
		glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
		sleep(1/60);
				
		cpSpaceStep(space, 1.0f/60);
		
		drawrect(shape, NULL);
				
		SDL_GL_SwapBuffers();
		
}

	cpSpaceFreeChildren(space);
	cpSpaceFree(space);
	SDL_Quit();
	
	return 0;
	
}



what i use to draw

Code: Select all

void drawrect (void *ptr, void* unused){
	
		cpShape *s = (cpShape*)ptr;
		cpVect verts[4];
		
		
		for(int i = 0; i < 4; i++){
			verts[i] =  cpPolyShapeGetVert(s, i);
			printf("(%f, %f)\n", verts[i].x, verts[i].y);
		}
			
		
		
		glBegin(GL_QUADS);
	
			glVertex2i(verts[0].x, verts[0].y);			//top left
			glVertex2i(verts[1].x, verts[1].y);			//top right
			glVertex2i(verts[2].x, verts[2].y);		//bottom right
			glVertex2i(verts[3].x, verts[3].y);		// bottom left
			
			
		glEnd();
		
		printf("\n");
		
}
the comments are things I've all ready tried.

i guess i really don't know what I'm doing that much.
I just what to get this test to work first

any help would be appreciated.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: just started trying to learn, and now im stuck

Post by slembcke »

cpPolyShapeGetVert() gives you the vertexes relative to the body, the same vertexes that you gave to cpPolyShapeNew(). You can use cpBodyLocal2World() to convert them to absolute positions.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
nobody
Posts: 3
Joined: Thu Jan 06, 2011 2:48 am
Contact:

Re: just started trying to learn, and now im stuck

Post by nobody »

That worked perfectly. Thanks

So let me see if i understand how this works.
the vertexes given to cpPolyShapeNew are static but are offset by p in the body that it belongs to?

if thats true how is p set if I don't set it myself?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: just started trying to learn, and now im stuck

Post by slembcke »

The rigid body structs hold information about where an object is, how fast it's moving and how much it weighs. Each time you call cpSpaceStep() Chipmunk updates the positions and velocities of all of the rigid bodies that have been added to it.

Rigid bodies don't have a shape and don't collide with things until you attach shapes to them, and the shapes are always defined relative to the position and rotation of the rigid body they are attached to. You can attach as many shapes as you want or none at all if you just need a body for attaching joints to.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
nobody
Posts: 3
Joined: Thu Jan 06, 2011 2:48 am
Contact:

Re: just started trying to learn, and now im stuck

Post by nobody »

OK thanks
I figured some of this stuff out messing around with it
last night

I really appreciate the help
Post Reply

Who is online

Users browsing this forum: No registered users and 40 guests