Strange collision issue



  • I'm working on a game where gravity can be manipulated, but running into some strange collision issues when walking up and down walls. The player seems to be colliding in directions it shouldn't. Here's some example code that demonstrates the issue:

    package;
    
    import flixel.FlxState;
    import flixel.FlxSprite;
    import flixel.FlxG;
    import flixel.group.FlxGroup;
    import flixel.util.FlxColor;
    import flixel.input.keyboard.FlxKey;
    import flixel.input.keyboard.FlxKeyList;
    
    class PlayState extends FlxState
    {
    	private var testPlayer:FlxSprite;
    	private var ground:FlxGroup;
    
    	private var TileSize:Int=20;
    
    	private function createGroundPiece(x:Int, y:Int){
    
    			var newGroundPiece:FlxSprite = new FlxSprite(x,y);
    			newGroundPiece.makeGraphic(TileSize, TileSize, FlxColor.BROWN);
    			newGroundPiece.immovable = true;
    			ground.add(newGroundPiece);
    	}
    
    	override public function create():Void
    	{
    		testPlayer = new FlxSprite(100,180);
    		testPlayer.makeGraphic(TileSize, TileSize, FlxColor.BLUE);
    		add(testPlayer);
    		ground = new FlxGroup();
    		for(i in 0...10){
    			createGroundPiece(i*TileSize,0);
    			createGroundPiece(i*TileSize ,200);
    			createGroundPiece(0,i*TileSize);
    			createGroundPiece(200, i*TileSize);
    		}
    		createGroundPiece(200, 200);
    		add(ground);
    		testPlayer.velocity.set(30,0);
    		testPlayer.acceleration.set(0,400);
    		super.create();
    	}
    
    	override public function update(elapsed:Float):Void
    	{
    		FlxG.collide(testPlayer, ground);
    
    		if(FlxG.keys.anyPressed([FlxKey.RIGHT])){
    			
    			testPlayer.acceleration.set(400,0);
    			testPlayer.velocity.set(0,-30);
    		}
    
    		if(FlxG.keys.anyPressed([FlxKey.LEFT])){
    			
    			testPlayer.acceleration.set(-400,0);
    			testPlayer.velocity.set(0,-30);
    		}
    
    		super.update(elapsed);
    	}
    }
    

    Here the blue square start on the ground moving right. Press left or right to switch gravity and the player will start walking up. But he seems to get stuck between ground sprites. Any ideas?



  • @WhaleFood
    I cannot duplicate this bug. Player does not get stuck. However, there is an issue with my versions of the libs where the player can get stuck in the seam of sprites. If that is the case then use allowCollisions. For example, allowCollisions = FlxObject.UP + FlxObject.LEFT + FlxObject.RIGHT;



  • Just some suggestions:

    • FlxG.collide should be called after super.update(elapsed)
    • Make sure you have FlxG.worldBounds.set() (people often forget this and let it cause some collision problems, but I'm not sure about this case)


  • Thanks for the suggestions guys. I tried changing the position of super.update and setting FlxG.worldBounds, but didn't seem to help. I could manually set the allowCollisions of each ground piece, but that would be kind of a pain once my ground gets more intricate than a simple square. I guess that might be the only way around the issue though. I uploaded a build to show what I'm talking about:

    https://s3.amazonaws.com/whalefoodteststuff/CollisionTest.swf

    Arrow keys switch gravity. For some reason floor and ceiling are fine. It just doesn't like walls...



  • Figured out what was going on. The collision hits for the ground above the player are happening before the hits for the ground sprite it's mostly on. And on the sprite above the player, the collision is happening only in the y direction. So I just need to make sure I'm accounting for all x collisions before y's when the gravity is on the side. Like this:

    	FlxG.overlap(testPlayer, ground, null, FlxObject.separateX);
    	FlxG.overlap(testPlayer, ground, null, FlxObject.separateY);

Log in to reply
 

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