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: 5808
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 16 2003 at 04: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 06: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 11:29:30
nice on omega i'll be useing this when i rebild my mod

Posted By: MIFUNE on Oct 14 2005 at 15: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, 16:42:38

Posted By: MIFUNE on Oct 14 2005 at 15: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 18: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 18: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, 18:49:30

Posted By: omega on Oct 14 2005 at 19: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 19: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, 19:56:42

Posted By: omega on Oct 14 2005 at 20: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 22: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 22:20:53
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 CODE IN THIS TUTORIAL. IT WAS FROM X-0UTS OTHER ONE*.
I'm sorry, just I get really irritated sometimes with repetition :(

Posted By: MIFUNE on Oct 14 2005 at 22: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, 22: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 05 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
knock knock
General | News
By: MIFUNE | Dec 31 2017
 
knock knock
General | News
By: omega | Dec 22 2016
 
knock knock
General | News
By: MIFUNE | Oct 10 2015
 
New HL HUD Message System
Half-Life | Coding | Shared Tutorials
By: chbrules | Dec 31 2011
 
knock knock
General | News
By: Whistler | Nov 05 2011
 
Particle Engine tutorial part 4
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 18 2010
 
Particle Engine tutorial part 2
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 11 2010
 
Particle Engine tutorial part 3
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 11 2010
 
Game Movement Series #2: Analog Jumping and Floating
Half-Life 2 | Coding | Shared Tutorials
By: mars3554 | Oct 26 2009
 
Particle Engine tutorial part 5
Half-Life | Coding | Client Side Tutorials
By: Deadpool | Aug 02 2009
 

Site Info
297 Approved Articless
8 Pending Articles
3940 Registered Members
0 People Online (5 guests)
About - Credits - Contact Us

Wavelength version: 3.0.0.9
Valid XHTML 1.0! Valid CSS!