Dont getting my Hero colliding with the Grid (SOLVED)


(christopf) #1

Hi everyone, i’ve got another question here since i’m lost in my code: I want my player to collide with some walls on the map. i implemented my oel like zachery has shown in his video tutorials and i get all files loaded on the screen. but whatever i try, the player aint colliding with the gridmask.the entities the player can collide with are in the world class and defined by type.

is it because the entity i put the image and the grid in needs a hitbox? i tried it with hitboxes but it didnt worked. the hero is just walking over the cliff like indiana jones on invisible paths.

the other question i was asking myself, if its a problem, when the entity is the whole background of the game and just some parts (defined through the gridmask) are collidable?

do you have an advice?


(David Williams) #2

Could we see the code where you’re defining the grid?


(christopf) #3

Ofcourse! Alabsa is my world class, Berge is the background graphic for the whole world, stein is a single graphic i wanted to try climbing and hiding later on):

	public function Alabsa(mapData:Class) 
	{
		super();
		
		Stein = new Image(stoneJPG);
		Berge = new Image(map2);
		
		_mapData = mapData;
		loadMap(_mapData);              }

	override public function update():void 
	{
		super.update();
		
		add(bergeundschluchten);
		add(stein);
		add(mori);			
	}

	protected function loadMap(mapData:Class):void
	{
		var mapXML:XML = FP.getXML(mapData);
		
		_mapGrid = new Grid(uint(mapXML.@width), uint(mapXML.@height), 16, 16, 0, 0);
		_mapGrid.loadFromString(mapXML.Grid, "", "/n");
		
		mori = new Mori(int(mapXML.objects.held.@x), int(mapXML.objects.held.@y));
		
		bergeundschluchten = new Entity(0, 0, Berge, _mapGrid);
		bergeundschluchten.type = "solid";	
		/** hitbox attempts */ 
		//bergeundschluchten.setHitbox(2304, 1088, 0, 0);
		
		stein = new Entity(int(mapXML.objects.stone.@x), int(mapXML.objects.stone.@y), Stein, _mapGrid);
		stein.type = "solid";
		//stein.setHitbox(32,32)
	}

I forgot to try to implement the graphic as tilemap. maybe this is the problem? i give it a try meanwhile


(Vishal) #4

Hi Chris You need to check for collision, which for your case will be something like:

if ((xSpeed != 0 || ySpeed != 0) && !collide("solid", x + xSpeed, y + ySpeed ))
{
    //Move your entity when there is no collision
}

where xSpeed is speed on the x axis and ySpeed is speed on the y axis and x and y are the coordinates.

For “bergeundschluchten” you specify a mask which acts like the hit area so you must not specify a hit box for “stein” you need to specify a hit box because you haven’t specified a mask.

the other question i was asking myself, if its a problem, when the entity is the whole background of the game and just some parts (defined through the gridmask) are collidable?

This is not a problem and that is mostly the reason to use masks.


(Stefan Langeder) #5

You could also use the entitie’s moveBy function: moveBy Documentation


(christopf) #6

Thanks for the response @stee @Vishal ! I just tried two different way of collision (i already did earlier but wanted to make sure it doenst work)… First:

		if (Input.check("Links"))
		{
			if (!checkCollide(new Point(x,y)))
			{
				x -= Geschwind * FP.elapsed;
			}
			curAnimation = "west_gehen";
		}

this for the update() function and beneath this

	public function checkCollide(position:Point):Boolean
	{
		if (collide("solid", position.x, position.y)) 
		{
			return true; trace("hell") 
		}
		else {return false};
	}

That was one way of trying the other one looked like your suggestion vishal:

		if (Input.check("Links"))
		{
			if ((Geschwind != 0) && (!collide("solid", x-Geschwind,y)))
			{
				x -= Geschwind * FP.elapsed;
			}
			curAnimation = "west_gehen";
		}

again this was in the update() function of the hero. Geschwind is my speed var. i guess i did somethin wrong like a wrong var or in the false function. also i didnt understand why you put the xSpeed != 0 into the if parentheses.

stee i dont know how to manage the moveby function but i remember i stumbled over it in my searches here. maybe i find it again so i get a clue how to start with it. but that may not be the solution for the problem :confused:

thanks anyway guys.


(christopf) #7

oh i didnt mention this : collision is still not working :confused:


(Vishal) #8

You want to check collision for the new coordinates like so:

 collide("solid", x - Geschwind * FP.elapsed, y)

The other problem might be hit boxes - have you set it up for your player entity? As Stee mentioned if you only have obstacles and don’t want any behavior on collisions you can use the moveBy function which will automatically check for collisions and move until the point before collision:

moveBy( x - Geschwind * FP.elapsed, y, "solid")

you might need to use the fourth sweep argument depending on the value of Geschwind


(Stefan Langeder) #9

You can have behavior on collisions even with using the moveBy function. Just override the moveCollideX and moveCollideY functions of your Entity. :wink:

@chris have you set hitboxes now? You need them to make it work.


(christopf) #10

Meow. I tought multiple times that i should check if i set a hitbox for the player entity and then i forgot it every time. You right, i had no hitbox set up. So i did that. But it aint working yet. I did my collide code like this (for each direction a little bit different)

		if (Input.check("Links"))
		{
			if (!collide("solid", x - Geschwind * FP.elapsed, y))
			{
				x -= Geschwind * FP.elapsed
			}
			curAnimation = "west_gehen";
		}

I was wondering if my grid aint setup right. i activated the console and seeing the hitbox of my player but i dont see the hitbox i set up for the stein entity (for checking only because i considered it while creating the grid in ogmo so the gridmask should be enough originally). i see the graphics for the entities but nothing else. is the grid to be shown originally when i toggle console? i posted the the world code above but forgot to say that mori is the player class. the collision checking as well as the player movement happens in the update() function of the player class


(Vishal) #11

You can’t see the grid mask on the console. I went through the code again and saw this:

override public function update():void 
	{
		super.update();

		add(bergeundschluchten);
		add(stein);
		add(mori);			
	}

I would be surprised if you were able to get any input accepted. You are adding the entities every time the world updates and since the game engine updates the world continuously this would lead to very strange behavior. Either shift the add() functions to loadMap() or to the constructor immediately after calling loadMap()


(Stefan Langeder) #12

@Vishal I just noticed the same thing. :wink: Anyay, you were faster.

To apply the grid to the bergeundschluchten object you need to write the following code:

bergeundschluchten.mask = _mapGrid;

(Vishal) #13

@Stee that is already done in the assignment:

bergeundschluchten = new Entity(0, 0, Berge, _mapGrid);

(christopf) #14

What you saying is pretty logical should have come to this on my own too but to now i had no problem with the input. it worked pretty well same as now (i changed it to the constructor). but the collision is still not working. i added a

if (collide ("solid", x, y)) { trace("meoz") }

to the update() of the player class but the output showing no signs.


(Vishal) #15

Okay lets simplify the problem - add only the player and one stein entity to the world - set up the hit boxes for both. Set up their position so that either they are colliding or you can make them collide with the input. Now run the code and check to see whether the hit boxes overlap in the console mode and you are getting the debug output (“meoz”) that you have added on the output console. If you still cannot get it to work post the code in full for the world class and the player class.


(christopf) #16

SO… FINALLY… i got it. Thank you a lot for all your time your dedicated to this problem. As i watched zachery video again i recognized i found the problem. it all laid in the loadFromString function. Zachary named his Grid class in ogmo Grid, i named it solid. but i copied just to blind and wrote myself Grid too. Also i used the wrong slash on the new line command: /n instead of \n

problem

_mapGrid.loadFromString(mapXML.Grid, "", "/n");

solved

_mapGrid.loadFromString(mapXML.solid, "", "\n");

(christopf) #17

just learned it shows you the grid <3


(Vishal) #18

It does. Looks like I got used to working on my levels with grids and didn’t notice that the grid on the console was the collision grid and not part of the tiles.


(Ultima2876) #19

This topic was automatically closed after 2 days. New replies are no longer allowed.