
任倩å ã®çŽæçãªSMBã³ã³ãããŒã«ãšåªããã¬ãã«ã®èšèšã«ãããé 管工ãšãã®ããŒãããŒã®ä»®æ³äžçã§äœæéãéããããšãã§ããŸããã
Jacob Gundersenã«ãããã®çŽ æŽããããã¥ãŒããªã¢ã«ã§ã¯ãç¬èªã®ãã©ãããã©ãŒããŒãäœæããŸãã ããããã¡ã€ã³ãã£ã©ã¯ã¿ãŒã¯ã³ã¢ã©ã«ãªãã®ã§ãã²ãŒã ããã¹ãŒããŒã³ã¢ãªãªãã©ã¶ãŒãºïŒããšåŒã³ãŸãã
ãŸããã¡ã«ããºã ãåçŽåããããã«ãæµã®ç§»åãå¿ããŠããŸãã 代ããã«ãåºã«åã蟌ãŸããã¹ã¿ããä»ããããã¯ã䜿çšããŸãã ããã«ããããã©ãããã©ãŒããŒã®äžå¿ã§ããç©çãšã³ãžã³ã«å®å šã«éäžã§ããããã«ãªããŸãã
泚æïŒ ã«ããã®äžã§ã¯ãä¿¡ããããªãã»ã©ã®éã®ç¿»èš³ãããããã¹ããåçãã³ãŒãïŒç¿»èš³ãããŠããªãã³ãŒãïŒãããã³ç¬èªã®ç©çãšã³ãžã³ãäœæããããã®ã¬ã€ãïŒ
ãã®ãã¥ãŒããªã¢ã«ã§ã¯ãäºåã«Cocos2Dããã°ã©ãã³ã°ã®åºæ¬ã«ç²ŸéããŠããããšãåæãšããŠããŸãã ãã以å€ã®å Žåã¯ãæåã«Rayã®Webãµã€ãã§ããã€ãã®æåã®ã¬ãã¹ã³ãç解ããããšã匷ããå§ãããŸãã
å§ããŸããã
éå§ããã«ã¯ããã®ãã¥ãŒããªã¢ã«ã®ã¹ã¿ãŒã¿ãŒãããžã§ã¯ããããŠã³ããŒãããŠãã ããã 解åããXcodeã§éããå®è¡ããŸãã åæ§ã®äœãããšãã¥ã¬ãŒã¿ç»é¢ã«è¡šç€ºãããã¯ãã§ãã

ããã§ã-ãã éå±ãªç©ºçœã®ç»é¢ïŒ ïŒ]ãã¥ãŒããªã¢ã«ãé²ããã«ã€ããŠãå®å šã«åããŠãããŸãã
å¿ èŠãªãã¹ãŠã®ç»åãšé³å£°ã¯ãã¹ã¿ãŒã¿ãŒãããžã§ã¯ãã«æ¢ã«è¿œå ãããŠããŸãã ãããžã§ã¯ãã®å 容ãèŠãŠãããŸãããã
- ã²ãŒã ã¢ãŒãã Ray Wikiã®åŠ»ããã®ç¡æã²ãŒã ã¢ãŒãããã¯ãå«ãŸããŠããŸãã
- ã¬ãã«ãããã SMBã®æåã®ã¬ãã«ããå§ããŠãå ·äœçã«ã¬ãã«ããããäœæããŸããã
- çŽ æŽããããµãŠã³ããšãã§ã¯ãã çµå±ã®ãšãããraywenderlich.comã®ãã¥ãŒããªã¢ã«ã§ãïŒ ïŒ]
- ãµãã¯ã©ã¹CCLayerã ç©çãšã³ãžã³ã®ã»ãšãã©ãå®è£ ããGameLevelLayerãšããã¯ã©ã¹ã ä»ã§ã¯ã³ã«ã¯ã®ããã«ç©ºã§ãã ïŒã¯ãããã®èµ€ã¡ããã¯æºããããã®ãåŸ ã£ãŠããŸãïŒïŒ
- ãµãã¯ã©ã¹CCSpriteã Koalaããžãã¯ãå«ãPlayerãšããã¯ã©ã¹ã ä»ãç§ãã¡ã®ã³ã¢ã©ã¯é ãã«é£ã³èŸŒãããšããŠããŸãïŒ
ç©çãšã³ãžã³ã®åºæ¬
ãã©ãããã©ãŒããŒã¯ç©çãšã³ãžã³ã«åºã¥ããŠåäœããŸãããã®ãã¥ãŒããªã¢ã«ã§ã¯ãç¬èªã®ç©çãšã³ãžã³ãäœæããŸãã
åãBox2DãChipminkã䜿çšããªãã§ç¬èªã®ãšã³ãžã³ãäœæããå¿ èŠãããçç±ã¯2ã€ãããŸãã
- 詳现èšå®ã zenãã©ãããã©ãŒããŒãå®å šã«ç解ããã«ã¯ããšã³ãžã³ãå®å šã«ã«ã¹ã¿ãã€ãºããæ¹æ³ãåŠã¶å¿ èŠããããŸãã
- ã·ã³ãã«ã Box2DãšChipmunkã«ã¯ã«ã¹ã¿ãã€ãºå¯èœãªæ©èœããããããããŸãããããã¯æŠããŠç§ãã¡ã«ãšã£ãŠã¯åœ¹ã«ç«ã¡ãŸããã ã¯ãããªãœãŒã¹ããããŸãã ãããŠãç§ãã¡èªèº«ã®ãšã³ãžã³ã¯ãèš±ãéãæ£ç¢ºã«é£ã¹ãŸãã
ç©çãšã³ãžã³ã¯ã2ã€ã®äž»ãªã¿ã¹ã¯ãå®è¡ããŸãã

- åããã·ãã¥ã¬ãŒãããŸãã ç©çãšã³ãžã³ã®æåã®ã¿ã¹ã¯ã¯ãéåãéåããžã£ã³ããæ©æŠã®çžåããåãã·ãã¥ã¬ãŒãããããšã§ãã
- è¡çªãæ€åºããŸãã 2çªç®ã®ã¿ã¹ã¯ã¯ããã¬ã€ã€ãŒãšãã®ã¬ãã«ã®ä»ã®ãªããžã§ã¯ããšã®è¡çªãå€æããããšã§ãã
ããšãã°ããžã£ã³ãäžãäžåãã®åãã³ã¢ã©ã«äœçšããŸãã ãã°ãããããšãéåããžã£ã³ãã®åãäžåããå€å žçãªé床ã®æŸç©ç·ç¶ã®å€åãåŸãããŸãã
è¡çªæ€åºã®å©ããåããŠãéåã®åœ±é¿äžã§åºãéãæãããã³ã«ã³ã¢ã©ãåæ¢ããã³ã¢ã©ããã€ã¹ãã€ã¯ãèžãã ããå€æããŸãïŒayïŒïŒã
å®éã«ã©ã®ããã«æ©èœããããèŠãŠã¿ãŸãããã
ç©çãšã³ãžã³ã®äœæ
äœæããç©çãšã³ãžã³ã§ã¯ãKoalaã«ã¯åããèšè¿°ããç¬èªã®å€æ°ããããŸãïŒé床ãå é床ãäœçœ®ã ãããã®å€æ°ã䜿çšããŠãããã°ã©ã ã®åã¹ãããã§æ¬¡ã®ã¢ã«ãŽãªãºã ã䜿çšããŸãã
- ãžã£ã³ããŸãã¯ã¢ãŒã·ã§ã³ã¢ã¯ã·ã§ã³ãéžæãããŠããŸããïŒ
- ãããããªããã³ã¢ã©ã®ãžã£ã³ããŸãã¯åãã®åŒ·ãã䜿çšããŠãã ããã
- ãŸããéåãã³ã¢ã©ã«é©çšããŸãã
- çµæã®ã³ã¢ã©é床ãèšç®ããŸãã
- åä¿¡ããé床ãKoalaã«é©çšãããã®äœçœ®ãæŽæ°ããŸãã
- Koalasãšä»ã®ãªããžã§ã¯ããšã®è¡çªã確èªããŸãã
- è¡çªãçºçããå Žåã¯ãè¡çªãçºçããªããªããŸã§é害ç©ããã³ã¢ã©ã移åããŸãã ãŸãã¯è²§ããã³ã¢ã¬ãæå·ããŸãã

