Welcome, Guest! Login | Register

Automatically set entity collision box by model [Print this Article]
Posted by: omega
Date posted: Apr 28 2003
User Rating: N/A
Number of views: 2829
Number of comments: 13
Description: (Extension to X-0ut's custom model tut)
While reading through X-0uts tutorial I decided that i should write a simple tutorial on UTIL_SetSize() (essentially). This tutorial will show you how to automatically set any entities bounding box for entity to entity collision, based on the model's box. This could be useful for weapon pickups other items, and most definitely the most important: monsters!

The significant changes we will be making are contained in cbase.h, So open it up and do the following:
include studio.h with the rest of the includes.
 CODE (C++) 

#include "../engine/studio.h"

Next, somewhere inside the CBaseEntity class, add this function:
 CODE (C++) 

void SetModelCollisionBox()
{
    studiohdr_t *pstudiohdr;
    pstudiohdr = (studiohdr_t*)GET_MODEL_PTR( ENT(pev) );

    if (pstudiohdr == NULL)
    {
        ALERT(at_console,"Unable to fetch model pointer!\n");
        return;
    }
    mstudioseqdesc_t    *pseqdesc;
    pseqdesc = (mstudioseqdesc_t *)((byte *)pstudiohdr + pstudiohdr->seqindex);
    UTIL_SetSize(pev,pseqdesc[ pev->sequence ].bbmin,pseqdesc[ pev->sequence ].bbmax);

}


Now, any time you wish to set the entities collision box based on its models bounds; Simply add a call to: SetModelCollisionBox(); inside one of the entities functions. Usually you will want to do this inside Spawn, at the bottom of the function. If you know that the entities mesh/bounding box scales up in different sequences, you'll want to call this every time the animation changes to make sure it's correct.

Following off of X-0ut's tutorial, to demonstrate this we will modify the spawn function.
 CODE (C++) 

void CModel::Spawn( void )
{
---[SNIP]---
    SetThink(Animate);
    pev->nextthink = gpGlobals->time+0.5;
    SetModelCollisionBox(); //Build our new collision box based on our model.
}


Simple huh? Gotta love simple tutorials. I need to write something more intermediate, without direct code blocks =D

Rate This Article
This article has not yet been rated.

You have to register to rate this article.
User Comments Showing comments 1-13

Posted By: omega on Sep 15 2003 at 22:50:20
I just updated this tutorial to fix a minor bug, and reformat it with the C++ tags.

Posted By: harSens on Nov 12 2003 at 00:31:38
Nice, just what i needed for my env_models.

Note:
If you set a boundingbox in the qc, using $bbox, you can use pstudiohdr->max and pstudiohdr->min to get the boundingbox. This is very usefull if you want a smaller boundingbox than the model. We use it to put the box around the branch of a tree, but not around the leaves.

Posted By: mathew on Dec 04 2003 at 05:29:30
nice on omega i'll be useing this when i rebild my mod

Posted By: MIFUNE on Oct 14 2005 at 09:17:10
Hello OMEGA. I´ve found this when I try to compile your tutorial:


--------------------Configuration: hl - Win32 Debug--------------------
Compiling...
sentinel.cpp
D:\JUEGOS\SIERRA\PROGRAMAS DESARROLLO MODS\HLSDK2.3\Single-Player Source\dlls\yourmonster.cpp(607) : error C2065: 'Animate' : undeclared identifier
D:\JUEGOS\SIERRA\PROGRAMAS DESARROLLO MODS\HLSDK2.3\Single-Player Source\dlls\yourmonster.cpp(607) : error C2440: 'static_cast' : cannot convert from 'int' to 'void (__thiscall CBaseEntity::*)(void)'
There are no conversions from integral values to pointer-to-member values
Error executing cl.exe.

hl.dll - 2 error(s), 0 warning(s)


I was completely lost about what was the problem, then I realized that the compiler asked for a function, then I added
under:

int CYourmonster :: Classify ( void )

this:


//------------------------------------------------------------
// Omega´s Set Collision Box by model Tutorial
//------------------------------------------------------------

void CYourmonster::Animate( void )
{
pev->nextthink = gpGlobals->time + 0.5;
}

and, of course in the function declarations under :

void BecomeDead(void);

I added.

void EXPORT Animate (void);

And all goes fine, it compiled without problems.Edited by MIFUNE on Oct 14 2005, 10:42:38

Posted By: MIFUNE on Oct 14 2005 at 09:29:18
Excuse me, I wrote that because I want to know If adding that function I should screw something...

Posted By: omega on Oct 14 2005 at 12:14:10
wait, no it's not.

that little bit of code was from x-0ut's tutorial, with my tutorial code added in. try reading it better next time ;)

Posted By: MIFUNE on Oct 14 2005 at 12:34:08
you say that "that's what you're supposed to do"...OMG, I screwed something , no?. Excuse me if I´m misunderstanding you, but my english is not very good. The monster works fine, BTW. I fear of a breakdown or similar because ( if Persuter reads this he´ll curse me for sure :D ) I didn´t debugged the code.Edited by MIFUNE on Oct 14 2005, 12:49:30

Posted By: omega on Oct 14 2005 at 13:30:30
just add the SetModelCollisionBox(); function to your spawn somewhere.
the animate thing was from x-0ut's tutorial; not mine.

Posted By: MIFUNE on Oct 14 2005 at 13:52:34
So I must add X-OUT´s part into the SPAWN part.

I´ll do it. :) Thanks a lot.Edited by MIFUNE on Oct 14 2005, 13:56:42

