Wall collision difficulties


(Elias) #1

Greetings!

I am trying to make my player collide with my map, so he wont pass through it, but i run into a few difficulties. My code was based on some old tutorials, like the one with the grid collision system but unfortunately i couldn’t make it work. Can anyone help me by posting me a piece of a code about wall collision for me to use, with a bit of explanation about it?


(billy2000) #2

is your map made with ogmo editor? u can give a type to your wall by typeing this in wall class:

type="wall";

and at player class in override public function update u can add this:

if(collide("wall",x,y){
x-=speed; //i taked the example if player's x is increaseing by speed so if he collide with the wall it will decrease with same speed;
}

Also u should have super.update() function at the end of override public function.


(Elias) #3

Well, i already did all this. I am using an entity for the player and he collides with the map if he touch the walls. I want him to not go through the wall when he touches it.


(billy2000) #4

also dont forget a very important think. The wall and the player should have a hitbox. u can do that by typeing in each class this:

sethitbox(his height , his width , u can let this 0 for the moment , this too)

u can see theyr hitboxes by entering in console.


(billy2000) #5

can you post the code please?


(Elias) #6

Here you go.

if(collide("solid", x, y))
		{
			moveSpeed = 0;
			
			if (rightMovement = true)
			{	
				leftMovement = false;
				x = Math.floor(x / 8) * 8 + 8 - width;
			}
			if (leftMovement = true)
			{	
				rightMovement = false;
				x = Math.floor(x / 8) * 8 + 8;
			}
			
		}

(billy2000) #7

can you post the moveing code of player too? >.<


(Elias) #8
public class Player extends Entity
{
	[Embed(source = "../gfx/_player.png")] private const PLAYER:Class;
	
	private var gravity:Number;
	private var moveSpeed:Number;
	private var upMovement:Boolean;
	private var downMovement:Boolean;
	private var leftMovement:Boolean;
	private var rightMovement:Boolean;
	
	public function Player()
	{
		y = 100;
		
		x = 100;
		
		graphic = new Image(PLAYER);
		
		type = "player";
		
		setHitbox(8, 8, 0, 0);
		
		layer = 1;
		
		Input.define("walkUp", Key.UP);
		Input.define("walkDown", Key.DOWN);
		Input.define("walkRight", Key.RIGHT);
		Input.define("walkLeft", Key.LEFT);
		Input.define("jump", Key.Z);
		Input.define("reset", Key.R);
	}
	
	override public function update():void
	{
		moveSpeed = 2;
		
		leftMovement = false;
		rightMovement = false;
		
		if(Input.check("walkUp"))
		{	
			
			y -= moveSpeed;
		}
		
		if(Input.check("walkDown"))
		{
			
			y += moveSpeed;
		}
		
		if(Input.check("walkLeft"))
		{
			leftMovement = true;
			
			x -= moveSpeed;
		}
		
		if(Input.check("walkRight"))
		{
			rightMovement = true;
			
			x += moveSpeed;
		}
		
		if(Input.check("reset"))
		{
			x = 100;
			y = 100;
		}
		
		if(collide("solid", x, y))
		{
			moveSpeed = 0;
			
			if (rightMovement = true)
			{	
				leftMovement = false;
				x = Math.floor(x / 8) * 8 + 8 - width;
			}
			if (leftMovement = true)
			{	
				rightMovement = false;
				x = Math.floor(x / 8) * 8 + 8;
			}
			
		}
		
		super.update();
	}
}

}


(billy2000) #9

try this:

 if(collide("solid", x, y))
    {

        if (rightMovement = true)
        {   
            leftMovement = false;
            x +=moveSpeed;
        }
        if (leftMovement = true)
        {   
            rightMovement = false;
           x -=moveSpeed;
        }
            moveSpeed = 0;

    }

(Elias) #10

Nothing happens. Still, the object passes through grid blocks. The code i gave ya do the trick, but only for one direction. I tried making it work but i got nothing so far.


(Jacob Albano) #11

Why not use moveBy()?

super.update(); // this should come first, not after

var p = new Point();

// get inputs
if (moveLeft) p.x--;
if (moveRight) p.x++;
if (moveUp) p.y--;
if (moveDown) p.y++;

p.normalize(speed);
moveBy(p.x, p.y, "wall");

As long as you’ve set up the grid correctly and given your player a hitbox that should be all you need to do.


(Elias) #12

Thank you very much! Is it possible to ask for one last thing? What exactly the point variable does? I know it’s for coordinates, but how exactly we use it here? I can’t figure out what moveBy make the entity collide by combining it with the point.


(Jacob Albano) #13

I was just using the point to store a movement vector. For example, when the user presses Up and Right, the point has the values { x : -1, y : 1 }. It has the handy side effect of canceling out movement on an axis when opposite keys are pressed; for example pressing Left and Right at the same time will result in a movement of 0 on X.

Normalizing the point by the speed value results in a movement vector that moves the player at the same speed even if moving on diagonal. You may have noticed that moving diagonally is often much faster than moving in a single direction, and normalizing the vector fixes that.

Ultimately you don’t need a point variable at all, just feed moveBy with the amount you want to move on X and Y. I like to use points for the reasons above, though, and it makes for a very concise example. :slight_smile: