Farseer physics + cocos2d-xna

Topics: Developer Forum, User Forum
Feb 8, 2013 at 1:06 PM
Is that connection possible ?

I'm trying to set CCSprite as Body and it wont work. My point is to do Linera Velocity but it wont detect CCSprite, if i try to change body position nothing happens.
world = new World(Vector2.Zero);

var stone = CCSprite.spriteWithFile("images/1");
addChild(stone);

Body body = BodyFactory.CreateBody(world, new Vector2(100, 100), stone);

body.BodyType = BodyType.Dynamic;
body.LinearVelocity = new Vector2(0, 100);

var fixture = body.CreateFixture(new CircleShape(25, 1));

world.BodyList.Add(body);
Coordinator
Feb 10, 2013 at 2:48 PM
Try this code instead:
world = new World(Vector2.Zero);

var stone = CCSprite.spriteWithFile("images/1");
addChild(stone);

Body body = BodyFactory.CreateCircle(world, 25, 1, new Vector2(100, 100), stone);
body.BodyType = BodyType.Dynamic;
body.ApplyLinearImpulse(new Vector2(0, 100));
This should add the body to the world correctly.
Feb 18, 2013 at 12:36 PM
Thanks for your response and sorry for my late response.

It won't works for me, maybe im still doing something wrong.
public Name()
{
            CCSprite board = CCSprite.spriteWithFile("images/board");
            board.contentSize = new CCSize(480, 2048);
            board.position = new CCPoint(board.contentSize.width / 2, board.contentSize.height / 2);
            this.addChild(board);

            world = new World(Vector2.Zero);

            var stone = CCSprite.spriteWithFile("images/1");
            addChild(stone);

            Body body = BodyFactory.CreateCircle(world, 25, 1, new Vector2(100, 100), stone);
            body.BodyType = BodyType.Dynamic;
            body.ApplyLinearImpulse(new Vector2(0, 100));

            this.schedule(update);
}

public override void update(float dt)
{
            world.Step(0.3333f);
            base.update(dt);
}
Always having the same ... :

https://dl.dropbox.com/u/32453794/screen.png

and it won't go up
Coordinator
Feb 18, 2013 at 7:45 PM
I've never worked with cocos2d-xna, but it is certainly possibly to use it, as Farseer only handles physics, and really does not care about graphics.
Can you get a sprite/texture moving on the screen? You need a drawing method somewhere, where you read the position and rotation of the body, and draw the sprite accordingly.

Also, remember that Farseer works in meters, not pixels. You have to define a meter to pixels ratio (such as 1 meter = 20 pixels) and convert between the two, in order to get realistic physics.
Feb 20, 2013 at 10:28 AM
Ok, so which code should i put in draw method ?

My class is inheriting from CCLayerColor so i want to put it into scene in appdelegate.cs
CCScene pScene = new StartMenu();
var layer = new Nowa();
//var pLayer = new Scenka();
pScene.addChild(layer);
pDirector.runWithScene(pScene);
And the rest is the same as post up, ive added overriden method draw but dont know what but inside
class Nowa : CCLayerColor
{
        World world;

        public Nowa()
        {
            CCSprite board = CCSprite.spriteWithFile("images/board");
            board.contentSize = new CCSize(480, 2048);
            board.position = new CCPoint(board.contentSize.width / 2, board.contentSize.height / 2);
            this.addChild(board);

            world = new World(Vector2.Zero);

            var stone = CCSprite.spriteWithFile("images/1");
            addChild(stone);

            Body body = BodyFactory.CreateCircle(world, 25, 1, new Vector2(100, 100), stone);
            body.BodyType = BodyType.Dynamic;

            this.schedule(update);
        }

        public override void draw()
        {
            base.draw();
        }

        public override void update(float dt)
        {
            world.Step(0.3333f);
            base.update(dt);
        }
}