Game Development Community

More Collision/Physics Difficulties...

by Matt Van Gorkom · in Torque Game Builder · 03/25/2005 (3:23 pm) · 21 replies

I need the player to stop when he hits a collision polygon. No bounce, no rotation, I just want the player to stop. I took care of the bounce and rotation with a collision material, but when something hits the player or the players collision polygon hits another at an angle the player drifts off. I can't seem to stop this any help would be appreciated.

- Matt VG

EDIT:
I also have the setCollisionActive( true, false) so the player doesn't recieve collisions, yet he still does.
Page «Previous 1 2
#1
03/25/2005 (3:28 pm)
Have you tried disabling the physics (could do a manual call)? let me run some examples and get back to you
#2
03/25/2005 (3:32 pm)
Matt,

You're player can still send collisions with the above command. It's the physics you want to control, not the collisions. Disable the physics response with "setCollisionPhysics(false, false);" which is the default anyway. Just turn the collision callback on "setCollisionCallback(true);" and simply do something like "%srcObj.setAtRest();"

- Melv.
#3
03/25/2005 (4:02 pm)
@Melv: I tried what you said and I think it might be working... I can collide with some stuff without player drift but not other stuff (I don't collide at all)... I'm still having difficulty with what to do in onCollision() :P

@Matt L: I'd like to see some more of your examples... I'm still pretty lost in TorqueScript especially with regard to collisions and physics. BTW your physics tutorial has helped a lot.
#4
03/25/2005 (4:19 pm)
In the ::onCollision() function you can seperate things depending on the %src (or %dst)

so you'd do something like
if(%srcObj == $player)
{
     if(%dstObj.type $= "wall")
     {
          %srcObj.AtRest();
     }
}

in this case when you created a wall you'd add

$wall.type = "wall";

also you would make sure the wall isn't sending or receiving physics, though keep the player sending and receiving them

glad the tutorial helped :)
#5
03/25/2005 (5:36 pm)
Somehow I have managed to still not have it working. It partially works because a little box I have for the player to knock around no longer knocks the player away, but I can't collide with the pillar I've been trying to collide with. I see no difference between how I've set up the pillar and how I've set up the box and I'm not even dealing with the box in onCollision.

For Player:
I have collisionPhysics false, false and collisionActive true, true.

For movable Box:
I have collisionPhysics true, true and collisionActive true, true.

For Immovable Pillar:
I have collisionPhysics false, false and collisionActive true, true.

My onCollision code is setup just as Melv and Matt L suggested for both %srcObj and %dstObj (just to be safe).
The box collides with the pillar.

I know it's got to be some stupid oversight on my part but I can't find it and it's really bothering me...
#6
03/25/2005 (7:10 pm)
I know how it is at times, can be very easy to miss omething, sometimes just need another set of eyes... post your code up here
#7
03/25/2005 (7:44 pm)
My code for player, pillar, box and onCollision as it now stands:

// Player 1
	$player1 = new fxAnimatedSprite2D() { scenegraph = t2dSceneGraph; };
	$player1.setPosition( "25 0" );
	$player1.setSize( "10 10" );
	$player1.playAnimation(standFront);
	
	$player1.type = "player1";
	// collision info
	$player1.setGroup( $playerGroup );
	$player1.setLayer( $playerLayer );
//	$player1.setCollisionPhysics( true, true ); // apparently not needed for now
	$player1.setCollisionActive( true, false );
	$player1.setCollisionPolyPrimitive( 3 );
	$player1.setCollisionMasks( BIT($projectileGroup) | BIT($miscGroup) | BIT($playerGroup), BIT($projectileLayer) | BIT($miscLayer) | BIT($playerLayer) );

	$player1.setCollisionCallback( true );
	$player1.setCollisionMaterial( playerMaterial );
	$player1.setmaxAngularVelocity( 0 );



//------------------------------------------------
// pillar to run into and behind
//-------------------------------------------------
	$pillar = new fxStaticSprite2D(pillarBase) { scenegraph = t2dSceneGraph; };
	$pillar.setPosition( "9 4" );
	$pillar.setSize( "14 25" );
	$pillar.type = "pillar";
	$pillar.setImageMap( pillarbaseImageMap );
	// collision info
	$pillar.setGroup( $miscGroup );
	$pillar.setLayer( $miscLayer );
	$pillar.setCollisionPhysics( true, false );
	$pillar.setCollisionActive( true, true );
	$pillar.setCollisionPolyCustom( 4, "-1 1 -1 0 1 0 1 1" );
	$pillar.setCollisionMasks( BIT($projectileGroup) | BIT($playerGroup), BIT($projectileLayer) | BIT($playerLayer) );

	$pillar.setCollisionCallback( true ); // destroy if hit by certain weapons??????
	$pillar.setCollisionMaterial( immovableMaterial );


//-------------------------------------------------
// box to smack around
//------------------------------------------------
	$box = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
	$box.setPosition( "20 20" );
	$box.setSize( "5 5" );
	$box.setImageMap( boxImageMap );
	$box.setCollisionPhysics( true, true );
	$box.setCollisionActive( true, true );
	$box.setGroup( $miscGroup );
	$box.setLayer( $miscLayer );
	// collision info
	$box.setCollisionMasks( BIT($projectileGroup) | BIT($playerGroup) | BIT($miscGroup), BIT($projectileLayer) | BIT($playerLayer) | BIT($miscLayer) );

	$box.setCollisionMaterial( boxMaterial );
	$box.setCollisionPolyPrimitive( 4 );




