Collison detection between two spaces

Official forum for the Chipmunk2D Physics Library.
pravin
Posts: 39
Joined: Thu Jun 25, 2009 7:20 am
Contact:

Collison detection between two spaces

Post by pravin »

Hi Everyone,

I have problem with collision detection in my iphone game: the collisions are between ship and enemy objects.
I think its a problem with setting spaces, bodies etc.
I am taken two different cpSpace instances.
In my code I am added ship object in one space and enemy object into other space
and setting collision_types 0 and 1 for respective object.

I didn't get to detect collision detection between two two objects of different spaces?
maximile
Posts: 157
Joined: Mon Aug 20, 2007 12:53 pm
Location: London, UK
Contact:

Re: Collison detection between two spaces

Post by maximile »

No, collisions between spaces don't work. Why do you need two spaces?
pravin
Posts: 39
Joined: Thu Jun 25, 2009 7:20 am
Contact:

Re: Collison detection between two spaces

Post by pravin »

Thanks for reply,

Here I explain my game structure :
I am developing game just like space war as there are many objects like ship , enemies of type different types.
In each level there are two enemy , ship and bullets.

The ship is an instance of class "MyShip" and the movement of ship is on accelerometer so
to update position of ship I added this object into space say space1 of "MyShip" class.
Bullets are instances of class "bullet". And enemies are instances of class say Enemy1 and Enemy2.

I created all these objects into "GamePlay" class and ship position is updated in "MyShip" class.
Here The Bullets and Enemy1 and Enemy2 are added into space say space2.

The condition is that bullets can collides with only Enemy1 and ship can collides with both enemies Enemy1 and Enemy2.

Now I can detect collision between Bullet and Enemy1.
But How can I detect collision between ship and Enemy1 , Enemy2.
maximile
Posts: 157
Joined: Mon Aug 20, 2007 12:53 pm
Location: London, UK
Contact:

Re: Collison detection between two spaces

Post by maximile »

You definitely don't need two spaces then. Have a look in the docs at collision layers. For anything more complicated you can use collision callbacks, but you could use layers in your example.
pravin
Posts: 39
Joined: Thu Jun 25, 2009 7:20 am
Contact:

Re: Collison detection between two spaces

Post by pravin »

You definitely don't need two spaces then. Have a look in the docs at collision layers. For anything more complicated you can use collision callbacks, but you could use layers in your example.

I am new to chipmunk and this is my first game in Landscape mode.
Suppose I am using only single space. Then I can know get collision detection between them but
I am not clear about fallowing few points :

1. How can I update the positions of ship (using accelerometer) , Bullets , Enemy1 & Enemy2.I mean How I can know the these objects in space?
2 .I am updating ship position in "MyShip" through accelerometer so do I need to implement accelerometer in "GamePlay" class i.e. Where I am initialising/adding all instances ?

Please have a look at sample code

1 . To update ship position through accelerometer


void updateShape(void* ptr, void* unused) {

cpShape* shape = (cpShape*)ptr; // Get our shape
Sprite* sprite = shape->data ;
if(sprite){
cpBody* body = shape->body;
shipCenterPoint.x = body->p.x ;
shipCenterPoint.y = body->p.y ;
[sprite setPosition:cpv(body->p.x, body->p.y)];
[sprite setRotation: CC_RADIANS_TO_DEGREES( -body->a )];
}
}

2. To Update the bullets positions


// To update bullet position
static void
bulletShape(void *ptr, void* unused)
{
cpShape *shape = (cpShape*) ptr;

Sprite *sprite = shape->data;
if( sprite) {
cpBody *body = shape->body;
[sprite setPosition: cpv( body->p.x, body->p.y)];
[sprite setRotation: (float) CC_RADIANS_TO_DEGREES( -body->a )];
}
}

As Ship is added into space say space1 and Bullets are added into space say space2.

if I add all instances in single space then I am worried about How can I know the these instances?
How to updated respective positions of these objects?

Please help me.
maximile
Posts: 157
Joined: Mon Aug 20, 2007 12:53 pm
Location: London, UK
Contact:

Re: Collison detection between two spaces

Post by maximile »

If you're asking how to find out what type of object you're looking at while iterating through a list of shapes, don't. You should keep track of all your game objects yourself and deal with them separately. Use Chipmunk just for the physics, not as a game engine.
pravin
Posts: 39
Joined: Thu Jun 25, 2009 7:20 am
Contact:

