Game Development Community

Best way to capture walking collisions?

by Jeremy Tilton · in Torque Game Builder · 04/03/2005 (4:45 pm) · 15 replies

So if I want a guy walking around on a map filled with collision stuff, what is the best way to go about this?

I am capturing collisions with the tilemap layer just fine, but with current implementation, there are still some undesireable side-effects. Currently I have the up, down, left, right keys moving my character by setting it's linear velocity. I have 4 small boxes surrounding the player (so I can see which direction a collision occurs), and in the callback, I set the appropriate linear velocity to 0. All works fine for the most part, but if I keep pressing in the direction of the collision, he slowly moves into the wall (or whatever). Should I use the physics engine instead? Did anyone implement a simple character walking on a map that wants to give some pointers?

#1
04/03/2005 (6:35 pm)
What I did was use onCollision to see what's being collided with. If it's something I want the player to stop at (wall) then in onCollision I use setAtRest() on the player.

I believe there's a command to see which side a collision ocurred on so you wouldn't need 4 small boxes surrounding the player.

I dunno if this helps at all... but I tried

- Matt VG
#2
04/03/2005 (7:59 pm)
SetAtRest() eh? I'll give it a shot. Thanks. If you keep tapping in the direction of collision, does he slowly move in?
#3
04/04/2005 (3:15 am)
I'm pretty sure you can find out the normal of a collision (i.e. its direction), as it is passed to the collision callback.
#4
04/04/2005 (6:43 am)
@Jeremy:
Nope. I don't get any movement I don't want.
#5
04/05/2005 (8:56 pm)
Working great. Although, I am still having an undesirable effect, wondering if you guys get a similar effect... Imagine walking at an angle by pressing both up and right. He hits a wall above him so he slides along the wall going right, but collision keeps him under the wall (this part works)...however, when he reaches the end of the wall and there is empty space up top, he should continue up and to the right (assuming you are still holding both keys down). It doesn't. Up must be released then repressed to move up again. Is there a script solution to this using the callbacks?
#6
04/05/2005 (9:06 pm)
Working great. Although, I am still having an undesirable effect, wondering if you guys get a similar effect... Imagine walking at an angle by pressing both up and right. He hits a wall above him so he slides along the wall going right, but collision keeps him under the wall (this part works)...however, when he reaches the end of the wall and there is empty space up top, he should continue up and to the right (assuming you are still holding both keys down). It doesn't. Up must be released then repressed to move up again. Is there a script solution to this using the callbacks?
#7
04/09/2005 (9:23 pm)
Sounds like an engine bug to me :)
#8
04/10/2005 (1:21 am)
What I do, simply, is to zero the player's velocity and then move him a smidge back into the scene.

function fxSceneObject2D::onWorldLimit(%this, %limitMode, %limit) {
	if (%this.tag $= "player") {
		if (%limit $= "left") {
			%this.setLinearVelocityX(0);
			%this.setPosition(vectorAdd2D(%this.getPosition(), "0.1 0"));
		} else if (%limit $= "right") {
			%this.setLinearVelocityX(0);
			%this.setPosition(vectorAdd2D(%this.getPosition(), "-0.1 0"));
		} else if (%limit $= "top") {
			%this.setLinearVelocityY(0);
			%this.setPosition(vectorAdd2D(%this.getPosition(), "0 0.1"));
		} else if (%limit $= "bottom") {
			%this.setLinearVelocityY(0);
			%this.setPosition(vectorAdd2D(%this.getPosition(), "0 -0.1"));
		}
	}
}

Edit: Oh, worth mentioning that this is, naturally, for the world limits, but you could just as well use this for collisions with wall tiles and such.
#9
04/10/2005 (1:30 am)
Hmm, this wouldn't work with Pacman-like control, where the character is in constant motion, right?
#10
04/10/2005 (1:56 am)
Refresh my memory, if Pacman hits a wall, he stops, right? Then the above code snippet would do just that, stop him in his tracks. But if he hits a wall and bounces back, or changes direction, then that would be a different thing.
I'm actually fiddling around with a setup for a platform type game. And I'm thinking that the best approach would be using rigid body physics. But I can't get the player to run, jump and land on a colliding object, without him bouncing wildly away.
#11
04/10/2005 (2:02 am)
Yes, but I thought he meant that when a path opened up at the top, that the character wouldn't take it. Instead, it keeps going to the right. But I could have misunderstood the situation.
#12
04/11/2005 (11:39 am)
No, I don't think I misunderstood the situation. I just created something myself and ran into the same problem. If you hold diagonal into a wall, you'll of course stop against it, and slide across the wall. But when the wall ends, if you are continuing to hold diagonal, the player will stil continue in the same direction he was sliding in. It won't change back to a diagonal direction.
#13
04/11/2005 (11:50 am)
Unfortunately, we've only got one stock collision response at the moment and that's rigid-body. After the file-format update we'll be including "none", "sticky", "clamp", "bounce" and "rigid", identical to the world-limit. Pacman would probably use "clamp" to simply stop PacMan moving in the direction of the collision. None of the above responses (apart from "rigid") cause any rotation. It'll also be easy to add more stock/custom responses to this list.

Rigid-body isn't the best case for lots of games but we just ran out of time implementing everything we wanted to for the initial release.

Won't be long everyone!

- Melv.
#14
04/21/2005 (8:11 pm)
@Dave, yeah, now you got it. It isn't an engine bug, it is something that must be accomplished in script I would think. I'm just having trouble conceptualizing it.

@Melv, those would be awesome, definitely looking forward to those changes!
#15
04/25/2005 (5:06 pm)
Would be nice for someone to do a G man garage games logo gobbling up dots in a maze. Just as a tutorial.