• Advertisement
Sign in to follow this  

Cocos2d Tilemap Collision

This topic is 2402 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

I'm new to game development and I am working on a project using Cocos2d. In my game I have a sprite that moves around freely on top of a tilemap. Currently I can check if specific points on the sprite has collided with a tile having the 'collidable' property. Where I have made a

CGPoint player = ccp(player.position.x, player.position.y);

and check if this point has collided with the tile. (i don't have the code which i used for this right now as I'm at work, i will paste this later)

What I am trying to accomplish however is, I want to detect whether or not 'the sprite itself' (instead of just a point on the sprite) has collided with a tile having this specific 'collidable' property so i can affect it's velocity appropriately (eg: reduce velocity along the x axis to 0 if the player sprite hit's the tile from the side). I have looked for documentation around the internet but unfortunately the best I can find is checking on a single point and updating the sprite position to the position of that tile if it does not have some kind of collidable property.

I would greatly appreciate any advice on this issue.

Thanks again

Share this post


Link to post
Share on other sites
Advertisement
This is the code I use to check for collision to see if a point on the sprite has hit a tile that is collidable.

bool collision = false;
[color=#008b00][color=#7925ac] CGPoint[color=#000000] playerPos = [color=#814726]ccp[color=#000000]([color=#3a8288]pos[color=#000000].[color=#7925ac]x[color=#000000],[color=#3a8288]pos[color=#000000].[color=#7925ac]y[color=#000000]); //Where pos is the position the player will be moved to if it is not collidable

tileCoordPos = [[color=#cd00a3]self tileCoordForPosition:playerPos];







-(CGPoint) tileCoordForPosition:(CGPoint) position {



[color=#cd00a3]int x = position.x/theMap.tileSize.width;

[color=#cd00a3]int y = ((theMap.mapSize.height * theMap.tileSize.height) - position.y)/theMap.tileSize.height;

[color=#cd00a3]return [color=#814726]ccp(x,y);

}



tileGID = [stLayer tileGIDAt:tileCoordPos];

[color=#008b00][color=#000000] [color=#cd00a3]if[color=#000000] ([color=#3a8288]tileGID[color=#000000] == [color=#2700dc]193[color=#000000]) //GID of 193 means it is a collidable cell

{

[color=#008b00][color=#000000] [color=#7925ac]NSDictionary[color=#000000] *properties = [[color=#3a8288]theMap[color=#000000] [color=#205a5e]propertiesForGID[color=#000000]:[color=#3a8288]tileGID[color=#000000]]; //get properties for tile on theMap.the tile we wanna move to



[color=#cd00a3]if (properties)

{

[color=#7925ac]NSString *collidable = [properties [color=#430083]valueForKey:[color=#e50000]@"collidable"];

[color=#cd00a3]if (collidable && [collidable compare:[color=#e50000]@"hit"]== NSOrderedSame)

collision = [color=#cd00a3]true;

}

}










But as I mentioned earlier, my problem is I need to see if the actual sprite collided with this tile, and not just the one point. Thanks again for any advice. It's much appreciated!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement