CastCollision - How to make it return a value?
by Dave Calabrese · in Torque Game Builder · 06/20/2008 (2:05 pm) · 6 replies
So I'm playing around with platformer physics and collision, and I need to be able to check if I'm colliding with something. I've tried using the 'castCollision' method to do this, however I'm not getting any responses from it. I have created a behavior that controls all the movements for the character, applied that to an object, and I can move around and do everything just fine. Here is the main code for this:
The problem here is that my output from the 'updateVerticle' functions reads like so:
From what I've read, the above code should return something for 'collision'. I've checked and my collision groups are just fine, I can physically collide with the objects as well - it's just have the castCollision method detect them is not working at all.
Any thoughts on how to make castCollision work?
-Dave Calabrese
function playerDefineBehavior::onUpdate(%this)
{
%this.updateHorizontal();
%this.updateVerticle();
}
function playerDefineBehavior::updateHorizontal(%this)
{
if(%this.moveRight && %this.moveLeft)
{
%this.owner.setLinearVelocityX(0);
%this.owner.setConstantForceY(0);
%this.owner.setLinearVelocityY(0);
return;
}
if(!%this.moveRight && !%this.moveLeft)
{
%this.owner.setLinearVelocityX(0);
%this.owner.setConstantForceY(0);
%this.owner.setLinearVelocityY(0);
return;
}
if(%this.moveRight)
{
%this.owner.setLinearVelocityX(%this.movementSpeed);
%this.owner.setConstantForceY(100);
return;
}
if(%this.moveLeft)
{
%this.owner.setLinearVelocityX((%this.movementSpeed) * -1);
%this.owner.setConstantForceY(100);
return;
}
}
function playerDefineBehavior::updateVerticle(%this)
{
%yVelocity = %this.owner.getLinearVelocityY();
%collision = %this.owner.castCollision(0.005);
%normalX = getWord(%collision, 4);
%normalY = getWord(%collision, 5);
error("@@@ Collision = " @ %collision);
error("@@@ normalX = " @ %normalX);
error("@@@ normalY = " @ %normalY);
error("@@@ yVelocity = " @ %yVelocity);
}The problem here is that my output from the 'updateVerticle' functions reads like so:
@@@ Collision = @@@ normalX = @@@ normalY = @@@ yVelocity = 0
From what I've read, the above code should return something for 'collision'. I've checked and my collision groups are just fine, I can physically collide with the objects as well - it's just have the castCollision method detect them is not working at all.
Any thoughts on how to make castCollision work?
-Dave Calabrese
About the author
Recent Threads
#2
-Dave Calabrese
06/20/2008 (5:25 pm)
That was it! Makes perfect sense, too. Thanks a ton, Phillip!-Dave Calabrese
#3
06/20/2008 (5:42 pm)
Well, this all seems to be working now, except when my player is on a slope. Whenever the player is on a slope, it cannot detect future collisions. Turning on the poly view, it looks like it should be detecting it... yet it's not. Very curious... any thoughts?
#4
06/20/2008 (6:09 pm)
Slopes are pretty tough to do properly. The Platformer Starter Kit implements them properly, but it was a pretty difficult system to implement.
#5
Check out where we do a Collision at A and at B. Both are echoing out the exact same variable. However, at 'A', it echoes out the full collision statement. And at B, it shows the statement as blank! Somewhere between A and B, it's becoming emptied, and is causing false no-collisions to trigger. That's a bit weird now...
06/20/2008 (6:28 pm)
I've almost got it working now, though I have an incredibly weird error. Check out my code here:function playerDefineBehavior::updateVerticle(%this)
{
%yVelocity = %this.owner.getLinearVelocityY();
%xVelocity = %this.owner.getLinearVelocityX();
//Add a velocity buffer so we can project any future collisions
%this.owner.setLinearVelocityY(%yVelocity + 50);
//Check for future collisions
%collision = %this.owner.castCollision(0.005);
%normalX = getWord(%collision, 4);
%normalY = getWord(%collision, 5);
debugBoxDumper.setText(%collision);
error("@@@ Collision at A: " @ %collision);
//Set the velocity back to what it currently is
%this.owner.setLinearVelocityY(%yVelocity);
// no collision
if (%collision == "")
{
error("@@@ Collision at B: " @ %collision);
%this.airborne = true;
%this.owner.setConstantForceY(80);
%this.owner.setLinearVelocityY(%yVelocity);
return;
}
}Check out where we do a Collision at A and at B. Both are echoing out the exact same variable. However, at 'A', it echoes out the full collision statement. And at B, it shows the statement as blank! Somewhere between A and B, it's becoming emptied, and is causing false no-collisions to trigger. That's a bit weird now...
#6
-Dave Calabrese
06/20/2008 (6:46 pm)
Ahhhh okay. I got it figured out. The above is just bizarre, but I suspect I was doing something wrong. The problem was I didn't have enough checks... I now am doing a number of additional checks and slopes are working fine!-Dave Calabrese
Associate Phillip O'Shea
Violent Tulip
function playerDefineBehavior::updateVerticle(%this) { %yVelocity = %this.owner.getLinearVelocityY(); // Apply a quick buffer %this.owner.setLinearVelocityY(%yVelocity + 50); // Cast the collision %collision = %this.owner.castCollision(0.005); // Revert back to the original velocity %this.owner.setLinearVelocityY(%yVelocity); %normalX = getWord(%collision, 4); %normalY = getWord(%collision, 5); error("@@@ Collision = " @ %collision); error("@@@ normalX = " @ %normalX); error("@@@ normalY = " @ %normalY); error("@@@ yVelocity = " @ %yVelocity); }