[SOLVED] MoveBy() and collision detecion


(Bartłomiej Kalemba) #1

Hello! I have a problem with colliding. I have two Entities.:

  1. Player is moving in update():

    override public function update():void { super.update(); if (!world.bPlayingCutscene) { if (Input.check(Key.RIGHT)) { moveBy(speed * FP.elapsed, 0, Game.COLLISIONS); } else if (Input.check(Key.LEFT)) { moveBy(-speed * FP.elapsed, 0, Game.COLLISIONS); }

     		if (Input.check(Key.DOWN))
     		{
     			moveBy(0, speed * FP.elapsed, Game.COLLISIONS);
     		}
     		if (Input.check(Key.UP))
     		{
     			moveBy(0, -speed * FP.elapsed, Game.COLLISIONS);
     		}
     		layer = -(y + Game.PLAYER_HEIGHT);
     	}
     	
     	FP.camera.x = -FP.halfWidth / FP.screen.scale + x;
     	FP.camera.x = FP.clamp(FP.camera.x, 0, Game.WIDTH - FP.screen.width / FP.screen.scale);
     	
     	FP.camera.y = -FP.halfHeight / FP.screen.scale + y;
     	FP.camera.y = FP.clamp(FP.camera.y, -100, Game.HEIGHT - FP.screen.height / FP.screen.scale);
     }
    

and the second Entity is “pseudo enemy” for now who is following us in update():

override public function update():void
	{
		if (!world.bPlayingCutscene)
		{
			
			if (x < world.player.x)
			{
				moveBy(speed * FP.elapsed, 0, Game.COLLISIONS);
			}
			if (x > world.player.x)
			{
				moveBy(-speed * FP.elapsed, 0, Game.COLLISIONS);
			}
			
			if (y < world.player.y)
			{
				moveBy(0, speed * FP.elapsed, Game.COLLISIONS);
			}
			if (y > world.player.y)
			{
				moveBy(0, -speed * FP.elapsed, Game.COLLISIONS);
			}
			if (collide("player", x, y))
			{
				world.recycle(this);
			}
			super.update();
		}
		layer = -(y + gfxHeight);
	}

But the If statement isn’t true…

And the Game.COLLISIONS:

public static const COLLISIONS:Array = ["map", "player", "nurser"];

Someone have an idea how to solve?


(Bora Kasap) #2

moveBy() function is not enough against “both moving” entities. I think you don’t make anything wrong but you need some fixer functions like “pushing” entities instead of colliding, one of them should be able to push another one. Probably you should give this ability to the player.


(Bartłomiej Kalemba) #3

I’m sorry but I don’t understand what I should do?

Maybe I don’t exlain everything as I should: When enemy fallow the player in y-axis, then there is moment when enemy is stopped by Player Hitbox, and enemy never go to the player y coords, when he is going from bottom.

But now, I want to recognize this (collide with hitbox) and then invoke some other functions, but for now just recycle Enemy


(Jacob Albano) #4

Why are you handling each axis separately? Your code moves on X first, then Y, instead of both at the same time.

var p = new Point();

// get inputs
if (Input.check(Key.LEFT)) p.x--;
if (Input.check(Key.RIGHT)) p.x++;
if (Input.check(Key.DOWN)) p.y--;
if (Input.check(Key.UP)) p.y++;

p.normalize(speed * FP.elapsed);
moveBy(p.x, p.y, Game.COLLISIONS);

And you can replace your Enemy movement code with the following:

moveTowards(world.player.x, world.player.y, speed * FP.elapsed, Game.COLLISIONS);

When you say “the if statement isn’t true”, which one do you mean? There are a lot of them in there.

If you’re talking about your check for collision against the player, that’s because moveBy() will cause your enemy to stop against the player. You’ll want to put your checks in the moveCollideX/Y() functions:

public override function moveCollideX(e:Entity):Boolean
{
    return checkCollision(e);
}

public override function moveCollideY(e:Entity):Boolean
{
    return checkCollision(e);
}

private function checkCollision(e:Entity):Boolean
{
    if (e.type == "player") world.recycle(this);
    return true;
}


(Jacob Albano) #5

moveBy is totally fine for this purpose.


(Bartłomiej Kalemba) #6

Yes, If was about collision detection. I correct my code as You write @jacobalbano But I’m not sure if I correctly used ovveride functions. Just place them in Enemy class? Invoke them or just override?

Second: now my Enemy when going from bottom didn’t stop at Player Hitbox


(Bora Kasap) #7

yeah, maybe i can’t understand what’s the problem… but now @Calemb says

and, i was thought that was the first problem.


(Jacob Albano) #8

The override functions should be placed on the Enemy class. They will be called during the movement routines, so don’t call them yourself.


(Bartłomiej Kalemba) #9

Och, overrided methods work fine! I thing I should sleep more or sth. in moveTowards() I forgot about collisions definitions as last argument.

Thank You both.