Simple way to remove on collision
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Simple way to remove on collision
Posting some code snippets would make helping easier.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 9
- Joined: Sun Apr 04, 2010 7:49 pm
- Contact:
Re: Simple way to remove on collision
Sure
So currently I have it removing the body / shape of the items fine. What I simply want is when these objects collide with this item it adds +1 to the NSLabel :
The code above is currently what removes the shape / bodies of the shapes I want to remove. If it was a usual Objective-C method I'd simply go :
Within the second method to add 1 to the label. Unfortunately because its C++ I can't seem to get it to identify 'score_int' or 'score' which are the variable and NSLabel respectively that are obviously declared in the header.
Its probably something extreeeeemmely simple but I'm brand new to Objective-C and chipmunk, so its been a lot to get my head around. Once I have this I'm good to go though
So currently I have it removing the body / shape of the items fine. What I simply want is when these objects collide with this item it adds +1 to the NSLabel :
Code: Select all
void begin(cpArbiter *arb, cpSpace *space,void *ignore)
{
cpShape *ballShape, *bottomShape; cpArbiterGetShapes(arb, &bottomShape, &ballShape);
//struct PlayerStruct *playerInstance = (PlayerStruct *)a->data;
cpSpaceAddPostStepCallback(space, (cpPostStepFunc)postStepRemove, ballShape, NULL);
}
Code: Select all
static void
postStepRemove(cpSpace *space, cpShape *shape, void *unused)
{
cpSpaceRemoveBody(space, shape->body);
cpBodyFree(shape->body);
cpSpaceRemoveShape(space, shape);
cpShapeFree(shape);
}
The code above is currently what removes the shape / bodies of the shapes I want to remove. If it was a usual Objective-C method I'd simply go :
Code: Select all
score_int = score_int + 1;
score.text = [NSString stringWithFormat:@"%d",score_int];
Its probably something extreeeeemmely simple but I'm brand new to Objective-C and chipmunk, so its been a lot to get my head around. Once I have this I'm good to go though
-
- Posts: 90
- Joined: Tue Aug 04, 2009 9:53 am
- Contact:
Re: Simple way to remove on collision
Is the ignore param being used? If not why don't you just stuff your object in there? It should be the last param in your cpSpaceAddCollisionHandler call.
You're in Obj-C? or is it C++. Is score_int and score both properties? Actually you could just make a method that you could then call.... For instance:
You're in Obj-C? or is it C++. Is score_int and score both properties? Actually you could just make a method that you could then call.... For instance:
Code: Select all
void begin(cpArbiter *arb, cpSpace *space,void *ignore)
{
MyClass *instance = (MyClass*)ignore;
instance->incrementCounter(); //C++ way
[instance incrementCounter]; //Obj-C way
}
-
- Posts: 9
- Joined: Sun Apr 04, 2010 7:49 pm
- Contact:
Re: Simple way to remove on collision
Just to confirm, I can call an objective-C method using the above code?
I still seem to be getting undeclared errors when trying to do it is all. Despite the method being declared in the .h file.
Its an objective-C game (For the iphone) that calls these c++ methods for chipmunk to handle use the collision handler. All I simply want to really be able to do is call an Objective-C method from the collision handler, but can't seem to do it.
So for example classicly in Objective-C I would just go :
To call a method in general Objective C. However when I try that in C++ it comes up with a 'self undeclared' error.
Oh I forgot to mention :
Score is an NSLabel and score_int is an NSInteger currently
I still seem to be getting undeclared errors when trying to do it is all. Despite the method being declared in the .h file.
Its an objective-C game (For the iphone) that calls these c++ methods for chipmunk to handle use the collision handler. All I simply want to really be able to do is call an Objective-C method from the collision handler, but can't seem to do it.
So for example classicly in Objective-C I would just go :
Code: Select all
[self updateCounter];
Oh I forgot to mention :
Score is an NSLabel and score_int is an NSInteger currently
Last edited by Sloth on Mon Apr 12, 2010 8:07 am, edited 1 time in total.
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Simple way to remove on collision
Are you putting this code in a .c instead of a .m (Objective-C) file? The compiler will throw a bunch of odd errors if you try to put an Objective-C method call into a normal .c file. Are you sure that you are including the headers? To answer your question, you can call C++ methods from a Objective-C code if it's in a .mm (Objective-C++) file. Though you don't need to do this, I was just using C++-like syntax in the example. Stay far away from Objective-C++ if you can, it's a bit of a disaster.
Also, you are going to have to make some object to hold the score related pointers as you can only pass one pointer into the Chipmunk callbacks. This is part of what mobilebros was getting at. I usually make a score class with methods for adding different types of score data.
Also, you are going to have to make some object to hold the score related pointers as you can only pass one pointer into the Chipmunk callbacks. This is part of what mobilebros was getting at. I usually make a score class with methods for adding different types of score data.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 9
- Joined: Sun Apr 04, 2010 7:49 pm
- Contact:
Re: Simple way to remove on collision
I'm sure you're getting sick of me now, but I'm still stuck!
Firstly I can't seem to get the last deceleration to be anything other than null in this line :
If I try adding something like *data and it flags it as undeclared. I've tried declaring something in the header but whatever I try it just constantly says undeclared. (This line is written within an objective-C function)
In addition to that I still cannot call an objective-C function from a C++ one. If I try using the classic Objective-C way of doing things :
then it flags 'self' as undeclared (Obviously)
Likewise if I try the C++ way of doing things it just doesn't like it either.
A quick (Probably obvious) question is to mobile bros line of code : From my understanding you're assinging a specific class to a pointer so it can be called. However for example my objective-C 'class' was called 'updateCounter' I take i would go again however this brings up an undeclared error.
Am I just getting the complete wrong end of the stick here and you can't call Objective-C from C++ within a .m ?
Firstly I can't seem to get the last deceleration to be anything other than null in this line :
Code: Select all
cpSpaceAddCollisionHandler(space, 6, 5, begin, NULL, NULL, NULL, *data);
If I try adding something like *data and it flags it as undeclared. I've tried declaring something in the header but whatever I try it just constantly says undeclared. (This line is written within an objective-C function)
In addition to that I still cannot call an objective-C function from a C++ one. If I try using the classic Objective-C way of doing things :
Code: Select all
[self callFunction];
Likewise if I try the C++ way of doing things it just doesn't like it either.
A quick (Probably obvious) question is to mobile bros line of code :
Code: Select all
MyClass *instance = (MyClass*)ignore;
Code: Select all
updateCounter *update = (updateCounter*)ignore;
Am I just getting the complete wrong end of the stick here and you can't call Objective-C from C++ within a .m ?
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Simple way to remove on collision
I think what you are getting at is that you are having trouble calling an Objective-C method on self from within a C function. self is basically a magical variable that is passed to every Objective-C method. You can call methods in C functions, but you don't have a self variable. It sounds like you just need to learn more about how Objective-C and C work, and this forum probably isn't the best way to learn it. I'd recommend picking up some books or trying the idevgames.com forums.
Assuming you have Objective-C classes named World and GameObject, your code might look something like this:
Assuming you have Objective-C classes named World and GameObject, your code might look something like this:
Code: Select all
static void
postStepRemove(cpSpace *space, GameObject *gameObject, World *world)
{
// Remove whatever shapes and bodies you need here...
[world addToScore:12];
}
static int
begin(cpArbiter *arb, cpSpace *space, World *world)
{
CP_ARBITER_GET_SHAPES(arb, bulletShape, monsterShape);
// monsterShape.data should be set to a GameObject instance
// If you aren't assigning anything to data, just use monstershape instead
cpSpaceAddPostStepCallback(space, (cpPostStepFunc)postStepRemove, monsterShape.data, world);
return 0;
}
// Just an Objective-C object
World *world = [[World alloc] initWithStuff:stuff];
// World is already a pointer, no need to add & or * in front of it.
cpSpaceAddCollisionHandler(space, BULLET_TYPE, MONSTER_TYPE, (cpCollisionBeginFunc)begin, NULL, NULL, NULL, world);
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Who is online
Users browsing this forum: No registered users and 7 guests