Jump to content
  • Advertisement

Henshin1

Member
  • Content Count

    5
  • Joined

  • Last visited

Community Reputation

100 Neutral

About Henshin1

  • Rank
    Newbie
  1. Hi all, I'm new to game development and I am working on a project using Cocos2d. I previously posted this in another section but i think this is the place it is meant to be. 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. The code I use for this is as follows: bool collision = false; CGPoint playerPos = ccp(pos.x,pos.y); //Where pos is the position the player will be moved to if it is not collidable tileCoordPos = [self tileCoordForPosition:playerPos]; -(CGPoint) tileCoordForPosition:(CGPoint) position { int x = position.x/theMap.tileSize.width; int y = ((theMap.mapSize.height * theMap.tileSize.height) - position.y)/theMap.tileSize.height; return ccp(x,y); [color="#008b00"]//Gets the tile co-ordinates for this point } tileGID = [stLayer tileGIDAt:tileCoordPos]; [color="#008b00"]//Gets the tile value at the point if (tileGID == 193) //GID of 193 means it is a collidable cell { NSDictionary *properties = [theMap propertiesForGID:tileGID]; //get properties for tile on theMap.the tile we wanna move to if (properties) { NSString *collidable = [properties valueForKey:@"collidable"]; if (collidable && [collidable compare:@"hit"]== NSOrderedSame) collision = true; } } 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 !
  2. Henshin1

    Cocos2d Tilemap Collision

    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!
  3. 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
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!