Getting started with haxeflixel and nape



  • I've looked high and low for any information on how the classes used to implement nape in haxeflixel are suposed to be used but can't find anything. I've read the nape manual and understand how it works.

    What I'm trying to do right now is add two FlxNapeSprites one with a kinematic body and one with a static body. the kinematic body was easy but whenever I try to add the static one I get runtime errors (namely: Cannot modifiy shapes of static object once added to Space). If anyone could provide an example of code that does this with a breif explanation that would be really helpful.



  • Could you post a snippet (maybe where you create the body)? I was playing around with Nape once and also ran in the same problem.



  • package;
    
    import flixel.FlxG;
    import flixel.FlxSprite;
    import flixel.FlxState;
    import flixel.addons.nape.FlxNapeSpace;
    import flixel.addons.nape.FlxNapeSprite;
    import flixel.text.FlxText;
    import flixel.ui.FlxButton;
    import flixel.math.FlxMath;
    import flixel.util.FlxColor;
    import nape.phys.BodyType;
    
    class MenuState extends FlxState
    {
    	public var kinematicObject:FlxNapeSprite;
    	public var staticObject:FlxNapeSprite;
    	
    	override public function create():Void
    	{
    		super.create();
    		
    		//init the nape space
    		FlxNapeSpace.init();
    		
    		//create the kinematic body
    		kinematicObject = new FlxNapeSprite(0, 0,false);
    		kinematicObject.makeGraphic(20, 20, FlxColor.BLUE);
    		kinematicObject.createRectangularBody(20, 20, BodyType.KINEMATIC);
    		add(kinematicObject);
    		
    		//create the static body
    		staticObject = new FlxNapeSprite(30, 0, false);
    		staticObject.makeGraphic(20, 20, FlxColor.RED);
    		staticObject.createRectangularBody(20, 20, BodyType.STATIC); //this line throws the error
    		add(staticObject);
    	}
    }
    

    This is the most basic form of what I'm trying to do.



  • @Grimmr I haven't worked on my Nape-based project in a while, but here's a snippet from it where I create a static object:

    		var newWall:Body = new Body(BodyType.STATIC);
    		newWall.shapes.add(new Polygon(Polygon.rect( X, -_upperExtension, width, _gameBottom + _upperExtension )));
    		newWall.space = FlxNapeSpace.space;
    

    Maybe you can't use createRectangularBody with STATIC, but have to do things manually? (If not, I'm not sure why else I would have done things manually there--I use createRectangularBody elsewhere in my code, and this is a rectangle I'm adding...)



  • @IBwWG thank you for this I won't be able to try and recreate it until tomorrow, but manually adding polygons actually makes sense when combined with an invalid polygon error I got when I was tinkering around. I'll see how it goes and report back.



  • @IBwWG Implementing your code has solved that rather cryptic issue. Now my only concern is that I get a weird vibration effect. Whenever a dynamic body collides with a static body it will start to vibrate untill it stops trying to move. I'm currently looking into nape to try and solve this.

    Thanks for providing some example source though it was a massive help.



  • @Grimmr No problem, although AFAICT that vibration effect seems to be a general problem with tweaking settings in soft-body physics simulations, according to googling I did months ago. I had a heck of a time getting this right although I haven't touched the project in a number of months, but I remember just endless setting tweaking because there are so many settings to try changing. If you only had this start happening because of my code, maybe that's a lead! But I'm not sure another way to approach it. If you find a solution please do share :)



  • Last year I created an endless platform jumper in HaxeFlixel with Nape.
    It's open source, so maybe it can help you.

    You can download the app for Android here:
    https://play.google.com/store/apps/details?id=nl.spip.gamechallenge.game2
    Source:
    https://github.com/spipnl/game-challenge/tree/master/game2
    Blogpost:
    http://spip.nl/my-second-game-skyroll/


  • administrators

    @spipnl Nice game! Looks like a good candidate for the showcase. ;) https://github.com/HaxeFlixel/haxeflixel.com#how-to-add-a-showcase-game



  • @IBwWG okay so i've found the cause of the vibration. When my two bodys collided nape would try to bounce the dynamic object away but in the next frame I would override that with a new velocity sending it back towards the static body. this would repeat indefinitely thus causing the vibration.



  • @Grimmr Ah OK that's maybe not the same sort of vibration I meant. I had a bunch of joints, and any jointed objects would seem to come to rest nearly, and then wriggle more and more at their joints until they went flying off into space. I ended up giving up on the joints idea because it was way too delicate to balance. Glad you solved yours! Best of luck with the rest :)


Log in to reply
 

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.