Alex Russell

April 2004

 

 

Attaching Entities to Each Other

 

From making the player grab a gun to attaching a turret to a Panzer, there are many situations where we need to attach one entity to another.

There are different ways to approach this problem depending on the situation. You may just want to stick something on at a certain point of a model, like when you want the player to pick objects up. You might require that the attached model moves perfectly in sync with the parent model. Examples of this might be hair, clothes, or body parts that you want to be able to change during game play.

Attaching Entities to a Point on a Model

We can find the position of any point on a model during a game via c-script. You can then attach another entity to this point. Here is how it is done:

1) Open up MED and the model that you want to attach something to.

 

2) Make sure you are in select mode. Chose the Vertex that you want to attach other entities to, and left click on it.

3) Alone the bottom panel of WED you will see Vertex#. Write down this number

 

Now you have the number of the vertex you are going to attach something to. Time for a bit of coding.

To find out where your vertex is at any time during the game, use this command:

vec_for_vertex(temp, entity, vertex_number)

This will set the "temp" vector to the position of the vertex we want to attach another entity to. "Temp" is the vector to set, "entity" is a pointer to the entity that we want to attach something to (maybe "my") and "vertex_number" should be the number of the vertex that you just found in MED.

Let's see that in action. I will attach the fish model that comes with 3dgs to the destructatron's horn. We already know that the vertex number for the point at the top of his horn is #218. There are two ways we could do this. We could have both entities already in the level and just make the fish jump onto the robot's horn, or we could create the fish at the horn point at keep it there. Both are just as easy. For this example I will create the fish on top of his horn.

 

entity* destructabot; //It is often useful to give entities thier own pointer or a handle. You could just use the "you" variable if you are always going to call the fish action from the destructatron action.

action fish //keeps the fish at the same location as the point on the horn when the destructatron moves around
{

my.passable=on; //passable must be set or it will be forever colliding with the robot.

while(my!=null)
{
vec_for_vertex(temp, destructabot, 218); //get temp the position of vertex #218 on this model
my.x=temp.x;
my.y=temp.y;
my.z=temp.z; //put it in its place. this will put the fish on the horn point at its origin. If it isn't quite in the right place, add or subtract from these values (e.g. my.x=temp.x+40//move fish 40 quants ahead of horn point) or change the origin in MED.

wait(1);
}
}


action destructatron
{

destructabot = my; //destructabot points to this entity

vec_for_vertex(temp, my, 218); //get temp the position of vertex #218 on this model
ent_create("fish.mdl", temp, fish); //create a fish on the horn and give it the fish action (which makes the fish move with the horn)

while(my!=null)

{
//move or do whatever here

}

}

 

 

Okay, this looks silly, but it works!

 

Attaching Models So They are in Sync

The female character in the illustration above is wearing a dress. The dress flaps around and has to stay perfectly against her body. She might even have a special occasion and want to change her dress. How could you do that? For something this complicated, simply gluing the dress to a vertex on her body is not going to work.

In this case the dress has to be a separate model that is animated at the same time as the character with the same point of origin. Then in c-script we keep the dress model at the same location as the character model.

Take a look at the models for this character. They were made as one model, animated, then split into two pieces:

 

Notice how both models have their point of origin (the point where the green lines meet) in the same place (which should ideally be somewhere around the pelvis -not the feet as shown here-, I'm just using this as an example).

Using this method you could model different clothes, hairstyles and even body parts.

Keeping them together in c-script is just a matter of giving both models the same location, rotation and animation frame. For example:

vec_set(my.x, you.x);

vec_set(my.pan,you.pan);

my.frame = you.frame;..... .

 

Argh! It Wobbles!

For a start, make sure the attached entity has its passable flag set:

my.passable=on;

The other thing it could be is that no two functions ever run at exactly the same time. One entity is being moved before the other. Try adding a proc_late command to one of the entities. That should do it.

 

Morphing to a New Entity

What now if our heroine is invited to a banquet for the rich and famous and needs to change into evening wear?

If you had made more than one dress, you could now use the ent_morph command to change that entity:

ent_,morph(my, "evening_dress.mdl");

I haven't made her another dress, so I can't show you that happen, however, you will notice that she is wearing a pistol strapped to her leg. The pistol is actually part of the model, not an attached entity. This is nice because it is perfectly in place, however it posses a challenge when she wants to use it.

In this case I made two character models. One with the gun strapped to her leg and the other with the gun in her hot little hand. When she needs to draw the gun, her hand goes to it, then, poof! She gets morphed into the version holding her gun before aiming and firing:

Model with gun in holster is morphed to: Model with gun in hand.

 

This is a little difficult to do well, but is very useful. Say, for example, you wanted a monster to pluck up a building and eat it. The building could start out as a map entity or a model, but when the monster grabs it, the entity is removed and the monster gets morphed into a model of the monster with the building in its hand. The animation then plays showing the monster put the house in its mouth and start chewing. All sorts of things become possible.

 

That is about it for now. Pretty soon, perhaps be the time you are reading this, it will also be possible to attach models to bones. More on that when it arrives (and when I have time to play with it myself).