Hitting Collision?
by Bobby Leighton · in Torque 3D Professional · 11/29/2010 (5:02 pm) · 51 replies
Ok I'm working on a zombie shooter game, and I am trying to work out a few things with projectile collision, here is what I want to do:
When projectile hits a certain collision marker I need it to return information about which on it hit, and then execute code based on which one it was.
I already have a test object setup with 9 collision markers setup, and collision is working. I can also get information that the collisions are returning values I just don't know how to separate from one collision box to another. Any help would be greatly appreciated!
Bobby
When projectile hits a certain collision marker I need it to return information about which on it hit, and then execute code based on which one it was.
I already have a test object setup with 9 collision markers setup, and collision is working. I can also get information that the collisions are returning values I just don't know how to separate from one collision box to another. Any help would be greatly appreciated!
Bobby
#22
I was afraid that last line (the interpolate call) might cause an issue, but I also think I neglected to notice that "info" gets overwritten by "shortest" even if no hitboxes are found. I'll post back (probably will just update this post) once I've built a working solution, shouldn't take long.
12/05/2010 (9:01 pm)
Quote:Ok I made the script changes then made the code changes. C++ changes Built, but when aiming at a bot with no hitbox it gives a "Out of bound interpolation factor", and wont let me active Debugger, but I will take these changes out for now and test a Player with a hitbox as my wall won't work:O)
I was afraid that last line (the interpolate call) might cause an issue, but I also think I neglected to notice that "info" gets overwritten by "shortest" even if no hitboxes are found. I'll post back (probably will just update this post) once I've built a working solution, shouldn't take long.
#23
12/05/2010 (9:41 pm)
Great! Also ive been look, and after so many changes I dont know what the hitboxes should be named as. i see some say they should be HB10, or should it be HBa0, just want to fix that problem before it is one:O)
#24
?
12/05/2010 (10:02 pm)
I definitely am not making the boxes right if they are visible I assume, did you make similar changes to comment #62 on http://www.torquepowered.com/community/resource/view/6538/4#comments?
#25
As far as the detail level thing, I actually place my hitboxes in the highest LOD. So, in one model I have HBA192, HBB192, etc. And yeah, they come out visible, so I just apply an invisible material to them.
By invisible material I mean a material with no diffuse texture (or any other type of texture) with Alpha Threshold (Advanced, last tab of Material) 255. Because there's no texture and max threshold (actually anything over 0 works in this case) no rendering is done on the material at all, so it shouldn't cause any artifacts. Also make sure shadow casting is disabled.
I feel fairly certain that there's a way to toggle a mesh invisible in Max and have that carry over to DTS... In fact I recall that you could animate this and it would work. That said I can't seem to even find the "visible" option in 3DS anymore, you might have more luck.
I do remember reading #62, but I personally felt it wasn't required. That said it might work to automatically make the HB's invisible, I've just never tried it.
------
I'll just toss this in here, actually went ahead and tested it this time. My previous code for defaulting back to bounding boxes was correct however I forgot one bit. There's a part of the Player::castRay function that looks like this:
Firing at unaltered Gideon right now, it's hitting his bounding box and returning -1 as the hitbox ID to indicate no specific HB was hit. Also tested vs. my model w/ hitboxes, it's working normally.
I'm going to do something similar for Vehicle next and will post that too just in case anyone's intereseted. I'd post it on the resource comments instead but the character limit makes that impossible.
12/05/2010 (11:40 pm)
The T3D version of the resource uses HBA0, HBB0, etc. Apparently there was some change made that made using the old format (HB1_0 I believe) impossible. because it would confuse the 1 with the detail level.As far as the detail level thing, I actually place my hitboxes in the highest LOD. So, in one model I have HBA192, HBB192, etc. And yeah, they come out visible, so I just apply an invisible material to them.
By invisible material I mean a material with no diffuse texture (or any other type of texture) with Alpha Threshold (Advanced, last tab of Material) 255. Because there's no texture and max threshold (actually anything over 0 works in this case) no rendering is done on the material at all, so it shouldn't cause any artifacts. Also make sure shadow casting is disabled.
I feel fairly certain that there's a way to toggle a mesh invisible in Max and have that carry over to DTS... In fact I recall that you could animate this and it would work. That said I can't seem to even find the "visible" option in 3DS anymore, you might have more luck.
I do remember reading #62, but I personally felt it wasn't required. That said it might work to automatically make the HB's invisible, I've just never tried it.
------
I'll just toss this in here, actually went ahead and tested it this time. My previous code for defaulting back to bounding boxes was correct however I forgot one bit. There's a part of the Player::castRay function that looks like this:
if (info->object == this)
{
// Copy out the shortest time...
*info = shortest;
}And that clears out the ray info data that was set from the initial BBox check, so do check for foundHB here as well:if (info->object == this && foundHB)
{
// Copy out the shortest time...
*info = shortest;
}Firing at unaltered Gideon right now, it's hitting his bounding box and returning -1 as the hitbox ID to indicate no specific HB was hit. Also tested vs. my model w/ hitboxes, it's working normally.
I'm going to do something similar for Vehicle next and will post that too just in case anyone's intereseted. I'd post it on the resource comments instead but the character limit makes that impossible.
#26
12/06/2010 (10:02 am)
Yes, when we are all done here with these changes I will post links here in the private forums to download the source files, to make them easier for others to add to their project:O) And thanks again Ill give this a try today:O)
#27
yeah, I'm very interested in a vehicle version,
more so than a player version,
I use damage mesh swapping,
and I need hitboxes for the vehicle body panels
12/06/2010 (6:57 pm)
@Henry,yeah, I'm very interested in a vehicle version,
more so than a player version,
I use damage mesh swapping,
and I need hitboxes for the vehicle body panels
#28
Instead of reverting to the bounding box like Player this will call Parent(ShapeBase)::castRay and return the results of that, meaning it will use whatever collision geometry has been supplied for the Vehicle.
Technically this method is adding a bit of extra work into the castRay effort for non-hitboxed Vehicles, as it now checks bounding box, checks for hitboxes, and then goes and calls the ShapeBase castRay. For maximum optimization you could add a hitbox count to ShapeBase and just dump straight to the Parent::castRay if it == 0, but I wouldn't bother unless your game really spams castRay beyond just using it for basic stuff like weapons fire.
12/06/2010 (8:22 pm)
Okay, here's an updated version of the castRay function for Vehicles. Again, there's no castRay function in Vehicle by default (it uses ShapeBase::castRay) so you'll have to add this function into Vehicle.cpp somewhere that seems appropriate, in addition to adding this line somewhere in the public section of the Vehicle Class in vehicle.h:bool Vehicle::castRay(const Point3F &start, const Point3F &end, RayInfo* info);Now the function itself, new bits maked with comment "fallback":
// [Vehicle Hitboxes]
// Added Player's custom raycast code from Hitbox resource
// Updated to fall back to default Vehicle rayCast if !hitboxes
bool Vehicle::castRay(const Point3F &start, const Point3F &end, RayInfo* info)
{
if (getDamageState() != Enabled)
return false;
// Collide against bounding box. Need at least this for the editor.
F32 st,et,fst = 0.0f,fet = 1.0f;
F32 *bmin = &mObjBox.minExtents.x;
F32 *bmax = &mObjBox.maxExtents.x;
F32 const *si = &start.x;
F32 const *ei = &end.x;
for (int i = 0; i < 3; i++) {
if (*si < *ei) {
if (*si > *bmax || *ei < *bmin)
return false;
F32 di = *ei - *si;
st = (*si < *bmin)? (*bmin - *si) / di: 0.0f;
et = (*ei > *bmax)? (*bmax - *si) / di: 1.0f;
}
else {
if (*ei > *bmax || *si < *bmin)
return false;
F32 di = *ei - *si;
st = (*si > *bmax)? (*bmax - *si) / di: 0.0f;
et = (*ei < *bmin)? (*bmin - *si) / di: 1.0f;
}
if (st > fst) fst = st;
if (et < fet) fet = et;
if (fet < fst)
return false;
bmin++; bmax++;
si++; ei++;
}
info->normal = start - end;
info->normal.normalizeSafe();
getTransform().mulV( info->normal );
info->t = fst;
info->object = this;
info->point.interpolate(start,end,fst);
info->material = 0;
// Hitboxes
//if we are here means that the ray has hit the bounding box, now check if an hit box was hit
//if it hit a collision box the HitBoxNum
//in the info struct is filled with the number of the box otherwise it remains -1
if (mShapeInstance)
{
RayInfo shortest;
shortest.t = 1e8;
if (isServerObject())
{
mShapeInstance->animate(0); //Animate the model on the server
}
bool foundHB = false; // Fallback
for (U32 i = 0; i < ShapeBaseData::Max_Hitboxes; i++)
{
if (mDataBlock->HBIndex[i] != -1)
{
foundHB = true; // Fallback
if (mShapeInstance->castRayEA(start, end, info,0,mDataBlock->HBIndex[i]))
{
info->object = this;
if (info->t < shortest.t)
{
shortest = *info;
shortest.HitBoxNum = i+1; // +1 because the meshes HB## begin from 1
}
}
}
}
if (info->object == this && foundHB) // Fallback
{
// Copy out the shortest time...
*info = shortest;
}
if (info->HitBoxNum == -1)// Fallback
{
if (foundHB)
return false;
else
return Parent::castRay(start, end, info);
}
info->point.interpolate(start,end,info->t);
}
return true;
}
// [/Vehicle Hitboxes]Instead of reverting to the bounding box like Player this will call Parent(ShapeBase)::castRay and return the results of that, meaning it will use whatever collision geometry has been supplied for the Vehicle.
Technically this method is adding a bit of extra work into the castRay effort for non-hitboxed Vehicles, as it now checks bounding box, checks for hitboxes, and then goes and calls the ShapeBase castRay. For maximum optimization you could add a hitbox count to ShapeBase and just dump straight to the Parent::castRay if it == 0, but I wouldn't bother unless your game really spams castRay beyond just using it for basic stuff like weapons fire.
#29
I am using this to try to get the number from the hitbox:
echo("ProjectileData::onCollision("@%data.getName()@", "@%proj@", "@%col.getClassName()@", "@%fade@", "@%pos@", "@%normal@", "@%hitbox@")");
All I Get is this:
ProjectileData::onCollision(RocketLauncherProjectile, 9390, AIPlayer, -0.0442304 0.998988 0.00814919, 3.20302 87.9488 0.647401, -0.0442304 0.998988 0.00814919, )
I am assuming that after the comma is where my data that I want is supposed to be, so I am unsure what I did wrong. Any Ideas how to troubleshot this? i would be willing to send you whatever files you need. Thank you again :O)
12/07/2010 (2:35 am)
Ok everything seems to be working, as far as now I can be killed, or kill bots that have a bounding box. I do have some running around with a single hitbox named HBA192, as you named yours. I am using this to try to get the number from the hitbox:
echo("ProjectileData::onCollision("@%data.getName()@", "@%proj@", "@%col.getClassName()@", "@%fade@", "@%pos@", "@%normal@", "@%hitbox@")");
All I Get is this:
ProjectileData::onCollision(RocketLauncherProjectile, 9390, AIPlayer, -0.0442304 0.998988 0.00814919, 3.20302 87.9488 0.647401, -0.0442304 0.998988 0.00814919, )
I am assuming that after the comma is where my data that I want is supposed to be, so I am unsure what I did wrong. Any Ideas how to troubleshot this? i would be willing to send you whatever files you need. Thank you again :O)
#30
I used the highest detail level in the model and it doesn't seem to be an issue (it searches by node name, and the node's name is actually "HBA" so it doesn't matter what the detail # is as far as the hitbox code is concerned).
As far as your issue, it should never be returning just a blank even if you don't hit a hitbox or the Player doesn't have one. Assuming %hitbox got added to the params of the script onCollision function, either something went wrong in the callback stuff in Projectile.cpp or you missed something somewhere in the resource. Instead of trying to find the problem, some more debug output might be easier.
In Projectile::onCollision, add this:
Con::printf("Projectile::onCollision; hitbox ID: %d", hitBox);
right before:
mDataBlock->onCollision_callback( this, hitObject, mFadeValue, hitPosition, hitNormal, hitBox );"
And in Projectile::simulate I might also suggest:
Con::printf("Projectile::simulate; hitbox ID: %d", rInfo.HitBoxNum);
right before:
onCollision(rInfo.point, rInfo.normal, rInfo.object, rInfo.HitBoxNum);
If both of these check out when you shoot a Player (even if it returns -1) then at least the hitboxes are working internally and the error is in getting that info to the script through the Callback system.
12/07/2010 (7:16 am)
I should mention that my "HBA192" only works because I have a detail level 192 in my model, in your case you want to use a detail level that exists in your model. IE if you have a detail500 use HBA500, etc. If you use a detail # that doesn't exist in your model the box won't be exported.I used the highest detail level in the model and it doesn't seem to be an issue (it searches by node name, and the node's name is actually "HBA" so it doesn't matter what the detail # is as far as the hitbox code is concerned).
As far as your issue, it should never be returning just a blank even if you don't hit a hitbox or the Player doesn't have one. Assuming %hitbox got added to the params of the script onCollision function, either something went wrong in the callback stuff in Projectile.cpp or you missed something somewhere in the resource. Instead of trying to find the problem, some more debug output might be easier.
In Projectile::onCollision, add this:
Con::printf("Projectile::onCollision; hitbox ID: %d", hitBox);
right before:
mDataBlock->onCollision_callback( this, hitObject, mFadeValue, hitPosition, hitNormal, hitBox );"
And in Projectile::simulate I might also suggest:
Con::printf("Projectile::simulate; hitbox ID: %d", rInfo.HitBoxNum);
right before:
onCollision(rInfo.point, rInfo.normal, rInfo.object, rInfo.HitBoxNum);
If both of these check out when you shoot a Player (even if it returns -1) then at least the hitboxes are working internally and the error is in getting that info to the script through the Callback system.
#31
So at least now I know it is my model, and that the code itself is working, I had forgot a ,hitbox entry as I went to put in the debug stuff you asked for, which i still put in. So far everything seems to be working right, I'm gonna work on the hitboxs now to iron those out, and then Im going to start working on a small tutorial write up based on our conversations here with the code. Ill put it here in the forums so only licenced people can get it, and Ill try to make sure that it stays somewhere that others can get it, even if IA/GG shuts down the website here.
12/07/2010 (5:51 pm)
Wooot! We got a -1! :O)So at least now I know it is my model, and that the code itself is working, I had forgot a ,hitbox entry as I went to put in the debug stuff you asked for, which i still put in. So far everything seems to be working right, I'm gonna work on the hitboxs now to iron those out, and then Im going to start working on a small tutorial write up based on our conversations here with the code. Ill put it here in the forums so only licenced people can get it, and Ill try to make sure that it stays somewhere that others can get it, even if IA/GG shuts down the website here.
#33
12/07/2010 (7:41 pm)
Well done Bobby!
#34
http://www.mediafire.com/file/5nficucujm0qt0g/HitBox%20Code.rar
For those that will need instructions I will be writing a tutorial about installing this, including all the .cs scripts that go with it. I haven't included those in that rar because the ones I was testing with has script changes(UAISK), so I can't include those, but I will make some that I can release, with the tutorial. Also I will breifly go over how to add hitboxes, still working on the "what I am doing for the scripting example", but Ill get there.
Also this would have been possible with out help, so thank you Mr. Todd:O)
12/07/2010 (10:44 pm)
For those that have no problem building the engine, (I have been building this in debug, but release "shouldn't" give you any problems)this is what you want:http://www.mediafire.com/file/5nficucujm0qt0g/HitBox%20Code.rar
For those that will need instructions I will be writing a tutorial about installing this, including all the .cs scripts that go with it. I haven't included those in that rar because the ones I was testing with has script changes(UAISK), so I can't include those, but I will make some that I can release, with the tutorial. Also I will breifly go over how to add hitboxes, still working on the "what I am doing for the scripting example", but Ill get there.
Also this would have been possible with out help, so thank you Mr. Todd:O)
#35
12/07/2010 (11:11 pm)
hey Bobby, could I get those scripts with the UAISK? I own that and would be doing exactly that too!! ;o)
#36
those files you uploaded, are exactly the same as the originals.
I diffed them, identical to stock,
me thinks you uploaded the wrong files.
12/07/2010 (11:56 pm)
mmm, Bobby,those files you uploaded, are exactly the same as the originals.
I diffed them, identical to stock,
me thinks you uploaded the wrong files.
#37
and you can have those just drop me your email:O)
12/08/2010 (3:26 am)
Hmmm lemme look I thought I took it from the new build sorry bout that:O(and you can have those just drop me your email:O)
#38
http://www.mediafire.com/file/gpmr1xn4luprgfl/HitBox%20Code.rar
let me know please, and Ill get those scripts to you soon, my wife has my time at the moment:O)
12/08/2010 (3:52 am)
Ok this one might be better:http://www.mediafire.com/file/gpmr1xn4luprgfl/HitBox%20Code.rar
let me know please, and Ill get those scripts to you soon, my wife has my time at the moment:O)
#40
a successful build,
but sceneObject.h and tsShapeInstance.cpp are exactly the same as the originals.
are they supposed to be?
12/08/2010 (2:13 pm)
@Bobby,a successful build,
but sceneObject.h and tsShapeInstance.cpp are exactly the same as the originals.
are they supposed to be?
Torque Owner Bobby Leighton
Imagn' Games