Re: Collison detection between two spaces

Post by pravin »

If you're asking how to find out what type of object you're looking at while iterating through a list of shapes, don't. You should keep track of all your game objects yourself and deal with them separately. Use Chipmunk just for the physics, not as a game engine.

Yes, I am updating the shapes body only. I am implementing Chipmunk just as the physics, not as a game engine.
As I am new to Chipmunk , I didn't get which parameter should I set to keep track of the all game objects?
maximile
Posts: 157
Joined: Mon Aug 20, 2007 12:53 pm
Location: London, UK
Contact:

Re: Collison detection between two spaces

Post by maximile »

You should keep track of them yourself, without using Chipmunk.

For example, I have an array of game objects. My game object class is responsible for drawing etc. Each game object that requires physics has a pointer to a cpBody, which is set when the object is added to the space. Then every frame I iterate through the objects and each one draws itself by checking the position and angle of its cpBody.
User avatar
Tam Toucan
Posts: 141
Joined: Tue Jun 23, 2009 4:26 pm
Contact:

Re: Collison detection between two spaces

Post by Tam Toucan »

As Ship is added into space say space1 and Bullets are added into space say space2.

if I add all instances in single space then I am worried about How can I know the these instances?
How to updated respective positions of these objects?
You have some misunderstanding about spaces and objects, but I'm not quite sure what it is. Think of a space as the area where all your physics takes place. There is no interaction between spaces i.e. your ship is in space1 and bullets in space2 then they can't collide. So typically you would only have 1 space in a game which would correspond to the "World" where your game takes place.

Chipmunk handles all the physics that happens in that world, you then need to draw the world, apply forces to the objects so they move etc. So you need to have some kind of relationship between the Chipmunk objects (Bodies, Shapes etc) and your game objects. This could be a simple 1 to 1 relationship that each cpShape corrsponds exactly to a "sprite" in the game e.g. look at the moon buggy tutorial. Or the relationship could be more complex where your player/sprite/ship is made up of several shapes and bodies.
I didn't get which parameter should I set to keep track of the all game objects?
Again, there is some misunderstanding that I can't quite work out what it is. Say your ship is just a single circle and your bullets are short lines. You could have something like (pseudo-code)

Code: Select all

struct MyObject {
   int my_object_type;
   cpShape* the_shape_ptr;
};

struct Ship {
MyObject header;
int health;
int ammo;
};
struct Bullet {
MyObject header;
int time_before_disappear;
};
...init player...
Ship ship;
ship.header.type = PLAYER_SHIP;
ship.header.the_shape_ptr = cpCircleShapeNew(body, radius, cpvzero);
ship.header.the_shape_ptr->data = (void*) &ship.header;
...create a bullet...
Bullet* bullet_ptr
...
bullet_ptr->header.type = PLAYER_BULLET;
bullet_ptr->header.the_shape_ptr = cpSegmentShapeNew(body, cpv(x1,y1), cpv(x2,y2), 1.0f);
bullter_ptr->header.the_shape_ptr->data = &bullet_ptr->header;
[/quote]
In your game engine you can use ship.theShape to apply forces to your ship to move it. In the physics (e.g. during a collision) you can cast the data parameter back to a MyObject pointer to get the header.type and then cast back to a Ship. Or there loads of other ways e.g. you could use the collision_type field to mark the cpShape as a PLAYER_SHIP etc.

Hopefully this helps, if not then I need more explanation as to where your confusion is.
pravin
Posts: 39
Joined: Thu Jun 25, 2009 7:20 am
Contact:

Re: Collison detection between two spaces

Post by pravin »

Thanks for reply Tam.

Yes , I am little bit confuse about chipmunk space , body and Hashes.

I am defining the structure of my game object as you discussed here.
I am assigning data as you explain i.e. ship.header.the_shape_ptr->data = (void*) &ship.header;

let suppose in call back function I am updating my object positions and movements etc.
In call back I am getting shapes like

void updateShape(void* ptr, void* unused)
{
cpShape* shape = (cpShape*)ptr; // Get our shape
Sprite* sprite = shape->data ;
}

Now the question is
How I can extract this wrapped data to find out the "my_object_type" in my call back function.?
Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests