Help determining if a shape is multi-shaped or not.

Official forum for the Chipmunk2D Physics Library.
Post Reply
progressxoverdue
Posts: 22
Joined: Sat May 09, 2009 8:54 pm
Contact:

Help determining if a shape is multi-shaped or not.

Post by progressxoverdue »

Hello all.

I would like to have shapes which will disappear when clicked. The shapes are stored in Objective-C objects and are created with member functions like so:

Code: Select all

#import "SCross.h"

@implementation SCross

- (id)init{
	UIImageView *temp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SCross.png"]];
	temp.center = CGPointMake(-1000, -1000);
	return temp;
}

+(cpBody *)setupBody{
	cpVect verts[]={cpv(-30,30), cpv(30, 30), cpv(30, -30), cpv(-30,-30)};
	cpFloat block_moment = cpMomentForPoly(kSCrossMass, 4, verts, cpvzero);
	return cpBodyNew(kSCrossMass, block_moment);
}
+(cpShape *)setupShape:(cpBody *)body inSpace:(cpSpace *) space{
	cpVect verts1[]={cpv(-6,30), cpv(6, 30), cpv(6, -30), cpv(-6, -30)};
	cpVect verts2[]={cpv(-30, 6), cpv(30, 6), cpv(30, -6), cpv(-30, -6)};
	
	cpShape *shape = cpPolyShapeNew(body, 4, verts1, cpv(0,0));
	shape->e = kSCrossElasticity;
	shape->u = kSCrossFriction;
	shape->collision_type = 0;
	//return shape;  
	
	cpSpaceAddShape(space, shape);  
	shape = cpPolyShapeNew(body, 4, verts2, cpv(0,0));  
	shape->e = kSCrossElasticity; 
	shape->u = kSCrossFriction; 
	shape->collision_type = 0; 
	
	return shape;
}

The problem is, the shape created by this is not an Objective-C object, it is a 'cpsShape' so I can't have a member function in my Objective-C object that will remove the shape and all of its 'sub-shapes'. I can't think of how to ask the cpShape if it has multiple shapes associated with it that need to be deleted ( and if so how many).

Any idea's on how I could go about this would be greatly appreciated!
User avatar
Tam Toucan
Posts: 141
Joined: Tue Jun 23, 2009 4:26 pm
Contact:

Re: Help determining if a shape is multi-shaped or not.

Post by Tam Toucan »

You use the data member of shapes and bodies to store a pointer to refer back to your own objects. In your own object store pointer(s) to all it's bodies and shapes. Make sense?
progressxoverdue
Posts: 22
Joined: Sat May 09, 2009 8:54 pm
Contact:

Re: Help determining if a shape is multi-shaped or not.

Post by progressxoverdue »

I'm not sure I've completely wrapped my head around it. I'll give it a try and report back.


Thanks for the help!
progressxoverdue
Posts: 22
Joined: Sat May 09, 2009 8:54 pm
Contact:

Re: Help determining if a shape is multi-shaped or not.

Post by progressxoverdue »

Ok, in principle I understand what you're saying, but in practice I can't figure out how to get it to work. In the code example above I've already lost track of one of my shapes by only returning the second shape (I realized that after I'd already posted it). So I'd need to store each shape in an array of pointers to shapes in my body's data member (and not return anything). But when I tried that I had all sorts of problems trying to use that array later because it's of type (void*).
User avatar
Tam Toucan
Posts: 141
Joined: Tue Jun 23, 2009 4:26 pm
Contact:

Re: Help determining if a shape is multi-shaped or not.

Post by Tam Toucan »

The easiest way to do it is to have your own classs (say MyObject) which represents items in your world. That class will have a pointer to the cpBody for the object (or pointers if you can have multiple bodies for a single game object) and pointers to the cpShapes for that body (or bodies). The data members of the cpBody and cpShapes will have the MyObject* that they belong to so that you have a two way connection.

You must surely have something like MyObject already so it should be a minor change. Hopefully that's more clear.
Post Reply

Who is online

Users browsing this forum: Heise IT-Markt [Crawler] and 5 guests