[SOLVED] C - dereferencing pointer " -> "

Official forum for the Chipmunk2D Physics Library.
Post Reply
luckybc
Posts: 3
Joined: Wed Nov 18, 2015 11:47 am
Contact:

[SOLVED] C - dereferencing pointer " -> "

Post by luckybc »

Hello everyone!

I'm trying to build a simple chipmunk project in Linux 32 bit, ArchLinux. And I have a problem with dereferencing pointer in the C Language.

The following code :

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <chipmunk/chipmunk.h>
#include <chipmunk/chipmunk_unsafe.h>

#define DENSITY (1.0/10000.0)
#define GRABBABLE_MASK_BIT (1<<31)

cpShapeFilter GRAB_FILTER = {CP_NO_GROUP, GRABBABLE_MASK_BIT, GRABBABLE_MASK_BIT};

cpShapeFilter NOT_GRABBABLE_FILTER = {CP_NO_GROUP, ~GRABBABLE_MASK_BIT, ~GRABBABLE_MASK_BIT};

static cpShape *shape;
int main ()
{
	//ChipmunkDemoMessageString="Hello Chipmunk";
	
	cpSpace *space = cpSpaceNew();
	cpSpaceSetIterations(space, 30);
	cpSpaceSetGravity(space, cpv(0, -500));
	cpSpaceSetSleepTimeThreshold(space, 0.5f);
	cpSpaceSetCollisionSlop(space, 0.5f);

	cpBody *body, *staticBody = cpSpaceGetStaticBody(space);
	
	// Create segment around the edge of the screen.
	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320, -240), cpv(320, -240), 0.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

	cpFloat width = 50.0f;
	cpFloat height = 70.0f;
	cpFloat mass = width*height*DENSITY;
	cpFloat moment = cpMomentForBox(mass, width, height);
	body = cpSpaceAddBody(space, cpBodyNew(mass, moment));

	shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height, 0.0));

	cpShapeSetFriction(shape, 0.6f);
	
	for(float time =0.0; time < 3.0; time += 1.0f/60.0f)
	{
		cpSpaceStep(space, time);
		cpVect mV = body->p;	// Error | dereferencing pointer to incomplete type ‘cpBody {aka struct cpBody}’
		printf("body position %f:x %f:y\n", mV.x, mV.y); 
	}
	
	//Destroy The Space
	cpSpaceFree(space);

	return 0;
}
Last edited by luckybc on Wed Nov 18, 2015 11:27 pm, edited 1 time in total.
luckybc
Posts: 3
Joined: Wed Nov 18, 2015 11:47 am
Contact:

Re: C - dereferencing pointer " -> "

Post by luckybc »

OK, I've done solving the problem only just do:

#include <chipmunk/chipmunk_private.h>

Because the header includes:

Code: Select all

#include <chipmunk/chipmunk_structs.h> //struct cpBody is in there


Now I can use the dereferencing pointer "->".
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: [SOLVED] C - dereferencing pointer " -> "

Post by slembcke »

In general you are supposed to use the getter/setter functions to access the structs. Almost all values that you can change are at least supposed to wake a body up, and sometimes more.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
luckybc
Posts: 3
Joined: Wed Nov 18, 2015 11:47 am
Contact:

Re: [SOLVED] C - dereferencing pointer " -> "

Post by luckybc »

Yes, I know that. Because I did the same into https://chipmunk-physics.net/release/Ch ... test-Docs/ and I see I couldn't use the dereferencing pointer "->" to do. When I check the demos in the source I saw the getters/setters (e.g cpBodyGetPosition ...). Then I surprised the above problem. Maybe I love C :lol: !!!
Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests