SetAsEdge problems Cocos 2D - iphone

I am trying to make a game in Cocos 2D & Box 2D
but when i try to put a "setAsEdge" it just said me that the function dosn't exist
my code:
// Define the ground body.
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0, 0); // bottom-left corner
// Call the body factory which allocates memory for the ground body
// from a pool and creates the ground box shape (also from a pool).
// The body is also added to the world.
groundBody = world->CreateBody(&groundBodyDef);
// Define the ground box shape.
b2PolygonShape groundBox;
// bottom
groundBox.SetAsEdge(b2Vec2(0,FLOOR_HEIGTH/PTM_RATIO), b2Vec2(screenSize.width*2.0f/PTM_RATIO,FLOOR_HEIGTH/PTM_RATIO));
groundBody->CreateFixture(&groundBox,0);
// top
groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width*2.0f/PTM_RATIO,screenSize.height/PTM_RATIO));
groundBody->CreateFixture(&groundBox,0);
// left
groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(0,0));
groundBody->CreateFixture(&groundBox,0);
// right
groundBox.SetAsEdge(b2Vec2(screenSize.width*2.0f/PTM_RATIO,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width*2.0f/PTM_RATIO,0));
groundBody->CreateFixture(&groundBox,0);
Error: No member named 'SetAsEdge' in 'b2PolygonShape'. I am using Cocos 2D 1.0.1 Stable
Thanks to everyone

You are working with Box2D 2.2.1 whose API has changed compared to the Box2D version in Cocos2D v1.0.1 (uses Box2D v2.1.2). I take it you're using Cocos2D 2.0 alpha? If so, it supposedly comes with updated Xcode project templates, so you might want to install these and create a project based on the new templates.
Here's how I set the border bounding box by using a body with 4 shapes attached to it:
// for the screenBorder body we'll need these values
CGSize screenSize = [CCDirector sharedDirector].winSize;
float widthInMeters = screenSize.width / PTM_RATIO;
float heightInMeters = screenSize.height / PTM_RATIO;
b2Vec2 lowerLeftCorner = b2Vec2(0, 0);
b2Vec2 lowerRightCorner = b2Vec2(widthInMeters, 0);
b2Vec2 upperLeftCorner = b2Vec2(0, heightInMeters);
b2Vec2 upperRightCorner = b2Vec2(widthInMeters, heightInMeters);
// Define the static container body, which will provide the collisions at screen borders.
b2BodyDef screenBorderDef;
screenBorderDef.position.Set(0, 0);
b2Body* screenBorderBody = world->CreateBody(&screenBorderDef);
b2EdgeShape screenBorderShape;
// Create fixtures for the four borders (the border shape is re-used)
screenBorderShape.Set(lowerLeftCorner, lowerRightCorner);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
screenBorderShape.Set(lowerRightCorner, upperRightCorner);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
screenBorderShape.Set(upperRightCorner, upperLeftCorner);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
screenBorderShape.Set(upperLeftCorner, lowerLeftCorner);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
Alternatively, get Kobold2D Preview 5 (available by tomorrow) and have a look at the Physics-Box2D template project. I made all the necessary changes to run a Box2D v2.2.1 app, including the changes needed for the Box2D debug drawing class. I'm also hosting the latest Box2D API reference.

Use b2EdgeShape instead of b2PolygonShape, for example:
b2EdgeShape groundBox;
groundBox.Set( b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO,0));

use
setAsBox(0,0,b2vec2(0,0),0);

Related

Unity 3D: draw line behind game object/record path taken by game object?

I am creating a VR app in Unity3d using Google cardboard and need to know how to record the path taken by the player (they are traversing a maze). Is there a way of drawing the path taken by the user (possibly in the console; not in the actual game and not visible to the user) and saving this path as an image?
I need to save an image or just a line of where the player went in the game so that I can then email this image/data to the player.. What is the best way to accomplish this?
You need to store the Player's path in List as Vector3. Then you can use LineRenderer to draw the line.change the vertext amount of the LineRenderer to List.Count with LineRenderer.SetVertexCount then loop over the List and change the position of the LineRenderer with LineRenderer.SetPosition(loopIndex,playersPo[loopIndex]).
List<Vector3> playerPos = new List<Vector3>();
//Store players positions somewhere
//playerPos.Add(pPos);
//playerPos.Add(pPos);
//playerPos.Add(pPos);
Color red = Color.red;
LineRenderer lineRenderer = gameObject.AddComponent<LineRenderer>();
lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
lineRenderer.SetColors(red, red);
lineRenderer.SetWidth(0.2F, 0.2F);
//Change how mant points based on the mount of positions is the List
lineRenderer.SetVertexCount(playerPos.Count);
for (int i = 0; i < playerPos.Count; i++ )
{
//Change the postion of the lines
lineRenderer.SetPosition(i, playerPos[i]);
}