ããã°ã©ã ã®ãã¹ãŠã®ã¹ãããã§ãããã®ã¹ããããå®è¡ããŸãã ç§ãã¡ã®ã²ãŒã ã§ã¯ãéåã«ãã£ãŠã³ã¢ã©ãåºããã©ãã©ãæ²ãã§ãããŸãããè¡çªãæ€åºããããã³ã«åºã®äžã®ãã€ã³ãã«æ»ããŸãã ãã®æ©èœã䜿çšããŠãã³ã¢ã©ãåå°ã«è§ŠããŠãããã©ãããå€æããããšãã§ããŸãã ããã§ãªãå Žåã¯ãã³ã¢ã©ããžã£ã³ãç¶æ ã«ããããé害ç©ãããžã£ã³ãããçŽåŸã«ãã¬ãŒã€ãŒããžã£ã³ãããã®ãé²ãããšãã§ããŸãã
ãã€ã³ã1ã5ã¯Koalaãªããžã§ã¯ãå ã§çºçããŸãã å¿ èŠãªæ å ±ã¯ãã¹ãŠãã®ãªããžã§ã¯ãå ã«ä¿åããå¿ èŠããããKoaleãå€æ°èªäœãæŽæ°ã§ããããã«ããããšã¯éåžžã«è«ççã§ãã
ãã ãã6çªç®ã®ç¹ãã€ãŸãè¡çªã®å®çŸ©ã«ã€ããŠã¯ãå£ãåºãæµããã®ä»ã®å±éºãªã©ãã¬ãã«ã®ãã¹ãŠã®æ©èœãèæ ®ããå¿ èŠããããŸãã è¡çªæ€åºã¯ãGameLevelLayerã䜿çšããŠããã°ã©ã ã®ãã¹ãŠã®ã¹ãããã§å®è¡ãããŸã-ããã¯ãã»ãšãã©ã®ç©çã¿ã¹ã¯ãå®è¡ããCCLayerã®ãµãã¯ã©ã¹ã§ããããšãæãåºããŠãã ããã
ã³ã¢ã©ãèªåã®æã§äœçœ®ãæŽæ°ã§ããããã«ãããšãæçµçã«ã³ã¢ã©ã¯å£ãåºã«è§ŠããŸãã ãããŠãGameLevelLayerã¯ã³ã¢ã«ã埩掻ãããŸãã ãããŠäœåºŠãäœåºŠã-ããã¯ã³ã¢ã©ã圌女ãæ¯åããŠããããã«èŠããããã«ãªããŸãã ïŒæã®ã³ãŒããŒãå€ããããã³ã¢ãªãªïŒïŒ
ãã®ãããã³ã¢ã¬ããã®ç¶æ ãæŽæ°ããããšãèš±å¯ããŸããã 代ããã«ãKoalaãæŽæ°ããæ°ããå€æ°desiredPositionãKoaleã«è¿œå ããŸãã GameLevelLayerã¯ãKoalaãdesiredPositionã«ç§»åã§ãããã©ããã確èªããŸãã ãã®å ŽåãGameLevelLayerã¯Koalaã®ç¶æ ãæŽæ°ããŸãã
ãã¹ãŠãæ確ã§ããïŒ ã³ãŒãã§ã©ã®ããã«èŠãããèŠãŠã¿ãŸãããïŒ
TMXTiledMapãããŠã³ããŒããã
Tile Mapsã®ãããªããããã©ã®ããã«æ©èœãããã«ã€ããŠã¯ãåãã ãšæããŸãã ããã§ãªãå Žåã¯ã ãã®ãã¥ãŒããªã¢ã«ã§ãããã«ã€ããŠèªãããšããå§ãããŸãã
ã¬ãã«ãèŠãŠã¿ãŸãããã ã¿ã€ã«ããããšãã£ã¿ãŒãèµ·åãïŒããããŸã è¡ã£ãŠããªãå Žåã¯ããŠã³ããŒãããŸãïŒããããžã§ã¯ããã©ã«ããŒããlevel1.tmxãéããŸãã 以äžã衚瀺ãããŸãã

ãµã€ãããŒãèŠããšã3ã€ã®ç°ãªãã¬ã€ã€ãŒãããããšãããããŸãã
- å±éºïŒãã®å±€ã«ã¯ãã³ã¢ã©ãçãç¶ããããã«æ³šæããªããã°ãªããªããã®ãå«ãŸããŠããŸãã
- å£ïŒãã®ã¬ã€ã€ãŒã«ã¯ãKoalaãééã§ããªãã»ã«ãå«ãŸããŠããŸãã åºæ¬çã«ããããã¯ããã¢ã»ã«ã§ãã
- èæ¯ïŒãã®ã¬ã€ã€ãŒã«ã¯ãé²ãäžãªã©ã®çŸçãªãã®ã®ã¿ãå«ãŸããŸãã
ã³ãŒãã£ã³ã°ã®æéã§ãïŒ GameLevelLayer.mãéãã #importã®åŸ
@implementation
åã«æ¬¡ãè¿œå ããŸãã
@interface GameLevelLayer() { CCTMXTiledMap *map; } @end
headã¯ã©ã¹ã®ã¡ãã·ã¥ããããæäœããããã«ãCCTMXTiledMapã¯ã©ã¹ã®ããŒã«ã«å€æ°ããããè¿œå ããŸããã
次ã«ãã¬ã€ã€ãŒã®åæåäžã«ã¬ã€ã€ãŒã«ã¡ãã·ã¥ããããé 眮ããŸãã initã¡ãœããã«æ¬¡ãè¿œå ããŸãã
CCLayerColor *blueSky = [[CCLayerColor alloc] initWithColor:ccc4(100, 100, 250, 255)]; [self addChild:blueSky]; map = [[CCTMXTiledMap alloc] initWithTMXFile:@"level1.tmx"]; [self addChild:map];
æåã«ãéã空ã®è²ã«èæ¯ïŒCCLayerColorïŒãè¿œå ããŸããã 次ã®2è¡ã®ã³ãŒãã¯ããããå€æ°ïŒCCTMXTiledMapïŒãèªã¿èŸŒãã§ã¬ã€ã€ãŒã«è¿œå ããã ãã§ãã
次ã«ã PlayerLevelLayer.mã§Player.hãã€ã³ããŒãããŸãã
#import "Player.h"
åŒãç¶ãGameLevelLayer.m㧠ã
@ interface
ã»ã¯ã·ã§ã³ã«æ¬¡ã®ããŒã«ã«å€æ°ãè¿œå ããŸãã
Player * player;
次ã«ã initã¡ãœããã«æ¬¡ã®ã³ãŒãã䜿çšããŠãKoalaãã¬ãã«ã«è¿œå ããŸã ã
player = [[Player alloc] initWithFile:@"koalio_stand.png"]; player.position = ccp(100, 50); [map addChild:player z:15];
ãã®ã³ãŒãã¯Koalaã¹ãã©ã€ããªããžã§ã¯ããããŒããããã®äœçœ®ãèšå®ããŠããããªããžã§ã¯ãã«è¿œå ããŸãã
ããªãã¯ããªãåã«ã³ã¢ã©ãªããžã§ã¯ããã¬ã€ã€ã«çŽæ¥è¿œå ããã®ã§ã¯ãªãããããã«è¿œå ããã®ã§ããããïŒ ãã¹ãŠãã·ã³ãã«ã§ãã ã©ã®ã¬ã€ã€ãŒãã³ã¢ã©ã®åã«é 眮ããã©ã®ã¬ã€ã€ãŒãã³ã¢ã©ã®åŸãã«é 眮ããããçŽæ¥å¶åŸ¡ããŸãã ãããã£ãŠãã³ã¢ã«ãã¡ã€ã³ã¬ã€ã€ãŒã§ã¯ãªããã«ãŒãã®åã«ããŸãã Koalaãåé¢ã«é 眮ãããããZãªãŒããŒã«15ãæå®ããŸãããŸããããããã¹ã¯ããŒã«ããŠããã³ã¢ã©ã¯ã¡ã€ã³ã¬ã€ã€ãŒã§ã¯ãªãããããã«å¯ŸããŠåãäœçœ®ã«ãããŸãã
ããããã£ãŠã¿ããïŒ ãããžã§ã¯ããå®è¡ãããšã次ã衚瀺ãããã¯ãã§ãã

ã²ãŒã ã®ããã«èŠããŸãããã³ã¢ãªãªã¯éåãç¡èŠããŸãïŒ ç©çãšã³ãžã³ã䜿çšããŠã倩ããå°çãžãšéãããŸãïŒ]
ã³ã¢ãªãªã®éåç¶æ³

