Sign in to follow this  
Henshin1

Cocos2d Tilemap Collision

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
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][color=#000000] playerPos = [/color][color=#814726]ccp[/color][color=#000000]([/color][color=#3a8288]pos[/color][color=#000000].[/color][color=#7925ac]x[/color][color=#000000],[/color][color=#3a8288]pos[/color][color=#000000].[/color][color=#7925ac]y[/color][color=#000000]); [/color]//Where pos is the position the player will be moved to if it is not collidable[/color]

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







-(CGPoint) tileCoordForPosition:(CGPoint) position {



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

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

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

}



tileGID = [stLayer tileGIDAt:tileCoordPos];

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

{

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



[color=#cd00a3]if[/color] (properties)

{

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

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

collision = [color=#cd00a3]true[/color];

}

}










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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this