Game Development Community

Tile Collision Difficulties

by chris · in Torque Game Builder · 03/10/2005 (4:13 pm) · 7 replies

I've searched everywhere for a way to do this that works for me, but can't seem to find one. So here's my problem:

I have a tilemap layer that holds all of the items the player can "pick up". The layer itself is tagged as "itemLayer". What I'm trying to achieve is having the player be able to collide with the tiles on this layer, have the tile be cleared and have the players current velocity not be affected.

Initially what happened upon collision is that the tile would be removed properly, but the player would have a regular response to the collision (ie bouncing off of the tile).

Some searching turned up the use of setPhysicsSuppress(true) to cancel out the physical response of colliding with the tile, but while this does stop the player from bouncing away, it also removes the $players velocity before hitting the tile.

switch$( %srcObj.tag ) {
        case "itemLayer":   
            
            // suppress $player's physics
            %dstObj.setPhysicsSuppress(true);

            // clear the tile/item from "itemLayer"
            %srcObj.clearTile(%srcRef);

            // set a schedule to reinitiate the $player's physics
            %dstObj.schedule(250, setPhysicsSuppress, false);

            break;
}

The code above is the section of the onCollision() callback that handles collisions with the "itemLayer" tiles. My question is, how can I adjust this so that the player will resume with the velocity it had before the collision? I hope this makes sense.

Thanks for any help you can give me,
Chris

#1
03/10/2005 (8:23 pm)
I have code that creates game entities using tiles. It basically iterates through the tile layer, looking for tiles using a certain datablock. Once it finds the datablock, it creates the object, and deletes the tile. I am working on nicer version, but here is an example for now, till i finish it.

If you look at the two if fuctions with the "// spawn object 1" and "// spawn object 2", you will notice all they basically do is erace the tile, and create the game entity. In reality you would have a function thats gets called to create the object in there instead.

When you call the "loadGameObjects", you pass it the layer that you want to use for object creation, and the index of that layer (the layer number).

function loadGameObjects(%layer, %layerIndex)
{
   %size = %layer.getTileSize();
   %sizex = getWord(%layer.getTileSize(), 0);
   %sizey = getWord(%layer.getTileSize(), 1);
	
   // loop through the layer, looking for tiles that
   // will spawn objects
   %tileCount = %layer.getTileCount();
   for (%x = 0; %x < getWord(%tileCount,0); %x++)
   {
      for (%y = 0; %y < getWord(%tileCount,1); %y++)
      {
         // get tile information
         %tile = %layer.getTileType(%x SPC %y);
         
         // check if the tile is static
         if(getWord(%tile, 0) $= "static")
         {
            // get the name of the datablock which
            // is used to determine which object to
            // create in the game world
            %gameObject = getWord(%tile, 1);
				
            // get the position of the object in world space
            %xoffset = getWord(%layer.getTileCount(), 0) * %sizex / 2;
            %yoffset = getWord(%layer.getTileCount(), 1) * %sizey / 2;
            %xpos =  (%x * %sizex) - %xoffset + (%sizex / 2);
            %ypos = (%y * %sizey) - %yoffset + (%sizey / 2);

            // spawn a object 1
            if(%gameObject $= "object1ImageMap")
            {
               %layer.clearTile(%x SPC %y);
               createObject1(%xpos SPC %ypos, %size, %layerIndex);
            }

            // spawn object 2
            if(%gameObject $= "object2ImageMap")
            {
               %layer.clearTile(%x SPC %y);
               createObject2(%xpos SPC %ypos, %size, %layerIndex);
            }
         }
      }
   }	
}
#2
03/10/2005 (8:28 pm)
And if you look at the code i put up, you can of course use a switch statement, instead of the if statements. Its just a quick clip of the in progress code I am working on, hacked to work outside of my framework.
#3
03/11/2005 (5:28 am)
@Ray - This is great! I still had the problem with my player reacting physicallly upon collision with the objects created from the tiles, but this completely solves another problem I was looking at for the future.

To solve my problem with physics (for those looking for a solution to a similar problem) I created an fxSceneObject2D() the same size as my player object, mounted it to my player and let it handle all none-physical collisions. It does add an extra object to the game, but the ease of using it outweighs that fact, I think.

Chris
#4
03/11/2005 (7:41 am)
Although I'm just starting to get into T2D development, and may not quite know enough to answer a question like this, I still have to ask: Couldn't you just disable the player's ability to receive a collision event from your 'pick up' tilemap layer, and have the tilemap object (when the the collision event is sent to it) handle the actual pickup?
#5
03/11/2005 (8:45 am)
Quote:To solve my problem with physics (for those looking for a solution to a similar problem) I created an fxSceneObject2D() the same size as my player object, mounted it to my player and let it handle all none-physical collisions. It does add an extra object to the game, but the ease of using it outweighs that fact, I think.

Could you explain that a bit?
In my game I'd need something similar. My sprite can be in two modes, in the first mode it collides with the tile and bounces of it (tile not affected) in the 2nd mode it should go right through the tile and the tile should be deleted.

How would I do that?
I can't disable physics because other forces still need to be applied (like gravity and wind) when I'm in that mode.
#6
03/11/2005 (8:50 am)
Well depending on how many tiles you want the user to be able to collect, it is probably better to implement them as game objects using a method like the one I outlined above. It makes things much more simple, and adds a lot of flexibility. In addition you can completely disable the physics on the game objects, and just detect the collisions. This would give you the exact functionality you want. In addition you wouldn't have to create another fxSceneObject2D and mount it to the player.
#7
03/11/2005 (9:17 am)
Never mind.