ç©çåŠã®ã·ãã¥ã¬ãŒã·ã§ã³ãäœæããã«ã¯ãåãåã£ãæ å ±ããå§ããŠãã³ã¢ã©ã®ç¶æ ãèæ ®ããã³ã¢ã©ã«åãå ããåå²ããžãã¯ã®è€éãªã»ãããäœæããŸãã ãããããã®äžçã¯ããã«è€éã«ãªããããŸã-ããã¯ããã»ã©é£ããä»äºã§ã¯ãªãã®ã§ãæ¬åœã®ç©çåŠã§ãã çŸå®ã®äžçã§ã¯ãéåã¯åžžã«ãªããžã§ã¯ããåŒãäžããŸãã ãã®ãããäžå®ã®éåãå ããŠãããã°ã©ã ã®ãã¹ãŠã®ã¹ãããã§ã³ã¢ã©ã«é©çšããŸãã
ä»ã®åããåã«ãªã³ãšãªããåãæ¿ããã ãã§ã¯ãããŸããã çŸå®ã®äžçã§ã¯ãå¥ã®åãæåã®åãäžåããçãããªããŸã§ãåã¯ãªããžã§ã¯ãã«äœçšããŸãã
ããšãã°ãè·³èºåã¯éåããªãã«ããŸããã éåãåã³ã³ã¢ã©ãå°é¢ã«æŒãä»ãããŸã§ããã°ããéåã®åãè¶ ããŸãã
ãããç©çåŠã®ã¢ãã«åæ¹æ³ã§ãã ã³ã¢ã©ã«éåãããããã©ããã決ããã ãã§ã¯ãããŸããã éåã¯åžžã«ååšããŸãã
ç¥ãæŒãã

ãšã³ãžã³ã®ããžãã¯ã¯ãåããªããžã§ã¯ãã«äœçšããå Žåãä»ã®åãæåã®åãè¶ ãããŸã§åãç¶ããããšãæå³ããŸãã Coalioãæ£ããé£ã³éãããšãã圌ã¯ãã¹ã§é害ç©ã«ééãããŸã§äžå®ã®å éã§äžã«ç§»åãç¶ããŸãã CoalioãåãããŠãã圌ã®åãã®åã䜿ãã®ãããããŸã§ã圌ã¯åããæ¢ããŸããã Coalioãåæ¢ãããŸã§æ©æŠãäœçšããŸãã
ç©çãšã³ãžã³ãäœæãããšããã®ãããªåçŽãªã²ãŒã ããžãã¯ããæ°·åºãåŽããã®èœäžãªã©ã®è€éãªç©ççåé¡ã®è§£æ±ºã«ã©ã®ããã«åœ¹ç«ã€ããããããŸãã ãã®åäœã¢ãã«ã«ãããã²ãŒã ãåçã«å€æŽã§ããŸãã
ãŸãããã®ãããªéŠ¬ã«ããåãã¯ããªããžã§ã¯ãã®ç¶æ ãåžžã«ç¢ºèªããå¿ èŠããªããããå®è£ ã容æã«ããŸãããªããžã§ã¯ãã¯ãçŸå®äžçã®ç©çæ³åã«åŸãã ãã§ãã
æã ãç¥ãæŒããå¿ èŠããããŸãïŒ ïŒ]
å°çã®æ³åïŒCGPointsããã³Forces
次ã®æŠå¿µã瀺ããŸãããã
- é床ã¯ããªããžã§ã¯ããç¹å®ã®æ¹åã«ç§»åããé床ã瀺ããŸãã
- å éã¯ããªããžã§ã¯ãã®é床ãšæ¹åãæéãšãšãã«ã©ã®ããã«å€åããããè¡šããŸãã
- 匷ãã¯ãé床ãŸãã¯æ¹åã®å€åãåŒãèµ·ããå¹æã§ãã
ç©çã·ãã¥ã¬ãŒã·ã§ã³ã§ã¯ããªããžã§ã¯ãã«é©çšãããåã¯ãªããžã§ã¯ããç¹å®ã®é床ã«å éãããªããžã§ã¯ãã¯ãã¹å ã§å¥ã®åã«ééãããŸã§ãã®é床ã§ç§»åããŸãã é床ã¯ãæ°ããã¢ã¯ãã£ããªãã©ãŒã¹ãçŸãããšããã¬ãŒã ããšã«å€åããå€ã§ãã
CGPointæ§é ã䜿çšããŠãé床ãå/å é床ãäœçœ®ã®3ã€ã®ããšã瀺ããŸãã CGPointæ§é ã䜿çšããçç±ã¯2ã€ãããŸãã
- ãããã¯2Dã§ãã é床ããã¯ãŒ/å é床ãããã³äœçœ®ã¯ãã¹ãŠã2Dã²ãŒã ã®2Då€ã§ãã éåã¯äžæ¹åã«ã®ã¿äœçšãããšèšãããšãã§ããŸãããã²ãŒã ã®ããæç¹ã§éåã®æ¹åãç·æ¥ã«å€æŽããå¿ èŠãããå Žåã¯ã©ãã§ããããïŒ ã¹ãŒããŒããªãªã®ã£ã©ã¯ã·ãŒãèããŠãã ããïŒ
- ããã¯äŸ¿å©ã§ãã CGPointã䜿çšãããšãCocos2Dã«çµã¿èŸŒãŸããŠããããŸããŸãªæ©èœã䜿çšã§ããŸãã ç¹ã«ãccpAddïŒå ç®ïŒãccpSubïŒæžç®ïŒãccpMultïŒfloatåã®å€æ°ã«ããä¹ç®ïŒã䜿çšããŸãã ããã«ãããã³ãŒãã®èªã¿åããšãããã°ãã¯ããã«ç°¡åã«ãªããŸãïŒ
ã³ã¢ã©ã®ãªããžã§ã¯ãã®é床ã¯å¯å€ã§ãããéåãéåããžã£ã³ããæ©æŠãªã©ã®ããŸããŸãªåã®åºçŸã«ãã£ãŠå€åããŸãã
ã²ãŒã ã®åã¹ãããã§ããã¹ãŠã®åãå ç®ããçµæã®å€ãã³ã¢ã©ã®çŸåšã®é床ã«å ç®ããŸãã ãã®çµæãæ°ããçŸåšã®é床ãåãåããŸãã ãã¬ãŒã ã¬ãŒãã䜿çšããŠåæžããŸãã ãã®åŸãã³ã¢ã©ã移åããŸãã
泚æïŒäžèšã®ããããã誀解ãæãå ŽåãçŽ æŽããã人ç©ã§ãããããšã«ã»ã·ããã³ã¯ããã¯ã¿ãŒã«é¢ããåªãããã¥ãŒããªã¢ã«ãäœæããŸãããããã¯ã䜿çšããæ§é ç©ã«å¯Ÿããåã®äœçšãå®å šã«èª¬æããŠããŸããéåããå§ããŸãããã åã䜿çšããå®è¡ã«ãŒããäœæããŸãã ifæ¡ä»¶ãããã¯ãéããçŽåã«ã GameLevelLayer.mãã¡ã€ã«ã®initã¡ãœããã«æ¬¡ã®ã³ãŒããè¿œå ããŸãã
[self schedule:@selector(update:)];
次ã«ãæ°ããã¡ãœãããã¯ã©ã¹ã«è¿œå ããŸãã
- (void)update:(ccTime)dt { [player update:dt]; }
次ã«ã Player.hãéãã次ã®ããã«å€æŽããŸãã
#import <Foundation/Foundation.h> #import "cocos2d.h" @interface Player : CCSprite @property (nonatomic, assign) CGPoint velocity; - (void)update:(ccTime)dt; @end
次ã®ã³ãŒããPlayer.mã«è¿œå ããŸãã
ç§ãã¯ãªãã¯
#import "Player.h" @implementation Player @synthesize velocity = _velocity; // 1 - (id)initWithFile:(NSString *)filename { if (self = [super initWithFile:filename]) { self.velocity = ccp(0.0, 0.0); } return self; } - (void)update:(ccTime)dt { // 2 CGPoint gravity = ccp(0.0, -450.0); // 3 CGPoint gravityStep = ccpMult(gravity, dt); // 4 self.velocity = ccpAdd(self.velocity, gravityStep); CGPoint stepVelocity = ccpMult(self.velocity, dt); // 5 self.position = ccpAdd(self.position, stepVelocity); } @end
äžèšã®ã³ãŒããã¹ãããããšã«èŠãŠãããŸããã
- ããã§ããªããžã§ã¯ããåæåããé床å€æ°ããŒãã«èšå®ããæ°ããinitã¡ãœãããè¿œå ããŸããã
- ããã§ãéåãã¯ãã«ã®å€ãæå®ããŸããã 1ç§ããšã«ãKoalaã®é床ã450ãã¯ã»ã«ãã€å éããŸãã
- ããã§ã¯ãéåãã¯ãã«ã®å€ãæžãããŠãã¬ãŒã ã¬ãŒããæºããããã«ccpMultã䜿çšããŸããã ccpMultã¯ããããŒããšCGPointãåãåããCGPointãè¿ããŸãã
- ããã§ã¯ãçŸåšã®ã¹ãããã®éåãèšç®ãããšããã«ãçŸåšã®é床ã«éåãè¿œå ããŸãã
- æåŸã«ã1ã¹ãããã®é床ãèšç®ããããccpAddã䜿çšããŠã³ã¢ã©ã®äœçœ®ãæŽæ°ããŸãã
ããã§ãšãããããŸãïŒ æåã®ç©çãšã³ãžã³ãäœæããæ£ããéãæ©ãã§ããŸãïŒ ãããžã§ã¯ããå®è¡ããŠçµæã確èªããŠãã ããïŒ

