{"id":18271522,"url":"https://github.com/rolandoam/CCPhysics","last_synced_at":"2025-04-05T02:30:32.814Z","repository":{"id":1146922,"uuid":"1029588","full_name":"rolandoam/CCPhysics","owner":"rolandoam","description":"simple extensible physics/collision detection engine for cocos2d","archived":false,"fork":false,"pushed_at":"2010-10-30T18:34:29.000Z","size":105,"stargazers_count":28,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-05T11:53:58.643Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://www.gamesforfood.com","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rolandoam.png","metadata":{"files":{"readme":"README.rdoc","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2010-10-27T18:57:43.000Z","updated_at":"2024-06-13T09:31:00.000Z","dependencies_parsed_at":"2022-08-16T12:15:22.089Z","dependency_job_id":null,"html_url":"https://github.com/rolandoam/CCPhysics","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rolandoam%2FCCPhysics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rolandoam%2FCCPhysics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rolandoam%2FCCPhysics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rolandoam%2FCCPhysics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rolandoam","download_url":"https://codeload.github.com/rolandoam/CCPhysics/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247279172,"owners_count":20912847,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-05T11:39:21.666Z","updated_at":"2025-04-05T02:30:32.478Z","avatar_url":"https://github.com/rolandoam.png","language":"Objective-C","funding_links":[],"categories":["etc"],"sub_categories":[],"readme":"= CCPhysics for cocos2d\n\nSimple - really simple! - physics and collision detection engine inspired on the articles about [[N physics|http://www.metanetsoftware.com/technique.html]].\n\nYou should add CCPhysicsContainer.(m|h) and CCPhysicsShape.(m|h) to your project and start hacking around!\n\nNOTE: This is a Work In Progress, and definitively not ready for production. Use carefully! :-)\n\n== Really quick intro\n\n```obj-c\n- (id)init {\n\n\t...\n\t// will add a container with the director's window as bounds\n\t// you can also call [CCPhysicsContainer containerWithBounds:bounds]\n\tCCPhysicsContainer *c = [CCPhysicsContainer node];\n\tfor (NSUInteger i=0; i \u003c 15; i++) {\n\t\tNSInteger shape = random() % 3 + 1;\n\t\tNSInteger cellY = random() % 10;\n\t\tCCPhysicsShape *t = [CCPhysicsShape shapeWithCollisionType:shape];\n\t\t// the object will be centered in the cell, but it could be placed anywhere\n\t\tt.position = ccp(i*32 + 16, cellY * 32 + 16);\n\t\t[c addChild:t];\n\t}\n\t[self addChild:t z:0 tag:1];\n\n\t...\n\n\t// schedule update function\n\t[self schedule:@selector(tick:)];\n}\n\n- (void)tick:(ccTime)delta {\n\tCCPhysicsContainer *c = (CCPhysicsContainer *)[self getChildByTag:1];\n\t[c simulate];\n}\n```\n\nFor every CCPhysicsDynamicShape in the space (dynamic shapes do not collide with each other. This might change in the future), you need to provide an update method. Otherwise they would just \"fall\" into because of gravity.\n\nIf your Hero object (in LevelSVG parlance) is a CCPhysicsDynamicShape - and it should be dynamic, otherwise it would be static! - then you might need to add the movement logic in the update method. Check the FAQ for a simple example.\n\n== CURRENT AVAILABLE SHAPES\n\n```c\n\ttypedef enum {\n\t\tShapeSquare     = 1,\n\t\tShapeHalfSquare, // square of 16x16\n\t\tShapeRect_32x14, // rectangle of 32x14\n\t\tShapeRect_32x20, // rectangle of 32x20\n\t\t// The following shapes are not implemented (yet)\n\t\tShapeCircle,     // circle of 16px of radius\n\t\tShapeTR_L45,     // right 45deg triangle\n\t\tShapeTR_R45,     // left 45deg triangle\n\t} CollisionShapeType;\n```\n\nThese shapes are defined to that way because the basic \"tile\" is 32x32 (the grid for the spatial hash). Please notice that you can still add any kind of rectangle shape you want, given that they are AABB (axis aligned bounded box, that is, the \"lines\" of the box are aligned with the x and y axis).\n\nPlease notice however, that due to constrictions of the engine, the ideal rectangle shape would not take more than two tiles wide. This is to keep things simple and fast.\n\nIf you want to add a shape that's wider than that, you would also need to modify the method that adds the object to the grid (right now I'm just checking the corners, assuming that no shape is bigger than 32px wide)\n\nAnother thing to keep in mind: if you add a sprite, make sure that the sprite has an anchor of (0.5,0.5) - the default anchor - this is because the engine assumes that the current position of an object is its center.\n\n== FAQ\n\nQ:: Hey! where do I define the mass of my objects?\nA:: This is not a *real* physics engine. It's intended to be fake, so objects do not weight. You can simulate it by changing the integrate method of a dynamic object. By default it will be something like this:\n\n```obj-c\n- (void)integrate {\n\tCGPoint p = position_;\n\tCGPoint o = oldPos_;\n\n\tNSAssert(collisionType_, @\"No collision type!!\");\n\toldPos_ = position_;\n\t// integrate\n\tp.x += (DRAG * p.x) - (DRAG * o.x) + gravity_.x;\n\tp.y += (DRAG * p.y) - (DRAG * o.y) + gravity_.y;\n\n\t// we need to call CCNode's set position\n\t[self setPosition:p];\n}\n```\n\nThat is, a simple verlet integration.\n\nQ:: What about rotation?\nA:: Again, *not* a real physics engine. You can do that yourself overriding the integrate function.\n-\n\nQ:: How can I move my CCPhysicsDynamicShape?\nA:: I would suggest you to create a new subclass of CCPhysicsDynamicShape, and do something like this:\n\n```obj-c\n// first, just mark the movement (move left as long as the flag is set)\n// for instance, set it on touchBegan, clear it on touchEnded\n- (void)moveLeft {\n\tmovement_ |= SpermMoveLeft;\n}\n\n// this is called once on every simulation\n- (void)update {\n\t// move\n\tCGPoint p = position_;\n\tCGPoint o = oldPos_;\n\tCGPoint v = ccpSub(p, o);\n\tCGPoint f = CGPointZero;\n\tif (movement_ \u0026 SpermMoveLeft) {\n\t\tf.x = -OBJSPEED;\n\t} else if (movement_ \u0026 SpermMoveRight) {\n\t\tf.x = OBJSPEED;\n\t}\n\n\t// this will add \"f\" to the current speed, with a max speed of MAXSPEED\n\tif (f.x != 0 || f.y != 0) {\n\t\tCGPoint np = ccp(MIN(MAXSPEED, MAX(-MAXSPEED, v.x + f.x)), MIN(MAXJSPEED, MAX(-MAXJSPEED, v.y + f.y)));\n\t\tp = ccpAdd(o, np);\n\t\tposition_ = p;\n\t}\n}\n```\n\nOf course, everything will depend on your actual code and what you really want to do.\n\n== TODO\n\n* add support for CCBatchNode inside CCPhysicsContainer\n* add the rest of the shapes: (semi)circle, triangles, slopes, etc.\n* provide a simple example project\n\n== THANKS\n\nMany, many thanks to the guys from whom I \"inspired\" (read: ripped off) most of the ideas.\n\nAlso, many thanks to riq from Cocos2D-iPhone for inspiring us in doing great games!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frolandoam%2FCCPhysics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frolandoam%2FCCPhysics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frolandoam%2FCCPhysics/lists"}