Game Development Community

Push an Object

by Brian Kelly · in Torque Game Builder · 08/30/2006 (7:53 am) · 4 replies

In an effort to start simple and work my way up to a more complex game, I've decided to create a "simple" Sokoban or crate pushing game (http://en.wikipedia.org/wiki/Sokoban).

I created a tile map which defines the floor and walls of the level. The walls provide collision. Then I placed a Crate as a simple t2dSceneObject.

What I'd like to happen is that when the Player "pushes" the Crate, it moves until the Player stops pushing or the Crate hits a wall.

So far, I've turned on Receive Collisions for the Crate and Send Collisions for the Player. I checked off Callback on the Crate and implemented onCollision so that the crate will move 1 unit in the direction it's being pushed. I also disabled the Physics Send/Receive, as they seemed to prevent anything from happening at all.

The problem I'm having is that as soon as the Player touches the crate, the Player stops moving. Any suggestions as how to keep the player moving? I tried called the Players updateMovement method inside the Crate's onCollision method (a bit of a hack I would think) but it doesn't seem to have an effect.

#1
08/30/2006 (3:52 pm)
I could be wrong, but you might want to turn off the receive physics for your player, that should keep the impact from the crate from affecting it and stopping it's motion automatically. Then handle the collision for the crate and set it to moving in the direction you want. Then if your character is still sticking, then call the player movement after setting the crate in motion and the character should no longer hit the crate and move because the crate should no longer be where it was.

Just a guess at what to try. If I find a method that works for sure I'll post back here.
#2
08/30/2006 (4:14 pm)
That is exactly what I have, though.

Tilemap:
Receive Collision = ON
Detection Mode = POLYGON
Collision Reponse = CLAMP
Certain tiles have Collision turned ON (the walls)

Player:
Send Collision = ON
Send Physics = ON (if I shut this off I can walk through everything, including the crate)
Detection Mode = POLYGON
Collision Response = CLAMP
Class = Player

Crate:
Receive Collision = ON
Callback = ON
Detection Mode = POLYGON
Collision Reponse = CLAMP
Class = Crate

With the following code:
function Crate::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts) {
   if (%dstObj.class $= "Player") {
      // Simplified movement below - just move it up 1 unit
      %srcObj.moveTo(%srcObj.getPositionX(), %srcObj.getPositionY() - 1, 25);

      %dstObj.updateMovement();  // Doesn't seem to have an affect
   }
}

I wonder if maybe I should Mount the box to the Player when they're pushing into it so they can move as one and then Dismount when the player goes in the opposite or a sideways direction. I haven't actually looked into mounting though.
#3
08/30/2006 (6:19 pm)
The mounting might be a good solution.
#4
08/30/2006 (6:20 pm)
A function that will help is

%obj.getLocalPoint(%x, %y);

with that you can do something like this

%crate.mount(%player, %player.getLocalPoint(%crate.getPosition));