Authoring a Brush Entity
This tutorial assumes you have completed and understood Authoring a Logical Entity and, ideally, Authoring a Model Entity.
Our last example dealt with giving entities a model. Here we'll use world architecture ("brushes") to represent our entity and to decide how it collides and moves around the world. We'll also look at the touch function, available to all entities, which we will use to make the entity move when touched.
Declaration and DATADESC
#include "cbase.h" class CMyBrushEntity : public CBaseToggle { public: DECLARE_CLASS( CMyBrushEntity, CBaseToggle ); DECLARE_DATADESC(); void Spawn(); bool CreateVPhysics(); void BrushTouch( CBaseEntity *pOther ); }; LINK_ENTITY_TO_CLASS( my_brush_entity, CMyBrushEntity ); BEGIN_DATADESC( CMyBrushEntity ) // Declare this function as being a touch function DEFINE_ENTITYFUNC( BrushTouch ), END_DATADESC()
A brush entity can inherit from CBaseEntity
if it really wants to, but in our example we'll take advantage of some code that's already written in CBaseToggle
.
DEFINE_ENTITYFUNC
is a badly-named command to declare what function should be executed OnTouch - i.e. when the entity touches or is touched by another. Aside from our intended use, it's good for for explode-on-contact ordnance, flying entities colliding with things, picking up health etc. (on foot or in a vehicle), triggers, and so on.
There is no constructor for this entity as there are no variables to initialise.
Spawn()
void CMyBrushEntity::Spawn() { // We want to capture touches from other entities SetTouch( &CMyBrushEntity::BrushTouch ); // We should collide with physics SetSolid( SOLID_VPHYSICS ); // We push things out of our way SetMoveType( MOVETYPE_PUSH ); // Use our brushmodel SetModel( STRING( GetModelName() ) ); // Create our physics hull information CreateVPhysics(); }
The first thing we do in this block is setup our touch function to point to BrushTouch() where we’ll do our movement code. Next we tell the entity to use SOLID_VPHYSICS
so we’ll use our exact bounds to collide. Setting the entity to MOVETYPE_PUSH
means that we’ll attempt to move entities out of our way, instead of just being blocked.
In this example we use the SetModel()
with our model name from the editor. In this case it tells the entity to use its brush model, as defined in the map.
CreateVPhysics()
bool CMyBrushEntity::CreateVPhysics() { // For collisions with physics objects VPhysicsInitShadow( false, false ); return true; }
Finally, we call CreateVPhysics()
to setup our collision shadow. This is what we’ll use to collide with physics objects in the world. Without this, the brush would pass through those objects.
BrushTouch()
The entity has been told to notify us when its been touched, via the BrushTouch()
function. When we receive this notification, we’ll cause the entity to move away from the entity that touched it. To do this, we’ll need information about the events surrounding the touch. This information is provided in the trace_t
structure, returned by the GetTouchTrace()
function. This returns the actual trace collision that generated the event.
void CMyBrushEntity::BrushTouch( CBaseEntity *pOther ) { // Get the collision information const trace_t &tr = GetTouchTrace(); // We want to move away from the impact point along our surface Vector vecPushDir = tr.plane.normal; vecPushDir.Negate(); vecPushDir.z = 0.0f; // Move slowly in that direction LinearMove( GetAbsOrigin() + ( vecPushDir * 64.0f ), 32.0f ); }
First we retrieve the normal of the surface that was hit. In our case, this will be one of the planes of the brush entity. We negate that value to point towards the direction of the impact, and then remove the Z component of the direction to keep us parallel to the floor.
Finally, we use the LinearMove()
function to cause the brush to move to a location at a given speed. The LinearMove()
function is implemented by CBaseToggle
and takes care of behind-the-scenes maintenance in how the brush model moves.
FGD entry
@include "base.fgd" @SolidClass base(Targetname) = my_brush_entity : "Tutorial brush entity." [ ]