FlashPunk grid and Nape?


(Oli) #1

Does anyone have experience with implementing Nape physics engine in flashpunk. What i am trying to do is add a nape body to my flashpunk grid made in Ogmo. I thought of adding 32 by 32 square for every “1” in the grid data but Im not sure how to get the location of each. from the loadfromstring method?

public class Solid extends Entity {

  public var border:Body = new Body(BodyType.STATIC);
   public function Solid(x:int, y:int, image:Graphic, grid:Grid)   

{

       mask = grid;
      graphic = image;
      type = "Solid";
       //Nape body                        
       border.shapes.add(new Polygon(Polygon.rect(0, 0, 32, 32)));
       border.space = theGameWorld.space;
   }
           override public function update():void
   {
       x = border.position.x;
       y = border.position.y;
   }

}


(Mike Evmm) #2

You have it the other way round:

override public function update():void
{
   x = border.position.x;
   y = border.position.y;
}

should be

override public function update():void
{
  border.position.x = x + halfWidth;
   border.position.y = y + halfHeight;
}

Since you are making the Nape object obey the FP coordinates. Also Nape uses the center of the object and FP uses the top left corner for coordinate reference, so you should compensate for that by adding halfWidth/halfHeight.


(Oli) #3

oh kk thx, but would u by chance know how to go about distributing each of these nape squares to my FP grid?


(Mike Evmm) #4

You could iterate through your grid and add a square body the size of a tile if that position is solid.


(Oli) #5

with a for each loop? How woudl u do it?


(Oli) #6
        for(var i:int = 0; i < grid.height; i+32)
        {
            
        for (var j:int = 0; j < grid.width; j+32) 
        {
        
        if (grid.getTile(i, j) == true)
        {
        //Nape body                        
            var border:Body = new Body(BodyType.STATIC);
            border.shapes.add(new Polygon(Polygon.rect(i, j, 32, 32)));
            border.space = theGameWorld.space;
        }
        
        }
        
        }

This dosent work:(


(Mike Evmm) #7

You have i and j mixed up. (@ border.shapes.add(new Polygon(Polygon.rect(i, j, 32, 32)));)


(Oli) #9

Would you know how to update the body location with the camera?


(Mike Evmm) #10

How so? (CCCharlimitcharlimitcha)


(Oli) #11

nevermind, I kind of have it working using this

    override public function update():void

    {
                    border.position.x = x - FP.camera.x
        border.position.y = y - FP.camera.y
       

    }

altough other bodies wont collide with it for some reason:(


(Mike Evmm) #12

I was about to reply with that solution. What do you mean not collide?


(Oli) #13

it just go through them, then when it reaches the end of the screen the programm crashes.


(Mike Evmm) #14

That’s strange. Are you sure the bodies are in the correct position? It doesn’t look like it from the screenshot


(Oli) #15

yea but u know man im kind of giving up lol. this is not my level i guess. ill post my source code if your interested.


(Mike Evmm) #16

I had so much trouble using Nape with FP that I temporarily changed to Flixel, so I feel you


(Oli) #17

(Hussein Tammam) #18

Does the game need to be tile based? If it doesnt you could just use resizable entities in ogmo and create a body depending on the width and height of the arbitrarily sized entity. it beats having hundreds of bodies for the ground and walls.


(Oli) #19

yea I guess so:) Altough it would have been much simple for level editin


(Hussein Tammam) #20

Hmmmm I dont see any reason why the bodies should be tile based? Nape handles all collisions by default, there is really no reason to be using a flashpunk grid.


(Mike Evmm) #21

Ogmo editor makes it a pain to work with resizable entities.