Posted By: omega on Oct 14 2005 at 14:57:00
NOOOO!!!!!
x-0ut's thing is a TOTALLY SEPERATE ENTITY!!!
i used his as a base for the *EXAMPLE*!!!!

Posted By: MIFUNE on Oct 14 2005 at 16:13:07
Oh...so, I must left the things I did untouched?. Oh, crap!! Sorry, It´s clear that X-Out´s code must go in cbase.h ,what a silly I am!! Thanks Omega for your patience. ^_^

Posted By: omega on Oct 14 2005 at 16:20:53
oh my god.

just take the function SetModelCollisionBox(); add it to CBaseEntity; DONE! now in your spawn function for whatever you wanna use it on, use SetModelCollisionBox(); *IGNORE ALL OF THE REST OF THE GODDAMNED CODE IN THIS TUTORIAL.* obviously you don't understand english very well. and im getting tired of repeating myself.

Posted By: MIFUNE on Oct 14 2005 at 16:25:34
Sorry, Omega, I didn´t want to piss you off. I understand you now ( C++ newbie + english level 3 ( of 6 ) = disaster ) .Edited by MIFUNE on Oct 14 2005, 16:29:26


You must register to post a comment. If you have already registered, you must login.

Latest Articles
3rd person View in Multiplayer
Half-Life 2 | Coding | Client Side Tutorials
How to enable it in HL2DM

By: cct | Nov 13 2006

Making a Camera
Half-Life 2 | Level Design
This camera is good for when you join a map, it gives you a view of the map before you join a team

By: slackiller | Mar 04 2006

Making a camera , Part 2
Half-Life 2 | Level Design
these cameras are working monitors that turn on when a button is pushed.

By: slackiller | Mar 04 2006

Storing weapons on ladder
Half-Life 2 | Coding | Snippets
like Raven Sheild or BF2

By: British_Bomber | Dec 24 2005

Implementation of a string lookup table
Half-Life 2 | Coding | Snippets
A string lookup table is a set of functions that is used to convert strings to pre-defined values

By: deathz0rz | Nov 13 2005


Latest Comments
Spinning Corpses Simple Fix
Half-Life | Coding | Snippets
By: darkPhoenix | Sep 05 2008
 
Where do we go from here
General | News
By: MIFUNE | Jun 09 2008
 
The Input/Output system
Half-Life 2 | Level Design
By: nazitaco | Dec 23 2007
 
Where do we go from here
General | News
By: Rob_F | Nov 22 2007
 
Rescaling Half-Life
Half-Life | Coding | Shared Tutorials
By: christoph | Nov 12 2007
 
GameUI
Half-Life 2 | Coding | Client Side Tutorials
By: Evil_j | Oct 29 2007
 
3 State Zoom For Any Weapon
Half-Life 2 | Coding | Server Side Tutorials
By: Ennuified | Oct 18 2007
 
Storing weapons on ladder
Half-Life 2 | Coding | Snippets
By: cct | Sep 07 2007
 
CTF Gameplay Part 1
Half-Life | Coding | Shared Tutorials
By: DarkNight | Aug 28 2007
 
CTF Gameplay Part 1
Half-Life | Coding | Shared Tutorials
By: deedok | Aug 20 2007
 

Site Info
296 Approved Articless
5 Pending Articles
3940 Registered Members
2 People Online (23 guests)
About - Credits - Contact Us

Wavelength version: 3.0.0.9
Valid XHTML 1.0! Valid CSS!