Oooo-Coalioãåºã«èœã¡ãŸãïŒ ãããä¿®æ£ããŸãããã
å€ã®ããã¯-è¡çªæ€åº
è¡çªæ€åºã¯ãç©çãšã³ãžã³ã®åºç€ã§ãã ç»åãã¬ãŒã ã®åçŽãªäœ¿çšããè€éãª3Dãªããžã§ã¯ãã®è¡çªãŸã§ãããŸããŸãªã¿ã€ãã®è¡çªæ€åºããããŸãã 幞ããªããšã«ããã©ãããã©ãŒããŒã¯è€éãªæ§é ãå¿ èŠãšããŸããã
ãªããžã§ã¯ããšã®ã³ã¢ã©ã®è¡çªãæ€åºããã«ã¯ãã³ã¢ã©ãçŽæ¥å²ãã»ã«ã«TMXTileMapã䜿çšããŸãã ããã«ãiOSã«çµã¿èŸŒãŸããããã€ãã®é¢æ°ã䜿çšããŠãKoalaã®ã¹ãã©ã€ããã»ã«ã®ã¹ãã©ã€ããšäº€å·®ãããã©ããã確èªããŸãã
CGRectIntersectsRectããã³CGRectIntersectioné¢æ°ã¯ããããã®ãã§ãã¯ãéåžžã«ç°¡åã«ããŸãã CGRectIntersectsRectã¯2ã€ã®é·æ¹åœ¢ã亀差ãããã©ããããã§ãã¯ããCGRectIntersectionã¯äº€å·®ããé·æ¹åœ¢ãè¿ããŸãã
ãŸããKoalaã®ã¹ã³ãŒããå®çŸ©ããå¿ èŠããããŸãã ããŒããããåã¹ãã©ã€ãã«ã¯ãã¬ãŒã ããããŸãããã¬ãŒã ã¯ãã¯ã¹ãã£ã®ãµã€ãºã§ãããboundingBoxãšåŒã°ãããã©ã¡ãŒã¿ãŒã䜿çšããŠã¢ã¯ã»ã¹ã§ããŸãã
ãã§ã«boundingBoxã«ãããã¬ãŒã ãå®çŸ©ããã®ã¯ãªãã§ããïŒ éåžžããã¯ã¹ãã£ã®åšå²ã«ã¯éæãªãšããžããããŸãããè¡çªãå€æããéã«èæ ®ããŸããã
æã«ã¯ãã¹ãã©ã€ãã®å®éã®ç»åïŒéæã§ã¯ãªãïŒã®åšå²ã®æ°ãã¯ã»ã«ãèæ ®ããå¿ èŠãããããŸããã ããªãªãå£ã«ã¶ã€ãã£ããšãã圌ã¯ããã«å°ã觊ããŸããããããšã圌ã®éŒ»ã¯ãããã¯ã«ãããã«åãŸã£ãŠããŸããïŒ
ãã£ãŠã¿ãŸãããã Player.hã«è¿œå ããŸã ã
-(CGRect)collisionBoundingBox;
Player.mã«è¿œå ããŸã ã
- (CGRect)collisionBoundingBox { return CGRectInset(self.boundingBox, 2, 0); }
CGRectInsetã¯ã2çªç®ãš3çªç®ã®åŒæ°ã®ãã¯ã»ã«æ°ã§CGRectãå§çž®ããŸãã ãã®å Žåãè¡çªãã¬ãŒã ã®å¹ ã¯6ãã¯ã»ã«å°ãããªããŸã-äž¡åŽã«3ãã¯ã»ã«ã
ééæã
ééç©ãæã¡äžããæéã§ãã ïŒããããããªãã¯ä»ç§ã倪ã£ãŠãããšæããŸãããïŒãã³ã¢ãªãªã¯èšããŸãïŒã
è¡çªãæ€åºããã«ã¯ãGameLevelLayerã«ããã€ãã®ã¡ãœãããå¿ èŠã§ãã ç¹ã«ïŒ
- çŸåšã®Coalioã»ã«ãå²ã8ã€ã®ã»ã«ã®åº§æšãè¿ãã¡ãœããã
- ã©ã®ã»ã«ãé害ç©ã§ãããïŒããã³äžè¬çã«ååšãããã©ããïŒã決å®ããæ¹æ³ã äžéšã®ã»ã«ã«ã¯ç©ççç¹æ§ïŒé²ïŒããªããCoalioã¯ããããšè¡çªããŸããã
- åªå é äœã«åŸã£ãŠè¡çªãåŠçããæ¹æ³ã
äžèšã®æ¹æ³ãç°¡çŽ åãã2ã€ã®ãã«ããŒé¢æ°ãäœæããŸãã
- Coalioã»ã«ã®äœçœ®ã決å®ããæ¹æ³ã
- ã»ã«åº§æšãåãåããCocos2D座æšã§ã»ã«ã®é·æ¹åœ¢ãè¿ãã¡ãœããã
次ã®ã³ãŒããGameLevelLayer.mã«è¿œå ããŸãã
- (CGPoint)tileCoordForPosition:(CGPoint)position { float x = floor(position.x / map.tileSize.width); float levelHeightInPixels = map.mapSize.height * map.tileSize.height; float y = floor((levelHeightInPixels - position.y) / map.tileSize.height); return ccp(x, y); } - (CGRect)tileRectFromTileCoords:(CGPoint)tileCoords { float levelHeightInPixels = map.mapSize.height * map.tileSize.height; CGPoint origin = ccp(tileCoords.x * map.tileSize.width, levelHeightInPixels - ((tileCoords.y + 1) * map.tileSize.height)); return CGRectMake(origin.x, origin.y, map.tileSize.width, map.tileSize.height); }
æåã®ã¡ãœããã¯ãã¡ãœããã«æž¡ããã¯ã»ã«åäœã®åº§æšã«ããã»ã«ã®åº§æšãè¿ããŸãã ã»ã«ã®äœçœ®ãååŸããã«ã¯ãã»ã«ãµã€ãºã§åº§æšãé€ç®ããŸãã
Cocos2D / OpenGLã·ã¹ãã ã®åº§æšã¯å·Šäžé ããå§ãŸããã·ã¹ãã 座æšã¯å·Šäžé ããå§ãŸããããé«ãã®åº§æšãå転ããå¿ èŠããããŸãã æšæº-ããã¯ã¯ãŒã«ã§ã¯ãããŸãããïŒ
2çªç®ã®æ¹æ³ã¯å察ã®ããšãè¡ããŸãã ã»ã«åº§æšã«ã»ã«ãµã€ãºãä¹ç®ãããã®ã»ã«ã®CGRectãè¿ããŸãã ç¹°ãè¿ããŸãããé«ããæ¡åŒµããå¿ èŠããããŸãã
ãªãé«ãã®y座æšã«1ãè¿œå ããå¿ èŠãããã®ã§ããïŒ ã»ã«ã®åº§æšã¯ãŒãããå§ãŸãããã20åã®ã»ã«ã®å®éã®åº§æšã¯19ã§ããé«ãã«1ãè¿œå ããªãå Žåããã€ã³ãã¯19 * tileHeightã«ãªããŸãã
ç§ã¯çŽ°èã«å²ãŸããŠããŸãïŒ
ããã§ã¯ãã³ã¢ã©ãå²ãã»ã«ã決å®ããæ¹æ³ã«ç§»ããŸãããã ãã®ã¡ãœããã§ã¯ãé åãäœæãããããè¿ããŸãã ãã®é åã«ã¯ãã»ã«ã®GIDãã»ã«åº§æšãããã³ãã®ã»ã«ã®CGRectæ å ±ãå«ãŸããŸãã
ãã®é åãåªå 床é ã«æŽçããè¡çªãå€æããŸãã ããšãã°ãæãã®è¡çªãå®çŸ©ããåã«ãäžãå·Šãå³ãäžããè¡çªãå€æããããšããŸãã ãŸããã³ã¢ã©ãšäžã®ã»ã«ã®è¡çªãæ€åºãããšãå°é¢ã«è§Šããããã®ãã©ã°ãèšå®ããŸãã
ãã®ã¡ãœãããGameLevelLayer.mã«è¿œå ããŸã ã
ç§ãã¯ãªãã¯
- (NSArray *)getSurroundingTilesAtPosition:(CGPoint)position forLayer:(CCTMXLayer *)layer { CGPoint plPos = [self tileCoordForPosition:position]; //1 NSMutableArray *gids = [NSMutableArray array]; //2 for (int i = 0; i < 9; i++) { //3 int c = i % 3; int r = (int)(i / 3); CGPoint tilePos = ccp(plPos.x + (c - 1), plPos.y + (r - 1)); int tgid = [layer tileGIDAt:tilePos]; //4 CGRect tileRect = [self tileRectFromTileCoords:tilePos]; //5 NSDictionary *tileDict = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:tgid], @"gid", [NSNumber numberWithFloat:tileRect.origin.x], @"x", [NSNumber numberWithFloat:tileRect.origin.y], @"y", [NSValue valueWithCGPoint:tilePos],@"tilePos", nil]; [gids addObject:tileDict]; } [gids removeObjectAtIndex:4]; [gids insertObject:[gids objectAtIndex:2] atIndex:6]; [gids removeObjectAtIndex:2]; [gids exchangeObjectAtIndex:4 withObjectAtIndex:6]; [gids exchangeObjectAtIndex:0 withObjectAtIndex:4]; //6 for (NSDictionary *d in gids) { NSLog(@"%@", d); } //7 return (NSArray *)gids; }
Pff-ã³ãŒãå šäœã å¿é ããªãã§ã詳现ã«èª¬æããŸãã
ãããããã®åã«ããããäžã«3ã€ã®ã¬ã€ã€ãŒãããããšã«æ³šæããŠãã ããã
ç°ãªãã¬ã€ã€ãŒãååšãããããåã¬ã€ã€ãŒã®è¡çªãå¥ã ã«å®çŸ©ã§ããŸãã
- ã³ã¢ã©ãšå±éºã è¡çªãçºçããå Žåãã³ã¢ã©ã殺ããŸãïŒååã«æ®é ·ã§ãããïŒïŒã
- ã³ã¢ã©ãšå£ã è¡çªãçºçããå Žåãã³ã¢ã¬ããã®æ¹åã«ããã«ç§»åããããšã¯ã§ããŸããã ãåŸ ã£ãŠãããŒã¬ïŒã
- ã³ã¢ã©ãšèæ¯ã è¡çªãçºçããå ŽåãäœãããŸããã æ zyãªããã°ã©ããŒã¯æé«ã®ããã°ã©ããŒã§ãã ããŠããŸãã¯äººã ã¯äœãšèšããŸããïŒ
ãã¡ãããããŸããŸãªãããã¯ãšã®ããŸããŸãªè¡çªãå€æããã«ã¯ããŸããŸãªæ¹æ³ããããŸããããããäžã®ã¬ã€ã€ãŒãªã©ãéåžžã«å¹æçãªãã®ããããŸãã
ããŠãã¹ãããããšã«ã³ãŒããèŠãŠãããŸãããã
1.ãŸããå ¥åããã»ã«ã®åº§æšïŒã³ã¢ã©ã®åº§æšïŒãååŸããŸãã
2.次ã«ãã»ã«ã«é¢ããæ å ±ãè¿ãæ°ããé åãäœæããŸãã
3.次ã«ããµã€ã¯ã«ã9åéå§ããŸããã³ã¢ã©ãæ¢ã«é 眮ãããŠããã»ã«ãå«ãã9ã€ã®å¯èœãªç§»åã»ã«ãããããã§ãã 次ã®æ°è¡ã¯ã9ã€ã®ã»ã«ã®äœçœ®ãå®çŸ©ããããããtilePoså€æ°ããä¿åããŸãã
泚ïŒã³ã¢ã©ãæ¢ã«é 眮ãããŠããã»ã«ãšã®è¡çªãå€æããå¿ èŠããªãããã8ã€ã®ã»ã«ã«é¢ããæ å ±ã®ã¿ãå¿ èŠã§ãã
ãã®ã±ãŒã¹ãåžžã«ãã£ããããŠãã³ã¢ã©ãã»ã«ã®1ã€ã«ç§»åããå¿ èŠããããŸãã Coalioããœãªããã»ã«å ã«ããå ŽåãCoalioã¹ãã©ã€ãã®åå以äžãäžã«å ¥ã£ãŠããŸãã 圌ã¯ããã»ã©éãåãã¹ãã§ã¯ãããŸãã-å°ãªããšããã®ã²ãŒã ã§ã¯ïŒ
ãããã®8ã€ã®ã»ã«ãæäœããããããã«ã¯ãæåã«Coalioã»ã«ãè¿œå ããæåŸã«åé€ããŸãã
4. 4çªç®ã®ã»ã¯ã·ã§ã³ã§ã¯ãtileGIDAtãåŒã³åºããŸãã ãã®ã¡ãœããã¯ãç¹å®ã®åº§æšã«ããã»ã«ã®GIDãè¿ããŸãã åä¿¡ãã座æšã«ã»ã«ããªãå Žåãã¡ãœããã¯ãŒããè¿ããŸãã 次ã«ãå€ãcell not foundãã«ãŒãã䜿çšããŸãã
5.次ã«ããã«ããŒã¡ãœããã䜿çšããŠãCocos2D座æšããŒã¿ã®ã»ã«ã®CGRectãèšç®ããŸãã åä¿¡ããæ å ±ãNSDictionaryã«ä¿åããŸãã ãã®ã¡ãœããã¯ãåä¿¡ããNSDictionaryããé åãè¿ããŸãã
6. 6çªç®ã®ã»ã¯ã·ã§ã³ã§ã¯ãé åããKoalaã»ã«ãåé€ããã»ã«ãåªå 床é ã«äžŠã¹æ¿ããŸãã

å€ãã®å Žåãã³ã¢ã©ã®äžã®ã»ã«ãšã®è¡çªã®å Žåãã»ã«ãšã®è¡çªãæãã«å®çŸ©ããŸãã å³ã®å³ãã芧ãã ããã èµ€ã§ç€ºãããã³ã¢ã©ã®äžã®ã»ã«ãšã®è¡çªãæ€åºããããšã«ãããéã§åŒ·èª¿è¡šç€ºããããããã¯ïŒ2ãšã®è¡çªãæ€åºããŸãã
è¡çªæ€åºã¢ã«ãŽãªãºã ã¯ããã€ãã®ä»®å®ã䜿çšããŸãã ãããã®ä»®å®ã¯ã察è§ã»ã«ã§ã¯ãªãé£æ¥ã»ã«ã«åœãŠã¯ãŸããŸãã ãã®ãããå¯èœãªéã察è§ã»ã«ã䜿çšããã¢ã¯ã·ã§ã³ãé¿ããããã«ããŸãã
ãããŠãããã¯äžŠã¹æ¿ãååŸã®é åå ã®ã»ã«ã®é åºãæ確ã«ç€ºããåçã§ãã äžãäžãå³ãå·Šã®ã»ã«ãæåã«åŠçãããããšã«æ°ä»ããããããŸããã ã»ã«ã®é åºããããã°ãã³ã¢ã©ããã€å°é¢ã«è§Šããã®ããé²ã®äžãé£ãã ã®ããå€æãããããªããŸãã

7.ã»ã¯ã·ã§ã³7ã®ãµã€ã¯ã«ã«ãããã»ã«ããªã¢ã«ã¿ã€ã ã§ç£èŠã§ããŸãã ã§ãããããã¹ãŠãèšç»éãã«é²ãã§ããããšã確å®ã«ç¥ãããšãã§ããŸãã
次ã®ã²ãŒã ã®çºå£²ã®æºåãã»ãŒæŽããŸããïŒ ãã ãããŸã ããã€ãã®å°ããªããšãããå¿ èŠããããŸãã å£ã¬ã€ã€ãŒãå€æ°ãšããŠGameLevelLayerã¯ã©ã¹ã«è¿œå ããŠã䜿çšã§ããããã«ããå¿ èŠããããŸãã
GameLevelLayer.må ã§ã次ã®å€æŽãè¡ããŸãã
// @interface CCTMXLayer *walls; // init, , walls = [map layerNamed:@"walls"]; // update [self getSurroundingTilesAtPosition:player.position forLayer:walls];
çºå°ïŒ ããããæ®å¿µãªãããã²ãŒã ã¯ã¯ã©ãã·ã¥ããŸãã ã³ã³ãœãŒã«ã«æ¬¡ã®ããã«è¡šç€ºãããŸãã

ãŸããã»ã«ã®äœçœ®ãšGIDå€ã«é¢ããæ å ±ãååŸããŸãïŒãã ããäžéšã«ç©ºã®å°åœ¢ããããããã»ãšãã©ã¯ãŒãã§ãïŒã
æçµçã«ããšã©ãŒãTMXLayerïŒç¡å¹ãªäœçœ®ãã§ãã¹ãŠãã¯ã©ãã·ã¥ããŸãã ããã¯ããããã®ç«¯ã®å€åŽã®äœçœ®ãtileGIDatïŒã¡ãœããã«æž¡ããããšãã«çºçããŸãã
å°ãåŸã§ãã®ãšã©ãŒãåé¿ããŸãããæåã«ãæ¢åã®è¡çªã®å®çŸ©ãå€æŽããŸãã
ã³ã¢ã©ã®ç¹æš©ãåãæ»ããŸã
ãã®ç¬éãŸã§ãã³ã¢ã©èªäœããã®äœçœ®ãæŽæ°ããŠããŸããã ãããä»ãç§ãã¡ã¯åœŒå¥³ãããã®ç¹æš©ãåã£ãŠããŸãã

ã³ã¢ã©ãç¬ç«ããŠãã®äœçœ®ãæŽæ°ãããšãæçµçã«ã¯çã£ãããã«ãžã£ã³ããå§ããŸãïŒ ããããç§ãã¡ã¯ãããæãã§ããŸããã
ãã®ãããKoalaã§ã¯ãGameLevelLayerãšå¯Ÿè©±ããè¿œå ã®å€æ°desiredPositionãå¿ èŠã§ãã
Koalaã¯ã©ã¹ã¯ããã®æ¬¡ã®äœçœ®ãç¬ç«ããŠèšç®ããå¿ èŠããããŸãã ãã ããGameLevelLayerã¯ã劥åœæ§ã確èªããåŸã«ã®ã¿ãã³ã¢ã©ãç®çã®äœçœ®ã«ç§»åããå¿ èŠããããŸãã åãããšãè¡çªæ€åºãµã€ã¯ã«ã«ãåœãŠã¯ãŸããŸãããã¹ãŠã®ã»ã«ã®è¡çªããã§ãã¯ãããåã«ãå®éã®ã¹ãã©ã€ããæŽæ°ããããšã¯æãŸãããããŸããã
ããã€ãã®å€æŽãå¿ èŠã§ãã ãŸãã以äžãPlayer.hã«è¿œå ããŸã
@property (nonatomic, assign) CGPoint desiredPosition;
Player.mã«è¿œå ããããã®ãåæããŸã ã
@synthesize desiredPosition = _desiredPosition;
次ã«ã Player.mã®collisionBoundingBoxã¡ãœããã次ã®ããã«å€æŽããŸãã
- (CGRect)collisionBoundingBox { CGRect collisionBox = CGRectInset(self.boundingBox, 3, 0); CGPoint diff = ccpSub(self.desiredPosition, self.position); CGRect returnBoundingBox = CGRectOffset(collisionBox, diff.x, diff.y); return returnBoundingBox; }
ãã®ã³ãŒãã¯ãGameLevelLayerãè¡çªãå€æããããã«äœ¿çšããæãŸããäœçœ®ã«åºã¥ããŠãã¬ãŒã ãèšç®ããŸãã
泚ïŒã³ãªãžã§ã³ãã¬ãŒã ã®èšç®ã«ã¯ããŸããŸãªæ¹æ³ããããŸãã CCNodeã¯ã©ã¹ã«æ¢ã«ãããã®ãšåæ§ã®ã³ãŒããæžãããšãã§ããŸãããçŸåšã®ã¡ãœããã¯ãããã€ãã®éèªææ§ã«ãããããããã¯ããã«åçŽã§ãã次ã«ãçŸåšã®äœçœ®ã§ã¯ãªãdesiredPositionãæŽæ°ããããã«ãupdateã¡ãœããã«æ¬¡ã®å€æŽãå ããŸãã
// 'self.position = ccpAdd(self.position, stepVelocity);' : self.desiredPosition = ccpAdd(self.position, stepVelocity);
è¡çªãå§ããŸãããïŒ
çå£ãªææã®æãæ¥ãŸããã ããããã¹ãŠãŸãšããã€ããã§ãã GameLevelLayer.mã«æ¬¡ã®ã¡ãœãããè¿œå ããŸãã
ç§ãã¯ãªãã¯
- (void)checkForAndResolveCollisions:(Player *)p { NSArray *tiles = [self getSurroundingTilesAtPosition:p.position forLayer:walls ]; //1 for (NSDictionary *dic in tiles) { CGRect pRect = [p collisionBoundingBox]; //2 int gid = [[dic objectForKey:@"gid"] intValue]; //3 if (gid) { CGRect tileRect = CGRectMake([[dic objectForKey:@"x"] floatValue], [[dic objectForKey:@"y"] floatValue], map.tileSize.width, map.tileSize.height); //4 if (CGRectIntersectsRect(pRect, tileRect)) { CGRect intersection = CGRectIntersection(pRect, tileRect); //5 int tileIndx = [tiles indexOfObject:dic]; //6 if (tileIndx == 0) { // p.desiredPosition = ccp(p.desiredPosition.x, p.desiredPosition.y + intersection.size.height); } else if (tileIndx == 1) { // p.desiredPosition = ccp(p.desiredPosition.x, p.desiredPosition.y - intersection.size.height); } else if (tileIndx == 2) { // p.desiredPosition = ccp(p.desiredPosition.x + intersection.size.width, p.desiredPosition.y); } else if (tileIndx == 3) { // p.desiredPosition = ccp(p.desiredPosition.x - intersection.size.width, p.desiredPosition.y); } else { if (intersection.size.width > intersection.size.height) { //7 // , float intersectionHeight; if (tileIndx > 5) { intersectionHeight = intersection.size.height; } else { intersectionHeight = -intersection.size.height; } p.desiredPosition = ccp(p.desiredPosition.x, p.desiredPosition.y + intersection.size.height ); } else { // , float resolutionWidth; if (tileIndx == 6 || tileIndx == 4) { resolutionWidth = intersection.size.width; } else { resolutionWidth = -intersection.size.width; } p.desiredPosition = ccp(p.desiredPosition.x , p.desiredPosition.y + resolutionWidth); } } } } } p.position = p.desiredPosition; //7 }
ãããïŒæžããã°ããã®ã³ãŒããèŠãŠã¿ãŸãããã
1.æåã«ãã³ã¢ã©ãå²ãã»ã«ã®ã»ãããååŸããŸãã次ã«ããã®ã»ããã®åã»ã«ã埪ç°ããŸããã»ã«ãééãããã³ã«ãè¡çªããã§ãã¯ããŸããè¡çªãçºçããå ŽåãKoalaã®desiredPositionãå€æŽããŸãã
2.ã«ãŒãã®åã«ãŒãå ã§ãæåã«çŸåšã®Koalaãã¬ãŒã ãååŸããŸããè¡çªãæ€åºããããã³ã«ãdesiredPositionå€æ°ã¯ãã®å€ããè¡çªãçºçããªããªããã€ã³ãã«å€æŽããŸãã
3.次ã®ã¹ãããã¯ãNSDictionaryã«æ ŒçŽããGIDãååŸããããšã§ããããã¯ãŒãã®å ŽåããããŸãã GIDããŒãã®å ŽåãçŸåšã®ã«ãŒãã¯çµäºãã次ã®ã»ã«ã«é²ã¿ãŸãã
4ãã»ã«ãæ°ããäœçœ®ã«ããå ŽåãCGRectãååŸããå¿ èŠããããŸããè¡çªããå Žåãšããªãå ŽåããããŸãããã®ããã»ã¹ã次ã®ã³ãŒãè¡ã§å®è¡ããtileRectå€æ°ã«ä¿åããŸããCGRect Koalaãšã»ã«ãã§ããã®ã§ãè¡çªããã§ãã¯ã§ããŸãã
5.ã»ã«ã®è¡çªããã§ãã¯ããã«ã¯ãCGRectIntersectsRectãå®è¡ããŸããè¡çªãçºçããå ŽåãCGRectIntersectionïŒïŒé¢æ°ã䜿çšããŠãCGRect亀差ç¹ãèšè¿°ããCGRectãååŸããŸãã
ãžã¬ã³ãã«ã€ããŠè©³ããèŠãŠã¿ãŸããã...
ããªãèå³æ·±ãã±ãŒã¹ã§ããè¡çªãæ£ããèå¥ããæ¹æ³ãç解ããå¿ èŠããããŸãã
ã³ã¢ã©ãåããæè¯ã®æ¹æ³ã¯ãè¡çªãšã¯å察ã®æ¹åã«åããããšã ãšæããããããŸãããäžéšã®ç©çãšã³ãžã³ã¯ãã®åçã§å®éã«åäœããŸãããããè¯ãæ¹æ³ã§ãœãªã¥ãŒã·ã§ã³ãé©çšããŸãã
èããŠã¿ãŸãããïŒéåã¯ã³ã¢ã©ãåžžã«ãã®äžã®ã»ã«ã«åŒãå¯ããŠããããããã®è¡çªã¯é²è¡äžã§ããããªããã³ã¢ã©ãåé²ããã®ãæ³åãããªããåæã«ãã³ã¢ã«ã¯ãŸã éåã«ãã£ãŠåŒãäžããããŠããŸããåããå察æ¹åã«åçŽã«å€æŽããããšã§ãã®åé¡ã解決ããå Žåãã³ã¢ã©ã¯å·Šäžã«ç§»åããŸãããäœãä»ã®ãã®ãå¿ èŠã§ãïŒ
ã³ã¢ã©ã¯ããããã®ã»ã«ã®äžã«ãšã©ãŸãããã«ååãªè·é¢ã移åããå¿ èŠããããŸãããåãããŒã¹ã§åé²ãç¶ããŸãã

ã³ã¢ã©ãå£ã転ããèœã¡ãå Žåãåãåé¡ãçºçããŸãããã¬ã€ã€ãŒãã³ã¢ã©ãå£ã«æŒãä»ãããšãã³ã¢ã©ã®åãã®æãŸããè»éãæãäžåãã«å£ã«åããããŸããæ¹åãå€ããã ãã§ãã³ã¢ã©ãå£ããäžã«ç§»åãããŸããã³ã¢ã©ãå£ã®å€ã«æ®ãããããããã§ãåãããŒã¹ã§äžãã£ãŠã»ããïŒ

ãããã£ãŠãè¡çªãåçŽã«ããã€æ°Žå¹³ã«åŠçãããã決å®ããäž¡æ¹ã®ã¢ã¯ã·ã§ã³ãçžäºã«æä»çã«åŠçããå¿ èŠããããŸããäžéšã®ç©çãšã³ãžã³ã¯ãæåã«æåã®ã€ãã³ããåžžã«åŠçãã次ã«2çªç®ã®ã€ãã³ããåŠçããŸããããããã³ã¢ã©ã®ã»ã«ã®äœçœ®ã«åºã¥ããŠããè¯ã決å®ãããããšèããŠããŸãããããã£ãŠãããšãã°ãã»ã«ãã³ã¢ã©ã®çäžã«ããå Žåãã³ãªãžã§ã³èå¥åãã³ã¢ã©ãäžã«æ»ãããã«ããŸãã
ããããã»ã«ãã³ã¢ã©ã®äœçœ®ã«å¯ŸããŠæãã«ãªã£ãŠããå Žåã¯ã©ãã§ããããïŒãã®å ŽåãCGRect亀差ç¹ã䜿çšããŠãã³ã¢ã©ãã©ã®ããã«ç§»åããããç解ããŸãããã®é·æ¹åœ¢ã®å¹ ãé«ãããã倧ããå ŽåãKoalaãåçŽã«æ»ãå¿ èŠããããŸããé«ããå¹ ããã倧ããå Žåãã³ã¢ã©ã¯æ°Žå¹³æ¹åã«ç§»åããå¿ èŠããããŸãã

Koalaã®é床ãšãã¬ãŒã ã¬ãŒããç¹å®ã®ãã¬ãŒã ã¯ãŒã¯å ã«ããéãããã®ããã»ã¹ã¯æ£ããæ©èœããŸããå°ãåŸã§ãã³ã¢ã©ãèœäžããããŠã»ã«ãããæããã±ãŒã¹ãåé¿ããæ¹æ³ãåŠã³ãŸãã
Koalaã®ç§»åæ¹æ³ïŒåçŽãŸãã¯æ°Žå¹³ïŒã決å®ãããã亀差ç¹ã®CGRectãµã€ãºã䜿çšããŠãKoalaã移åããéã決å®ããŸããããããå¹ ãŸãã¯é«ããèŠãŠããã®å€§ãããã³ã¢ã©ã®å€äœè·é¢ãšããŠäœ¿çšããŸãã
ç¹å®ã®é åºã§ã»ã«ããã§ãã¯ããã®ã¯ãªãã§ããïŒåžžã«é£æ¥ããã»ã«ãæåã«åŠçãã次ã«æãã®ã»ã«ãåŠçããå¿ èŠããããŸããçµå±ãKoalaã®çäžã®ã»ã«ã®è¡çªããã§ãã¯ãããå Žåãå€äœãã¯ãã«ã¯åçŽã«åããããŸãã

ãã ããã³ã¢ã©ãã»ã«ã«ãããã«è§ŠãããšãCGRectã®è¡çªãäžæ¹ã«åºããå¯èœæ§ããããŸãã
å³åŽã®åçãèŠãŠãã ãããã³ãªãžã§ã³åè§åœ¢ã¯ã³ãªãžã§ã³å šäœã®ããäžéšã§ãããããéè²ã®é åãæ¡å€§ãããŸãããã ããã³ã¢ã©ã®çŽäžã®ã»ã«ã«é¢ããåé¡ããã§ã«è§£æ±ºããŠããå Žåã¯ãã³ã¢ã©ã®å³äžã®ã»ã«ãšã®è¡çªãæ€åºããå¿ èŠããªããªããŸãããããã£ãŠãæ°ããªåé¡ãåé¿ããŸãã
ã³ãŒãã«æ»ããŸãããïŒ
巚倧ãªã¡ãœããcheckForAndResolveCollisionsã«æ»ããŸãããïŒ ...
6. 6çªç®ã®ã»ã¯ã·ã§ã³ã§ã¯ãçŸåšã®ã»ã«ã®ã€ã³ããã¯ã¹ãååŸã§ããŸããã»ã«ã®ã€ã³ããã¯ã¹ã䜿çšããŠãã»ã«ã®äœçœ®ãååŸããŸããé£æ¥ããã»ã«ãåå¥ã«æäœããŠãã³ã¢ã©ãã·ããããè¡çªã®é·ããŸãã¯é«ããæžç®ãŸãã¯å ç®ããŸãããšãŠãç°¡åã§ãããã ãã察è§ã»ã«ã«ãªããšããã«ãåã®ã»ã¯ã·ã§ã³ã§èª¬æããã¢ã«ãŽãªãºã ãé©çšããŸãã
7. 7çªç®ã®ã»ã¯ã·ã§ã³ã§ã¯ãè¡çªé åãåºãã现é·ãããå€æããŸããïŒå¹ ãåºãå Žå-åçŽã«äœæ¥ããŸããã»ã«ã€ã³ããã¯ã¹ã5ãã倧ããå Žåãã³ã¢ã©ãäžã«ç§»åããŸããé åãäžæ¹åã«åŒã䌞ã°ãããŠããå Žåãæ°Žå¹³æ¹åã«äœæ¥ããŸããã»ã«ã€ã³ããã¯ã¹ã®é åºã«ã€ããŠããåæ§ã®ååã«åºã¥ããŠè¡åããŸããæåŸã«ãåãåã£ãããžã·ã§ã³ãã³ã¢ã¬ã«å²ãåœãŠãŸãã
ãã®æ¹æ³ã¯ãè¡çªæ€åºã·ã¹ãã ã®é è³ã§ãã
å®éã«å©çšå¯èœãªç¥èããã¹ãŠæŽ»çšããŸãããïŒæ¹æ³ã«å€æŽããæŽæ°ãïŒãŸã ã§GameLevelLayerïŒ ïŒ
// "[self getSurroundingTilesAtPosition:player.position forLayer:walls];" : [self checkForAndResolveCollisions:player];
getSurroundingTilesAtPositionïŒforLayerãããã¯ãåé€ãŸãã¯ã³ã¡ã³ãã¢ãŠãããããšãã§ããŸãã
/* for (NSDictionary *d in gids) { NSLog(@"%@", d); } //8 */
çºå°ïŒçµæã«é©ããïŒ

ããŒã«ã¯ã³ã¢ãªãªãæ¢ããŸãããããã«åœŒã«inããŸãïŒãªãã§ïŒ
ç§ãã¡ãéãããã®ãæšæž¬ã§ããŸããïŒèŠããŠãããŠãã ãã-ã²ãŒã ã®ãã¹ãŠã®ã¹ãããã§ãã³ã¢ã©ã®é床ã«éåãå ãããŸããããã¯ãã³ã¢ã©ã絶ããã¹ããŒããèœãšããŠããããšãæå³ããŸãã
ã»ã«ã®ãµã€ãºã«ãªããŸã§ã³ã¢ã©ã®è»è·¡ã«é床ã絶ããè¿œå ããŸãã1ã¹ãããã§ã»ã«å šäœã移åãããšãåé¡ãçºçããŸãïŒæè¿ããã®ããšã«ã€ããŠè©±ããŸããïŒã
è¡çªãæ€åºãããããã«ãåºäŒã£ãã»ã«ã®æ¹åã«ã³ã¢ã©ã®é床ããŒãã«ããå¿ èŠããããŸãïŒã³ã¢ã©ã¯åããæ¢ããã®ã§ãé床ãèæ ®ããªããã°ãªããŸããã
ãããè¡ããªããšãã²ãŒã ã®åäœãããªãå¥åŠã«ãªããŸããåã«è¿°ã¹ãããã«ãã³ã¢ã©ãããã«é«ããžã£ã³ãã§ããªãããã«ãã³ã¢ã©ãå°é¢ã«è§ŠããŠãããã©ãããå€æããæ¹æ³ãå¿ èŠã§ããããã§ãã®ããã¯ã¹ããã§ãã¯ããŸãã以äžã®è¡ãcheckForAndResolveCollisionsã«è¿œå ããŸãã
ç§ãã¯ãªãã¯
- (void)checkForAndResolveCollisions:(Player *)p { NSArray *tiles = [self getSurroundingTilesAtPosition:p.position forLayer:walls ]; //1 p.onGround = NO; ////// for (NSDictionary *dic in tiles) { CGRect pRect = [p collisionBoundingBox]; //3 int gid = [[dic objectForKey:@"gid"] intValue]; //4 if (gid) { CGRect tileRect = CGRectMake([[dic objectForKey:@"x"] floatValue], [[dic objectForKey:@"y"] floatValue], map.tileSize.width, map.tileSize.height); //5 if (CGRectIntersectsRect(pRect, tileRect)) { CGRect intersection = CGRectIntersection(pRect, tileRect); int tileIndx = [tiles indexOfObject:dic]; if (tileIndx == 0) { // p.desiredPosition = ccp(p.desiredPosition.x, p.desiredPosition.y + intersection.size.height); p.velocity = ccp(p.velocity.x, 0.0); ////// p.onGround = YES; ////// } else if (tileIndx == 1) { // p.desiredPosition = ccp(p.desiredPosition.x, p.desiredPosition.y - intersection.size.height); p.velocity = ccp(p.velocity.x, 0.0); ////// } else if (tileIndx == 2) { // p.desiredPosition = ccp(p.desiredPosition.x + intersection.size.width, p.desiredPosition.y); } else if (tileIndx == 3) { // p.desiredPosition = ccp(p.desiredPosition.x - intersection.size.width, p.desiredPosition.y); } else { if (intersection.size.width > intersection.size.height) { //tile is diagonal, but resolving collision vertially p.velocity = ccp(p.velocity.x, 0.0); ////// float resolutionHeight; if (tileIndx > 5) { resolutionHeight = intersection.size.height; p.onGround = YES; ////// } else { resolutionHeight = -intersection.size.height; } p.desiredPosition = ccp(p.desiredPosition.x, p.desiredPosition.y + resolutionHeight); } else { float resolutionWidth; if (tileIndx == 6 || tileIndx == 4) { resolutionWidth = intersection.size.width; } else { resolutionWidth = -intersection.size.width; } p.desiredPosition = ccp(p.desiredPosition.x + resolutionWidth, p.desiredPosition.y); } } } } } p.position = p.desiredPosition; //8 }
ã³ã¢ã©ã®äžïŒé£æ¥ãŸãã¯å¯Ÿè§ç·ïŒã«ã»ã«ããããã³ã«ãp.onGroundå€æ°ã®å€ãYESã«èšå®ããé床ããŒãã«ããŸãããŸããã³ã¢ã©ã®äžã«é£æ¥ããã»ã«ãããå Žåããã®é床ããªã»ããããŸããããã«ãããçŸåšã®ã³ã¢ã©ã®é床ã«æ£ãã察å¿ããããšãã§ããŸãã
ã«ãŒãã®éå§æã«onGroundå€æ°ãNOã«èšå®ããŸãããã®å ŽåãonGroundã¯ããã®äžã®ã»ã«ãšã®ã³ã¢ã©ã®è¡çªãæ€åºãããšãã«ã®ã¿YESã«ãªããŸãããã®æ©èœã䜿çšããŠãçŸæç¹ã§ã³ã¢ã©ããžã£ã³ãã§ãããã©ãããå€æã§ããŸããPlayer.h
ã®ããããŒãã¡ã€ã«ã«æ¬¡ã®ã³ãŒããè¿œå ããŸãïŒãããŠããšã°ãŒã¯ãã£ãã§å¿ èŠãªãã®ããã¹ãŠåæããŸãïŒã
@property (nonatomic, assign) BOOL onGround;
ãããŠPlayer.mã§ïŒ
@synthesize onGround = _onGround;
çºå°ïŒ ãã¹ãŠãæå³ãããšããã«æ©èœããŸããïŒ ã¯ãïŒ ããããã®è±ªè¯ãªæ¥ïŒ ãã£ãïŒ

次ã¯ïŒ
ããã§ãšãããããŸãïŒç©çãšã³ãžã³ãå®å šã«å®æããŸããïŒãã®ããã¹ãã«å°éããããå®reliefã®ããæ¯ãã€ãããšãã§ããŸããé£ããéšåã§ãã-ãã¥ãŒããªã¢ã«ã®2çªç®ã®éšåã§ã¯è€éãªããšã¯äœããããŸããã
ãããŠãããã«ç§ãã¡ãå®äºãããããžã§ã¯ãã®ãœãŒã¹ããããŸãã
ã§ã¯ç¬¬äºéšç§ãã¡ã®ã³ã¢ã©ãå®è¡ãããŠãããšãžã£ã³ãã«ãªããŸãããŸããåºã«æ£ãã°ãããããããã¯ãã³ã¢ã©ã«ãšã£ãŠå±éºãªãã®ã«ããåã¡è² ãã®ç»é¢ãäœæããŸãã
ãã©ãããã©ãŒããŒã®ç©çãšã³ãžã³ã«ã€ããŠããã«è©³ããç¥ãããå Žåã¯ã次ã®ãªãœãŒã¹ã«ã¢ã¯ã»ã¹ããããšããå§ãããŸã
ãThe Sonic the Hedgehog Wikiã¯ãSonicãåºäœçŽ°èãšçžäºäœçšããæ¹æ³ã®åªãã説æã§ãã
ããããæé«é«æ¬¡ãã¡ã³ãã©ãããã©ãŒã ã¬ã€ãã
翻蚳è ã®ã¡ã¢
ãã®ãã¥ãŒããªã¢ã«ã«è§ŠçºãããŠããã®ãã¥ãŒããªã¢ã«ã翻蚳ããããšã«ããŸããã
ç§èªèº«ã¯çŸåšiOSã§ãã©ãããã©ãŒã ã²ãŒã ãæžããŠãããraywenderlich.comã®ãã¥ãŒããªã¢ã«ãç©æ¥µçã«äœ¿çšããŠããŸããçããã«ã¢ããã€ã¹ããŸãïŒ
æåã®éšåã®ç¿»èš³ã«æ¯èŒçé·ãæéãè²»ãããåŸã2çªç®ã®éšåã®ç¿»èš³ã«ã€ããŠèããŸãããå¿ èŠã«å¿ããŠã³ã¡ã³ããèšå ¥ããŠãã ãããéèŠãããã°ã翻蚳ããŸãã
çºèŠããããã¹ãŠã®äžæ£ç¢ºããšã¿ã€ããã¹ã«ã€ããŠã¯ãHabrahtaãŸãã¯ããã®ã³ã¡ã³ãã«èšå ¥ããŠãã ããã
ãã¥ãŒããªã¢ã«ã«é¢ãããã¹ãŠã®è³ªåã«åãã§ãçãããŸãïŒ