Back to Site
Basic Character Motion Tutorial
Friday, October 01 2004 @ 02:37 PM EDT
In order to properly understand the underlying code for this application please be sure you have read and understood the previous tutorials. The following
ragmatic tutorial will explain the instantiation of a Delta3D character, apply motion and explain the attachment of a tripod camera.
In this tutorial you will need to go to your delta3d installation folder, and grab the data/models/marine files to use. Let’s start by creating a header file in order
to define actions based on user input. For this example the class has been named "KeyController" and derives the Delta3D classes “Base” and
“KeyboardListener”.
class KeyController : public Base, public KeyboardListener
{
};
We now need to override the inherited virtual functions associated with the two parent classes. Create definitions for “OnMessage”, “KeyPressed”, and
“KeyReleased” and add two member variables representing a character and a corresponding keyboard.
class KeyController : public Base, public KeyboardListener
{
DECLARE_MANAGEMENT_LAYER(KeyController)
private:
Character *character;
Keyboard *keyboard;
public:
KeyController(Character *_character, Keyboard *_keyboard) : Base(“KeyController”),
character(_character), keyboard(_keyboard)
{
AddSender(System::Instance());
keyboard->AddKeyboardListener(this);
}
virtual void OnMessage(MessageData *data)
{
}
virtual void KeyPressed(Keyboard* keyboard, Producer::KeyboardKey key,
Producer::KeyCharacter character)
{
}
virtual void KeyReleased(Keyboard *keyboard, Producer::KeyboardKey key,
Producer::KeyCharacter character)
{
}
};
IMPLEMENT_MANAGEMENT_LAYER(KeyController)
ow we have defined our class and made sure that the constructor handles message passing to the keyboard. When the user interacts with the keyboard a
message is sent and the function OnMessage is called implicitly. The following will set the function required to rotate and add velocity to our member
variable “character”.
virtual void OnMessage(MessageData *data)
{
if(data->message == "preframe")
{
double delta = *(double*)data->userData;
float rotation = character->GetRotation(),
velocity = 0.0f;
if(keyboard->GetKeyState(Producer::Key_Left))
{
rotation += (float)(delta*90.0);
}
if(keyboard->GetKeyState(Producer::Key_Right))
{
rotation -= (float)(delta*90.0);
}
if(keyboard->GetKeyState(Producer::Key_Up))
{
if(keyboard->GetKeyState(Producer::Key_Shift_R) ||
keyboard->GetKeyState(Producer::Key_Shift_L))
{
评论1