How to set position of 3D model on center AR Camera?

My application using Unity And Vuforia. I want set position of 3D model of tracked found target to center Screen and AR Camera after track lost. I mean that I want to show lost Image target on center position.
void centerGameObject(GameObject gameOBJToCenter, Camera cameraToCenterOBjectTo, float zOffset = 2.6f)
{
gameOBJToCenter.transform.position = cameraToCenterOBjectTo.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, cameraToCenterOBjectTo.nearClipPlane + zOffset));
}
Then you can call it with
centerGameObject(gameOBJ, Camera.main);
The default zoffset(2.6f) should work but you can change it by supplying the third parameter.
centerGameObject(gameOBJ, Camera.main, 6f);

SpriteKit - How do I check if a certain set of coordinates are inside an SKShapeNode?

In my game, I'm trying to determine what points to dole out depending on where an arrow hits a target. I've got the physics and collisions worked out and I've decided to draw several nested circular SKShapeNodes to represent the different rings of the target.
I'm just having issues working out the logic involved in checking if the contact point coordinates are in one of the circle nodes...
Is it even possible?
The easiest solution specific to Sprite Kit is to use the SKPhysicsWorld method bodyAtPoint:, assuming all of the SKShapeNode also have an appropriate SKPhysicsBody.
For example:
SKPhysicsBody* body = [self.scene.physicsWorld bodyAtPoint:CGPointMake(100, 200)];
if (body != nil)
{
// your cat content here ...
}
If there could be overlapping bodies at the same point you can enumerate them with enumerateBodiesAtPoint:usingBlock:
You can also compare the SKShapeNode's path with your CGPoint.
SKShapeNode node; // let there be your node
CGPoint point; // let there be your point
if (CGPathContainsPoint(node.path, NULL, point, NO)) {
// yepp, that point is inside of that shape
}

Chipmunk 6 space initialization

I am trying to use chipmunk6.x and having problems with it. It is not creating the physics environment. How do I enable the chipmunk6.x physics environment in my project? The space manager system is working well. I hope there is no problem with the chipmunk lib attachment.
I am using the cocos2d old version I just replaced the chipmunk lib.
space = cpSpaceNew();
space->gravity = cpv(0, -100);
//
// rogue shapes
// We have to free them manually
//
// bottom
cpShape *walls_[4];
walls_[0] = cpSegmentShapeNew( space->staticBody, cpv(0,0), cpv(s.width,0), 0.0f);
// top
walls_[1] = cpSegmentShapeNew( space->staticBody, cpv(0,s.height), cpv(s.width,s.height), 0.0f);
// left
walls_[2] = cpSegmentShapeNew( space->staticBody, cpv(0,0), cpv(0,s.height), 0.0f);
// right
walls_[3] = cpSegmentShapeNew( space->staticBody, cpv(s.width,0), cpv(s.width,s.height), 0.0f);
for( int i=0;i<4;i++) {
walls_[i]->e = 1.0f;
walls_[i]->u = 1.0f;
cpSpaceAddStaticShape(space, walls_[i] );
}
You need to be more specific than "It is not creating the physics environment."
The code you pasted otherwise looks fine. What happens with it, and what are you expecting should happen.

Box2d sensor rotation problem

in my app the user is able to move or rotate a box2d fixture. The collision detection works fine. But if I set the fixture as a sensor, with the SetSensor(true) method, the collision detection reacts weird.
In the following picture the red rectangle is the sensor, but a collision is also detected if an other fixture collides with the black border.
Image: http://img851.imageshack.us/img851/7292/rect.png
Is it possible that only the red rectangle reacts as a sensor?
BR
I found the help here:
Box2D Forum
I just forgot to check if the contact is touching (with the IsTouching() method)! Now it works fine!!!
b2BodyDef bodyDef;
bodyDef.type = b2_staticBody;
bodyDef.position.Set(position.x/PTM_RATIO, position.y/PTM_RATIO);
bodyDef.userData = NULL;
b2Body *body = _game.world->CreateBody(&bodyDef);
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(size.x/2/PTM_RATIO, size.y/2/PTM_RATIO);
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.isSensor = true;
body->CreateFixture(&fixtureDef);
body->SetTransform(body->GetPosition(), rotatingAngle);