Collision Problem


(Gil) #1

I’ve tried using moveby() with no luck. Sometimes the entity is halfway through the ground, other times it actually works. But isn’t much reliable. Tried presumably everything, please help :confused:

Note: I do use solidTypes with the ground type, and sweeping. Still nothing.


(John Andersson) #2

I know a good example :smiley:


(Jacob Albano) #3

Some code would probably help here.

  • Are you overriding moveCollideX/Y()?
  • Are you sure each entity has its type set?
  • Do all entities involved have hitboxes?

Finally, are you trying to use moveBy() to resolve a collision after the fact? It doesn’t work that way, unfortunately; it only prevents collisions when you start off without colliding.


(Gil) #4
override public function update():void 
		{
			/* Walking and Jumping */
			
		if (collide("ground", x, y + 1) || collide("ground2", x, y + 1) || collide("ground3", x, y + 1)) 
			{
				ySpeed = 0;
				//moveBy(xSpeed, ySpeed, solidTypes, true);
				
				if (Input.check("jump")) 
				{
					if (facingForward) beagleSprite.play("jump_right");
					else beagleSprite.play("jump_left");
					ySpeed -= jumpPower;
				}
				
				if (Input.check("left"))
				{
					x -= jumpPower * .2;
					facingForward = false;
					
				}
				if (Input.check("right"))
				{
					x += jumpPower * .2;
					facingForward = true;
				}
			} 
			
			else if (collide("vehicle", x, y))
			{
				ySpeed = 0;
				
				if (Input.check("jump")) 
				{
					if (facingForward) beagleSprite.play("jump_right");
					else beagleSprite.play("jump_left");
					ySpeed-= .8 * jumpPower;
				}
			}
			
			else ySpeed += gravity;
			
			moveBy(xSpeed, ySpeed, solidTypes, true);
			
			
			y += FP.elapsed * ySpeed;
			FP.clampInRect(this, 0 + originX, 0 + originY, 640 - width, 460 - hei

ght);

I have 3 different grounds because I’m attempting to have it so that if a certain UPkey is pressed, the player will instantaneously appear on the upper level ground each time, with the Downkey making the player go down a level. And on each different ground level, the entity is still able to jump over obstacles with the SPACE key but still retain it’s current level. Any clue as to how to achieve this? Any help on that would be heavily appreciaite, I barely know what i’m doing :confused: lol


(Jacob Albano) #5

I think I might see your problem:

moveBy(xSpeed, ySpeed, solidTypes, true);
y += FP.elapsed * ySpeed;

The second line will move your player without taking collision into account, which is probably why it gets stuck. If moving based on elapsed time is important, you can do this:

moveBy(xSpeed * FP.elapsed, ySpeed * FP.elapsed, solidTypes, true);

Bonus: If you want to trim down on typing while potentially improving performance a negligible amount, use collideTypes():

if (collideTypes(["ground", "ground2", "ground3"], x, y + 1))
{
}

(Gil) #6

Very useful, everything is working swell! Thanks again! Life saver much!


(Jacob Albano) #7