Show kplayer.cpp syntax highlighted
/*
Copyright (C) 2003, 2004, 2005 by Luca Cappa
Written by Luca Cappa groton@users.sourceforge.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
//
//Define this symbol below to use a cal3d mesh object.
//#define __USE_CAL3D__
//
// I N C L U D E S
//
#include "cssysdef.h"
#include "csutil/sysfunc.h"
#include "csutil/cscolor.h"
#include "csutil/cmdhelp.h"
#include "csutil/cspmeter.h"
#include "csutil/csstring.h"
#include "csutil/scfstr.h"
#include "csutil/dirtyaccessarray.h"
#include "csutil/xmltiny.h"
#include "csutil/array.h"
#include "cstool/csview.h"
#include "cstool/initapp.h"
#include "cstool/collider.h"
#include "cstool/cspixmap.h"
#include "iutil/vfs.h"
#include "iutil/eventq.h"
#include "iutil/event.h"
#include "iutil/objreg.h"
#include "iutil/csinput.h"
#include "iutil/virtclk.h"
#include "iutil/plugin.h"
#include "iutil/string.h"
#include "iengine/sector.h"
#include "iengine/engine.h"
#include "iengine/camera.h"
#include "iengine/light.h"
#include "iengine/texture.h"
#include "iengine/mesh.h"
#include "iengine/movable.h"
#include "iengine/material.h"
#include "imesh/thing.h"
#include "imesh/object.h"
#include "imesh/sprite3d.h"//ADDED
#include "imesh/spritecal3d.h"//ADDED
#include "ivideo/graph3d.h"
#include "ivideo/graph2d.h"
#include "ivideo/natwin.h"
#include "ivideo/txtmgr.h"
#include "ivideo/texture.h"
#include "ivideo/material.h"
#include "ivideo/fontserv.h"
#include "igraphic/imageio.h"
#include "ivaria/reporter.h"
#include "ivaria/stdrep.h"
#include "ivaria/conout.h"
#include "ivaria/reporter.h"
#include "ivaria/stdrep.h"
#include "ivaria/collider.h"
#include "csgeom/quaterni.h"
#include "csgeom/transfrm.h"
#include "csgeom/math3d_d.h"
#include "csgeom/math3d.h"
#include "igeom/polymesh.h"
#include "igeom/objmodel.h"
#include "imap/loader.h"
#include "iaws/aws.h"
#include "iaws/awscnvs.h"
#include "isense/iisensetracker.h"//??
#include "korientation.h"
#include "kcamera.h"
#include "kblock.h"
#include "kblockid.h"//??
#include "kmap.h"
#include "kblockid.h"
#include "kflags.h"
#include "ksprite3d.h"
#include "ksprite3dlist.h"//??
#include "kappstate.h"
#include "ske.h"
#include "klandmark.h"
#include "klandmarklist.h"
#include "kappstate.h"//??
#include "skybox.h"//??
#include "kobject3d.h"//??
#include "kcommandprocessor.h"
#include "kutil.h"
#include "kpath.h"
#include "kplayer.h"
#include "kmission.h"//??
#include "kxmlhelper.h"
#include "ksign.h"//??
#include "kdragmanager.h"
#include "kwireframe.h"
#include "kkeystate.h"
//Include file for this file!
#include "kplayer.h"
SCF_IMPLEMENT_IBASE_EXT (KPlayer)
SCF_IMPLEMENTS_INTERFACE (KPlayer)
SCF_IMPLEMENTS_EMBEDDED_INTERFACE (iKDragHandler)
SCF_IMPLEMENTS_EMBEDDED_INTERFACE (iKDraggable)
SCF_IMPLEMENT_IBASE_EXT_END
SCF_IMPLEMENT_EMBEDDED_IBASE (KPlayer::eiKDragHandler)
SCF_IMPLEMENTS_INTERFACE (iKDragHandler)
SCF_IMPLEMENT_EMBEDDED_IBASE_END
SCF_IMPLEMENT_EMBEDDED_IBASE (KPlayer::eiKDraggable)
SCF_IMPLEMENTS_INTERFACE (iKDraggable)
SCF_IMPLEMENT_EMBEDDED_IBASE_END
//???
const float KPlayer::GROUND_Y = 0.05f;
const float KPlayer::BODY_HEIGHT = 1.3f;
const float KPlayer::LEGS_HEIGHT = 0.5f;
KPlayer::KPlayer ()
{
//Here it is not needed the SCF_CONSTRUCT_IBASE (0) cause it is already
//called by the derived ctor of csObject. We are using
//SCF_DECLARE_IBASE_EXT (csObject); in the header file indeed.
SCF_CONSTRUCT_EMBEDDED_IBASE (scfiKDragHandler);
SCF_CONSTRUCT_EMBEDDED_IBASE (scfiKDraggable);
m_walking = false;
m_showArrow = false;//?? Force this to false (temporary changes for desktop VR mode).
m_showHead = true;
m_mov3d.SetVelocity (csVector3 (0, 0, 0));
m_mov3d.SetMaxVelocity (1.7f);
m_mov3d.SetPosition (csVector3 (0.0f, GROUND_Y, 0.0f));
m_playerAcc = 0.0f; //Actual meter/(second*second)
m_height = 1.75f;//Height of the eyes.
m_oldPos = csVector3 (0.0f, 0.0f, 0.0f);
m_friction = 2.0f;
m_accK = 3.5f;
m_FOVAngle = 74.0f;
m_FOVWidth = 640.f;
m_bodyQ.SetWithEuler (csVector3 (0,0,0));
m_headQ.SetWithEuler (csVector3 (0,0,0));
m_sector = 0;
m_headOffset = csVector3 (0.0f, -0.27f, 0.03f);
m_topOffset = csVector3 (-0.09f, 0.0f, 0.11f);
m_bottomOffset = csVector3 (-0.02f, 0.13f, 0.25f);
}
KPlayer::~KPlayer ()
{
SCF_DESTRUCT_EMBEDDED_IBASE (scfiKDragHandler);
SCF_DESTRUCT_EMBEDDED_IBASE (scfiKDraggable);
}
bool KPlayer::Initialize ()
{
LoadPlayerMeshes ();
Hiding (false);
//
//Create the collider using a cube.
//This mesh below is just used to get its iPolygonMesh interface,
//thus creating its colliderwrapper.
#ifndef __USE_CAL3D__
m_bodyCollMesh.AttachNew (new KSprite3D ());
if (!m_bodyCollMesh->Create ("body_coll", "/SKE/model/SPRbody",
"black", .35f, BODY_HEIGHT, .35f))
SKE::Report (CS_REPORTER_SEVERITY_ERROR, "Failed to load the\
body collider mesh");
m_bodyCollMesh->SetSector (g_ske->m_world);//??
m_bodyCollMesh->UpdateMove ();
csVector3 l_crap;
m_bodyCollMesh->GetMeshWrapper ()->GetRadius (m_bodyRadius, l_crap);
iMeshFactoryWrapper& l_mFW =
m_bodyCollMesh->GetMeshFactoryWrapper ();
l_mFW.HardTransform (csReversibleTransform (csMatrix3 (),
csVector3 (0, LEGS_HEIGHT + GROUND_Y, 0)));
m_feetsCollMesh.AttachNew (new KSprite3D ());
if (!m_feetsCollMesh->Create ("feets_coll", "/SKE/model/SPRbody",
"black", .2f, LEGS_HEIGHT, .2f))
SKE::Report (CS_REPORTER_SEVERITY_ERROR, "Failed to load the\
feets collider mesh");
m_feetsCollMesh->SetSector (g_ske->m_world);//??
m_feetsCollMesh->UpdateMove ();
m_feetsCollMesh->GetMeshWrapper ()->GetRadius (m_feetsRadius, l_crap);
iMeshFactoryWrapper& l_mFW2 =
m_feetsCollMesh->GetMeshFactoryWrapper ();
l_mFW2.HardTransform (csReversibleTransform (csMatrix3 (),
csVector3 (0, GROUND_Y, 0)));
csRef<iPolygonMesh> l_polMesh =
m_bodyCollMesh->GetMeshWrapper ()->GetMeshObject ()->GetObjectModel ()->
GetPolygonMeshColldet ();
m_bodyCollider = new csColliderWrapper (m_bodyCollMesh->GetMeshWrapper ()->
QueryObject (), g_ske->m_collisionDet, l_polMesh);//??
m_bodyCollider->SetName ("body collider");//??
m_bodyCollTracker.AttachNew (new KWireframe ("m_bodyCollTracker"));
m_bodyCollTracker->SetTracked (m_bodyCollMesh->GetMeshWrapper (), false);
l_polMesh = m_feetsCollMesh->GetMeshWrapper ()->GetMeshObject ()->
GetObjectModel ()->GetPolygonMeshColldet ();
m_feetsCollider = new csColliderWrapper (m_feetsCollMesh->
GetMeshWrapper ()->QueryObject (), g_ske->m_collisionDet, l_polMesh);//??
m_feetsCollider->SetName ("feets collider");//??
m_feetsCollTracker.AttachNew (new KWireframe ("m_feetsCollTracker"));
m_feetsCollTracker->SetTracked (m_feetsCollMesh->GetMeshWrapper (), false);
#else // __USE_CAL3D__
//????????
m_playerCollMesh.AttachNew (new KSprite3D ());
m_playerCollMesh->Create ("body_coll", "/SKE/model/SPRbody",
"black", .35f, BODY_HEIGHT, .35f);
m_playerCollMesh->SetSector (g_ske->m_world);//??
m_playerCollMesh->UpdateMove ();
csRef<iPolygonMesh> l_polMesh = m_playerCollMesh->GetMeshWrapper ()->
GetMeshObject ()->GetObjectModel ()->GetPolygonMeshColldet ();
m_collider = new csColliderWrapper (m_meshW->QueryObject (),
g_ske->m_collisionDet, l_polMesh);
m_collider->SetName ("player collider");
m_playerCollTracker.AttachNew (new KWireframe());
m_playerCollTracker->SetTracked (m_meshW, true);
#endif // __USE_CAL3D__
return true;
}
void KPlayer::LoadPlayerMeshes ()
{
csRef<iKDraggable> l_d;
csRef<iKDragHandler> l_kDH;
#ifndef __USE_CAL3D__
csReversibleTransform l_revTr;
csZRotMatrix3 l_mZ(DEG2RAD(90));
csXRotMatrix3 l_mX(DEG2RAD(90));
l_revTr.RotateThis (l_mZ);
l_revTr.RotateThis (l_mX);
//
//Load head.
m_head.AttachNew (new KSprite3D ());
if (!m_head->Create ("S3D_head", "/SKE/model/PLRhead",
"ripleyhead.mat", 0.2f, 0.3f, 0.2f, &l_revTr.GetInverse ()))
SKE::Report (CS_REPORTER_SEVERITY_ERROR, "Failed to load the\
head mesh");
//
//Set the drag-handler for the head.
m_head->SetDraggable (&scfiKDraggable);
l_kDH = SCF_QUERY_INTERFACE (this, iKDragHandler);
scfiKDraggable.SetDragHandler (l_kDH);
//
//Load top.
csReversibleTransform l_tr2 (l_revTr);
l_tr2.RotateThis (csXRotMatrix3 (DEG2RAD(20)));//??a simple hack for repositioning it
m_top.AttachNew (new KSprite3D ());
if (!m_top->Create ("S3D_upper", "/SKE/model/PLRupper",
"ripley.mat", 0.5f, 0.8f,0.4f, &l_tr2.GetInverse ()))
SKE::Report (CS_REPORTER_SEVERITY_ERROR, "Failed to load the\
top mesh");
//
//Set the drag-handler for the top.
m_top->SetDraggable (&scfiKDraggable);
l_kDH = SCF_QUERY_INTERFACE (this, iKDragHandler);
scfiKDraggable.SetDragHandler (l_kDH);
m_top->SetAction ("TORSO_STAND2");
//Load bottom.
m_bottom.AttachNew (new KSprite3D ());
if (!m_bottom->Create ("S3D_lower", "/SKE/model/PLRlower",
"ripley.mat", 0.3f, 0.7f, 0.7f, &l_revTr.GetInverse ()))
SKE::Report (CS_REPORTER_SEVERITY_ERROR, "Failed to load the\
bottom mesh");
//
//Set the drag-handler for the bottom.
m_bottom->SetDraggable (&scfiKDraggable);
l_kDH = SCF_QUERY_INTERFACE (this, iKDragHandler);
scfiKDraggable.SetDragHandler (l_kDH);
m_bottom->SetAction ("LEGS_IDLE");
#else // __USE_CAL3D__
//?????
g_ske->MountDirectory ("/balazar", "$.$/data$/SKE$/model$/balazar$/");
csRef<iMeshFactoryWrapper> l_mFW (g_ske->m_loader->LoadMeshObjectFactory
("/balazar/balazar.xml")); //??
//
//??????? SCALING TEST!!!!
csMatrix3 l_m;
l_m.Identity ();
/*l_m *= 2.0f;
l_mFW->HardTransform (csReversibleTransform (l_m, csVector3 (0,0,0)));*/
// Create the character and add it to the engine.
csRef<iMeshWrapper> l_balazar (g_ske->m_engine->CreateMeshWrapper
(l_mFW, "Balazar", g_ske->m_hiddenWorld, csVector3 (-3, 0, 3)));//??
l_balazar->GetMovable ()->SetTransform (l_m);
l_balazar->GetMovable ()->SetPosition (csVector3 (0,0,0));
l_balazar->GetMovable ()->UpdateMove ();
csRef<iSpriteCal3DState> spcal3dstate (
SCF_QUERY_INTERFACE (l_balazar->GetMeshObject (), iSpriteCal3DState));
spcal3dstate->SetAnimCycle("walk", 1);
csRef<iSpriteCal3DFactoryState> l_factory = SCF_QUERY_INTERFACE
(l_balazar->GetFactory ()->GetMeshObjectFactory (), iSpriteCal3DFactoryState);
l_factory->RescaleFactory (0.5f);
l_balazar->SetZBufMode (CS_ZBUF_USE);
l_balazar->SetRenderPriority (g_ske->m_engine->GetObjectRenderPriority ());
m_meshW = l_balazar;
/* {
csMatrix3 l_m;
l_m.Identity ();
l_m *= 2.0f;
l_mFW->HardTransform (csReversibleTransform (l_m, csVector3 (0,0,0)));
// Create the character and add it to the engine.
csRef<iMeshWrapper> l_balazar (g_ske->m_engine->CreateMeshWrapper (
l_mFW, "Balazar2", g_ske->m_world, csVector3 (-6, 0, 6)));//??
l_balazar->GetMovable ()->SetTransform (l_m);
l_balazar->GetMovable ()->SetPosition (csVector3 (6,0,6));
l_balazar->GetMovable ()->UpdateMove ();
csRef<iSpriteCal3DState> spcal3dstate (
SCF_QUERY_INTERFACE (l_balazar->GetMeshObject (), iSpriteCal3DState));
spcal3dstate->SetAnimCycle("walk", 1);
spcal3dstate->SetScalingFactor (0.5f);
l_balazar->SetZBufMode (CS_ZBUF_USE);
l_balazar->SetRenderPriority (g_ske->m_engine->GetObjectRenderPriority ());
}*/
#endif // __USE_CAL3D__
//
// Create s_arrow.
csReversibleTransform l_revTr2;
csXRotMatrix3 l_mX2 (DEG2RAD(90));
l_revTr2.RotateThis (l_mX2);
m_arrow.AttachNew (new KSprite3D ());
m_arrow->Create ("S3D_arrow", "/SKE/model/SPRarrow",
"green", 0.2f, 0.05f, 0.45f,
&l_revTr2);
//
//Set the handler for the bottom.
l_d = SCF_QUERY_INTERFACE (m_arrow, iKDraggable);
l_kDH = SCF_QUERY_INTERFACE (this, iKDragHandler);
l_d->SetDragHandler (l_kDH);
//
//Set the transparency of the arrow.
csRef<iSprite3DState> l_sS =
SCF_QUERY_INTERFACE (m_arrow->GetMeshWrapper ()->
GetMeshObject (), iSprite3DState);
l_sS->SetMixMode (CS_FX_SETALPHA (0.8f));
}
void KPlayer::ShowArrow (bool p_b)
{
//??????m_showArrow = p_b;
}
void KPlayer::ShowHead (bool p_b)
{
m_showHead = p_b;
}
void KPlayer::SetWalking (bool p_walking)
{
m_walking = p_walking;
}
void KPlayer::SetPosition (csVector3 p_pos)
{
p_pos.y = GROUND_Y;//??????HACK!
m_mov3d.SetPosition (p_pos);
}
csVector3 KPlayer::GetPosition ()
{
return m_mov3d.GetPosition ();
}
void KPlayer::Hiding (bool p_hiding)
{
m_sector = p_hiding ? g_ske->m_hiddenWorld : g_ske->m_world;//??
}
void KPlayer::SetBodyAngle (KQuaternion const& p_q)
{
m_bodyQ = p_q;
}
KQuaternion const& KPlayer::GetBodyAngle ()
{
return m_bodyQ;
}
void KPlayer::SetHeadAngles (KQuaternion const& p_hQ)
{
m_headQ = p_hQ;
}
KQuaternion const& KPlayer::GetHeadAngles ()
{
return m_headQ;
}
void KPlayer::SetHeight (float p_height)
{
m_height = p_height;
}
float KPlayer::GetHeight ()
{
return m_height;
}
void KPlayer::SetupFrame ()
{
//
//Normalize the quats.
m_headQ.Normalize ();
m_bodyQ.Normalize ();
static iSector* s_lastSector = 0;
if (!s_lastSector || s_lastSector != m_sector)
{
s_lastSector = m_sector;
#ifndef __USE_CAL3D__
m_top->SetSector (m_sector);
m_top->UpdateMove ();
m_bottom->SetSector (m_sector);
m_bottom->UpdateMove ();
m_head->SetSector (m_sector);
m_head->UpdateMove ();
#else // __USE_CAL3D__
m_meshW->GetMovable ()->SetSector (m_sector);
m_meshW->GetMovable ()->UpdateMove ();
#endif // __USE_CAL3D__
m_arrow->SetSector (m_sector);
m_arrow->UpdateMove ();
}//if
static bool s_lastValueOfWalking = false;
if (m_walking && s_lastValueOfWalking != m_walking)
{
s_lastValueOfWalking = m_walking;
#ifndef __USE_CAL3D__
m_bottom->SetAction ("LEGS_WALK");
m_bottom->GetState ().SetSingleStepAction (false);
#endif // __USE_CAL3D__
}//if
else if (s_lastValueOfWalking != m_walking)
{
s_lastValueOfWalking = m_walking;
#ifndef __USE_CAL3D__
m_bottom->SetAction ("LEGS_IDLE");
#endif // __USE_CAL3D__
}//else
csVector3 l_eyeCurrPos (m_mov3d.GetPosition ());
l_eyeCurrPos.y += m_height;//Add the height of the eyes.
//
//Update the position of the arrow.
csVector3 l_upArrow (0,1,0);
l_upArrow = m_bodyQ.Rotate (l_upArrow);
csVector3 l_arrowNormal (0,0,1);
l_arrowNormal = m_bodyQ.Rotate (l_arrowNormal);
csVector3 l_down = -l_upArrow;//?? remove this un-optimization.
csVector3 l_sprPos (l_eyeCurrPos + l_down * 0.27f + l_arrowNormal * 0.37f);
csReversibleTransform l_revTr;
l_revTr.LookAt (l_arrowNormal, l_upArrow);
m_arrow->GetMovable ().SetTransform (l_revTr.GetT2O ());
m_arrow->GetMovable ().SetPosition (l_sprPos);
m_arrow->GetMovable ().UpdateMove ();
//
//Update the position of head, top, bottom part of the player's body.
#ifndef __USE_CAL3D__
csVector3 l_headOffsetRotated;
l_headOffsetRotated = m_bodyQ.Rotate (m_headOffset);
csVector3 l_headNormal (0,0,1);
l_headNormal = (m_bodyQ * m_headQ).Rotate (l_headNormal);
csVector3 l_upHead (0,1,0);
l_upHead = m_headQ.Rotate (l_upHead);
l_upHead = m_bodyQ.Rotate (l_upHead);
/*//???static csVector3 l_oldHN;
static csVector3 l_oldUN;
if ((l_oldHN != l_headNormal ) || (l_oldUN != l_upHead))
{
l_oldHN = l_headNormal;
printf ("FOR: %7.3f,%7.3f,%7.3f", l_headNormal.x, l_headNormal.y,
l_headNormal.z);
l_oldUN = l_upHead;
printf (" UP: %7.3f,%7.3f,%7.3f\n", l_upHead.x, l_upHead.y,
l_upHead.z);
}//if*/
l_revTr.LookAt (l_headNormal, l_upHead);
l_eyeCurrPos.y -= 0.1f;
m_head->SetTransform (l_revTr.GetT2O ());
m_head->SetPosition (l_eyeCurrPos + l_headOffsetRotated);
m_head->UpdateMove ();
csVector3 l_upBody (0,1,0);
csVector3 l_bodyNormal (0,0,1);
l_bodyNormal = m_bodyQ.Rotate (l_bodyNormal);
l_revTr.LookAt (l_bodyNormal, l_upBody);
csVector3 l_topOffsetRotated = m_bodyQ.Rotate (m_topOffset);
l_eyeCurrPos.y -= 0.7f;
m_top->SetTransform (csMatrix3 (m_bodyQ.GetMatrix ()));
m_top->SetPosition (l_eyeCurrPos + l_topOffsetRotated);
m_top->UpdateMove ();
csVector3 l_bottomOffsetRotated = m_bodyQ.Rotate (m_bottomOffset);
l_eyeCurrPos.y -= 0.9f;
m_bottom->SetTransform (csMatrix3 (m_bodyQ.GetMatrix ()));
m_bottom->SetPosition (l_eyeCurrPos + l_bottomOffsetRotated);
m_bottom->UpdateMove ();
//show the head?
if (m_showHead)
{
m_head->SetSector (g_ske->m_world);//??
m_head->UpdateMove ();
}//if
else
{
m_head->SetSector (g_ske->m_hiddenWorld);//??
m_head->UpdateMove ();
}//else
#else
m_meshW->GetMovable ()->SetTransform (csMatrix3 (m_bodyQ.GetMatrix ()));
m_meshW->GetMovable ()->SetPosition (l_eyeCurrPos);
m_meshW->GetMovable ()->UpdateMove ();
#endif // __USE_CAL3D__
//show the arrow?
if (m_showArrow)
{
m_arrow->GetMovable ().SetSector (g_ske->m_world);//??
m_arrow->GetMovable ().UpdateMove ();
m_arrow->SetZBufMode (CS_ZBUF_FILL);//just fill the zbuffer.
m_arrow->SetRenderPriority
(g_ske->m_engine->GetRenderPriority ("alpha"));//set priority to alpha//??
}//if
else
{
m_arrow->GetMovable ().SetSector (g_ske->m_hiddenWorld);//??
m_arrow->GetMovable ().UpdateMove ();
}//else
//
//Place also the the collider's meshes
//
if (g_ske->mDebugMode)//???
{
csVector3 l_currPos = m_mov3d.GetPosition ();
#ifndef __USE_CAL3D__
m_feetsCollMesh->SetPosition (l_currPos);
m_feetsCollMesh->UpdateMove ();
m_feetsCollTracker->SetVisible (true);
m_bodyCollMesh->SetPosition (l_currPos);
m_bodyCollMesh->UpdateMove ();
m_bodyCollTracker->SetVisible (true);
#else // __USE_CAL3D__
/*m_meshW->GetMovable ()->SetPosition (l_currPos);
m_meshW->GetMovable ()->UpdateMove ();
m_playerCollTracker->SetInvisible (false);*/
#endif // __USE_CAL3D__
}//if
else
{
#ifndef __USE_CAL3D__
m_feetsCollTracker->SetVisible (false);
m_bodyCollTracker->SetVisible (false);
#else // __USE_CAL3D__
m_playerCollTracker->SetVisible (false);
#endif // __USE_CAL3D__
}//else if
}
csColliderWrapper* KPlayer::GetBodyColliderWrapper ()
{
#ifndef __USE_CAL3D__
return m_bodyCollider;
#else //
return m_collider;
#endif // __USE_CAL3D__
}
csColliderWrapper* KPlayer::GetFeetsColliderWrapper ()
{
#ifndef __USE_CAL3D__
return m_feetsCollider;
#else //
return m_collider;
#endif // __USE_CAL3D__
}
//
//Calculate both the linear and angular acceleration (//??Although
//the angular acceleration there is no yet!!!!!)
void KPlayer::CalculatePlayerAcceleration (bool p_transBody, bool p_rotBody,
float speedRadians)
{
//
//Player acceleration zeroed.
m_playerAcc = 0;
csVector3 l_acc;
if (g_ske->m_useJoy)//??
{
//handle camera translation movements.
if (g_ske->m_joyDriver->GetLastY(0) > 0 && p_transBody)
{
csVector3 l_v (0,0,1);
l_v = m_bodyQ.Rotate (l_v);
l_v.Normalize ();
m_playerAcc.x = l_v.x * m_accK;
m_playerAcc.z = l_v.z * m_accK;
}//ifs
if (g_ske->m_joyDriver->GetLastY(0) < 0 && p_transBody)
{
csVector3 l_v (0,0,1);
l_v = m_bodyQ.Rotate (l_v);
l_v.Normalize ();
m_playerAcc.x = -l_v.x * m_accK;
m_playerAcc.z = -l_v.z * m_accK;
}//if
//body left right rotation.
if (g_ske->m_joyDriver->GetLastX(0) > 0 && p_rotBody)
{
KQuaternion l_q;
l_q.SetWithEuler (csVector3 (speedRadians, 0, 0));
l_q.Normalize ();
m_bodyQ *= l_q;
}//if
if (g_ske->m_joyDriver->GetLastX (0) < 0 && p_rotBody)
{
KQuaternion l_q;
l_q.SetWithEuler (csVector3 (-speedRadians, 0, 0));
l_q.Normalize ();
m_bodyQ *= l_q;
}//if
}//if
else
{
//handle camera translation movements.
if (Key_Forward && p_transBody)
{
csVector3 l_v (0,0,1);
l_v = m_bodyQ.Rotate (l_v);
l_v.Normalize ();
m_playerAcc.x = l_v.x * m_accK;
m_playerAcc.z = l_v.z * m_accK;
}//ifs
if (Key_Back && p_transBody)
{
csVector3 l_v (0,0,1);
l_v = m_bodyQ.Rotate (l_v);
l_v.Normalize ();
m_playerAcc.x = -l_v.x * m_accK;
m_playerAcc.z = -l_v.z * m_accK;
}//if
//body left right rotation.
if (Key_Left && p_rotBody)
{
KQuaternion l_q;
l_q.SetWithEuler (csVector3 (-speedRadians, 0, 0));
l_q.Normalize ();
m_bodyQ *= l_q;
}//if
if (Key_Right && p_rotBody)
{
KQuaternion l_q;
l_q.SetWithEuler (csVector3 (speedRadians, 0, 0));
l_q.Normalize ();
m_bodyQ *= l_q;
}//if
}//else
}
csVector3 KPlayer::GetNextWalkingStep (float p_seconds)
{
return m_mov3d.GetMovement (p_seconds, m_playerAcc,
m_friction, true);
}
void KPlayer::Walk (float p_seconds)
{
csVector3 l_oldPos = m_mov3d.GetPosition ();
//calculate the translating vector.
csVector3 l_movement = m_mov3d.GetMovement (p_seconds, m_playerAcc,
m_friction);
//
//This will deny a movement for more than 0.1 meters each frame.
/* if(l_movement.Norm () > 0.1f)
{
l_movement.Normalize ();
l_movement *= 0.1f;
}*/
//
//check for collision.
g_ske->DoGravity (l_oldPos, l_movement, csMatrix3());//???
//????????????????????TO DO: ADD COLLISION WITH THE GROUND.
l_movement.y = 0.0f;//???UGLY!
m_walking = l_movement.Norm () > EPSILON;
//
//speed feedback
/*if (p_seconds > 0.0f)
m_mov3d.SetVelocity (l_movement / p_seconds);*/
csVector3 l_pos (l_oldPos + l_movement);
m_mov3d.SetPosition (l_pos);//do the movement.
m_oldPos = m_mov3d.GetPosition ();//save the current position for later.
//
//Test for : if velocity > 0 then "walking".
bool l_walking = false;
csVector3 l_vel = m_mov3d.GetVelocity ();
l_walking = l_vel.Norm () > SMALL_EPSILON;
}
bool KPlayer::Drag (float p_x, float p_y, const csVector3& p_v, DRAG_TYPE p_dT)
{
m_dragType = p_dT;
return true;
}
bool KPlayer::HandlerDrag (float p_x, float p_y)
{
switch (m_dragType)
{
case TRANSLATION:
{
csVector3 lIntersection;
float lDist;
g_ske->CalculateIntersection (p_x, p_y,
csPlane3 (0, 1, 0), lIntersection, lDist);
lIntersection.y = 0.0f;
g_ske->m_initPos.SetPosition (lIntersection);//??
return true;
}//case
case ROTATION:
{
static float l_oldMouseYS3D = 0;
if (l_oldMouseYS3D == 0)
l_oldMouseYS3D = p_y;//only called the first time we get in here.
float l_angle = (K_2PI / 480.0f) * (p_y - l_oldMouseYS3D);
l_oldMouseYS3D = p_y;//store old value.
//
//??
KQuaternion l_q;
l_q.SetWithEuler (csVector3 (l_angle, 0, 0));
g_ske->m_initPos.SetBodyAngle (g_ske->m_initPos.GetBodyAngle () * l_q);//??
return true;
}//case
}//switch
return false;
}
const csRefArray<iMeshWrapper> KPlayer::GetMeshWrappers () const
{
csRefArray<iMeshWrapper> l_mWL;
#ifndef __USE_CAL3D__
l_mWL.Push (m_head->GetMeshWrapper ());
l_mWL.Push (m_top->GetMeshWrapper ());
l_mWL.Push (m_bottom->GetMeshWrapper ());
#else // __USE_CAL3D__
l_mWL.Push (m_meshW);
#endif // __USE_CAL3D__
return l_mWL;
}
See more files for this project here