//-----------------------------------------------------------------------------
// onCollision callback
//-----------------------------------------------------------------------------
function fxSceneObject2D::onCollision( %srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts )
{
	// ----------------------------------------------------------
	// -------------- player 1 caused collision ----------
	if( %srcObj == $player1 )
	{
		
		if( %dstObj.type $= "pillar" ) // collision with pillars, boxes, etc...
		{
			%srcObj.setAtRest(); // stop player from moving
		}
	}
	
	// ------------ player 1 recieve collision ------
	if( %dstObj == $player1 )
	{
		if( %srcObj.type $= "pillar" ) // collision with pillar, boxes, etc...
		{
			%dstObj.setAtRest(); // stop player from moving
		}
		
	}

}

Thanks for your help.
#8
03/26/2005 (11:45 am)
My code still isn't working. Anybody got any ideas why?

Player can collide with box, box can collide with pillar, player cannot collide with pillar.

EDIT:
onCollision isn't called at all when the player and pillar should collide
#9
03/26/2005 (3:00 pm)
Just so you know I finally got a second to run some examples with this now :)
#10
03/26/2005 (3:23 pm)
Hmm ran your code on mine and it seems to work fine... only thing I noticed is your collision poly for the pillar only covers half of it, like this

www.razedskyz.com/games/torque/tutorials/T2D/collision.JPG
#11
03/26/2005 (3:36 pm)
The reason the collision map only covers half the pillar is because I'm creating a pseudo-isometric game. I have the second half of the pillar mounted on top of the bottom half and on a layer above the player so the player can run behind the upper half.

If the collision works for you, then it's not my collison setup nor my onCollision function.

Sigh... once again it's time to run through my code with a even-finer-toothed comb...

BTW, thanks for taking the time to help me. I appreciate it.
#12
03/26/2005 (3:51 pm)
Sure, wish I could help more... did you check the console for any red text ?
#13
03/26/2005 (3:56 pm)
Btw just to confirm, results I got were that the player hit the pillar and stopped (the AtRest)... the player hits the box and applies physics to it moving it
#14
03/26/2005 (4:02 pm)
OK, it was my collision code.

My player can collide with the pillar if the collisionphysics are active, but I get player drift when the collision polygons are at angles to each other...

This collides but drifts:
$player1.setCollisionPhysics(true, false );
	$player1.setCollisionActive( true, true );


Turning collisionphysics off results in no collision with pillar by the player:
$player1.setCollisionPhysics(false, false );
	$player1.setCollisionActive( true, true );


Did you try colliding the side of the collision triangle with the corner of the "pillar" collision box? That's where my problem occurs.

EDIT:
The console shows no errors...
#15
03/26/2005 (7:56 pm)
@ Matt L:
On another of the threads I noticed you and your team are also doing a pseudo-iso game... if it's not giving away trade secrets or such could you email me some tips/technique for how you're implementing it? :) And no, I don't know if I'm breaking common courtesy by asking so don't hate me if this is no-no between developers :P
#16
03/27/2005 (8:27 pm)
Hmm didn't try that exactly... but I think my code still worked without making the physics active, i just did the AtRest()... of course I'm thinking back a bit (had to stop a couple days and relax and get some sleep)

Yup, working on a Diablo style RPG... lol no your not breaking common courtesy, well at least not to me :) I'm still working up things, but I've got a few tricks under my sleeve I'm thinking on, the basic of it you already have, clever collision and art, thats a good deal of it... the only other thing I *might* work on is some sort of dynamic shading system, where you put a light in game, specify some value that represents heigth, then it automaticaly applies a preset shadow map in the right direction and proportioned, etc... not sure how deep I'll take that if I do... agian just some thoughts bouncing around to make things a bit more easy in the long run...
#17
03/28/2005 (3:45 am)
Which object is without physics? When I turn off physics, onCollision doesn't seem to even get called.... actually, it doesn't ever seem to get called, even when the box and pillar collide...
onCollisionCallback is true for each of my objects currently, but never called.

Shadows and dynamic lighting are in my plans, though I'm still thinking about the implementation :)
How are you handling player rotation? I'm thinking of rendering my player in 8 directions, but when the player turns to another direction I rotate the current animation 45° to the next animation, then set rotation back to 0° so next animation is facing correct direct. I haven't tried it, but it might work.



EDIT:
[sheepishly] Umm... ok, my stupidity will now be made known: I forgot to load my collision.cs file...
Don't hurt me! I knew it had to be something dumb I was forgetting on my part. It works now.
And here I thought I was being all clean and concise with my files... doh!
It seems most of my bugs are of this variety :P [/sheepishly]

Now, on to figure out how to create a schedule loop the right way...
#18
03/28/2005 (7:00 am)
Hmm yeah I was considering the same thing as well... player rotation without having sprites for every angle lol... I was thinking of something along the lines of this... 8 directions (like yours, or similar) then I rotate the weapon slightly to compensate for those sub directions, not sure how this will look... but then agian my teams RPG uses guns mainly, so a bit different than a medeival RPG. Its kind of like Diablo meets Resident Evil.



RE: EDIT:.... lol no worries, I was going to ask something like that since I tested your file and it worked fine... things like that happen :) I'm keeping all my exec files in exec.cs ... that way I can see them all together easily
#19
03/28/2005 (9:43 am)
Hey Matt, how did you set up your window to display the bounding box, collision poly, mount nodes, mount link etc...

This looks really useful

Richard
#20
03/28/2005 (9:45 am)
T2D Simle Debug Button Gui

I did a resource on that (yeah helps me a lot also having simple buttons to click lol :) )... I also have a bunch of helpful links on this resource

Torque 2D Unofficial F.A.Q.
Page «Previous 1 2