Authoring a Brush Entity: Difference between revisions
m (added russian otherlang) |
TomEdwards (talk | contribs) (partial rewrite) |
||
Line 1: | Line 1: | ||
''This tutorial assumes you have completed and understood [[Authoring a Logical Entity]] and, ideally, [[Authoring a Model Entity]].'' | |||
Our last example dealt with [[Authoring a Model Entity|giving entities a model]]. Here we'll use world architecture ("[[brush]]es") 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 == | |||
<span style="color:blue;">#include</span> <span style="color:brown;">"cbase.h"</span> | |||
<span style="color:blue;">class</span> CMyBrushEntity : <span style="color:blue;">public</span> CBaseToggle | |||
{ | |||
<span style="color:blue;">public</span>: | |||
DECLARE_CLASS( CMyBrushEntity, CBaseToggle ); | |||
DECLARE_DATADESC(); | |||
<span style="color:blue;">void</span> Spawn(); | |||
<span style="color:blue;">bool</span> CreateVPhysics(); | |||
<span style="color:blue;">void</span> BrushTouch( CBaseEntity *pOther ); | |||
}; | |||
LINK_ENTITY_TO_CLASS( my_brush_entity, CMyBrushEntity ); | |||
BEGIN_DATADESC( CMyBrushEntity ) | |||
<span style="color:green;">// Declare this function as being a touch function</span> | |||
DEFINE_ENTITYFUNC( BrushTouch ), | |||
END_DATADESC() | |||
A brush entity can inherit from <code>CBaseEntity</code> if it really wants to, but in our example we'll take advantage of some code that's already written in <code>CBaseToggle</code>.<!-- Which is deprecated now, so I'll have to figure out the proper baseclass before finishing --> | |||
<code>DEFINE_ENTITYFUNC</code> 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), [[trigger]]s, and so on. | |||
< | |||
There is no constructor for this entity as there are no variables to initialise. | |||
== Spawn() == | |||
<span style="color:blue;">void</span> CMyBrushEntity::Spawn() | |||
{ | |||
= | <span style="color:green;">// We want to capture touches from other entities</span> | ||
SetTouch( &CMyBrushEntity::BrushTouch ); | |||
<span style="color:green;">// We should collide with physics</span> | |||
SetSolid( SOLID_VPHYSICS ); | |||
<span style="color:green;">// We push things out of our way</span> | |||
SetMoveType( MOVETYPE_PUSH ); | |||
<span style="color:green;">// Use our brushmodel</span> | |||
SetModel( STRING( GetModelName() ) ); | |||
</ | |||
<span style="color:green;">// Create our physics hull information</span> | |||
CreateVPhysics(); | |||
} | |||
{ | |||
} | |||
The first thing we do in this block is setup our touch function to point to <i>BrushTouch()</i> where we’ll do our movement code. Next we tell the entity to use <code>SOLID_VPHYSICS</code> so we’ll use our exact bounds to collide. Setting the entity to <code>MOVETYPE_PUSH</code> means that we’ll attempt to move entities out of our way, instead of just being blocked. | The first thing we do in this block is setup our touch function to point to <i>BrushTouch()</i> where we’ll do our movement code. Next we tell the entity to use <code>SOLID_VPHYSICS</code> so we’ll use our exact bounds to collide. Setting the entity to <code>MOVETYPE_PUSH</code> means that we’ll attempt to move entities out of our way, instead of just being blocked. | ||
Line 68: | Line 58: | ||
In this example we use the <code>SetModel()</code> with our model name from the editor. In this case it tells the entity to use its brush model, as defined in the map. | In this example we use the <code>SetModel()</code> 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 <code>CreateVPhysics()</code> 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. | Finally, we call <code>CreateVPhysics()</code> 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.<!-- Why is this a separate function? The returned value is never used... --> | ||
== | == BrushTouch() == | ||
The entity has been told to notify us when its been touched, via the <code>BrushTouch()</code> 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 <code>trace_t</code> structure, returned by the <code>GetTouchTrace()</code> function. This returns the actual trace collision that generated the event. | The entity has been told to notify us when its been touched, via the <code>BrushTouch()</code> 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 <code>trace_t</code> structure, returned by the <code>GetTouchTrace()</code> function. This returns the actual trace collision that generated the event. | ||
void CMyBrushEntity::BrushTouch( CBaseEntity *pOther ) | |||
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. | 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. | ||
Line 104: | Line 92: | ||
Finally, we use the <code>LinearMove()</code> function to cause the brush to move to a location at a given speed. The <code>LinearMove()</code> function is implemented by <code>CBaseToggle</code> and takes care of behind-the-scenes maintenance in how the brush model moves. | Finally, we use the <code>LinearMove()</code> function to cause the brush to move to a location at a given speed. The <code>LinearMove()</code> function is implemented by <code>CBaseToggle</code> 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." | |||
[ | @SolidClass base(Targetname) = my_brush_entity : "Tutorial brush entity." | ||
[ | |||
] | |||
] | |||
== See also == | |||
*[[Brush Entity Code|Tutorial code in full]] | |||
*[[My First Entity]] | |||
{{otherlang:en}} | {{otherlang:en}} | ||
{{otherlang:en:ru|Authoring a Brush Entity:ru}} | {{otherlang:en:ru|Authoring a Brush Entity:ru}} | ||
[[Category:Programming]] | [[Category:Programming]] |
Revision as of 13:56, 2 March 2008
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." [ ]