Panda3D
|
00001 // Filename: actorNode.cxx 00002 // Created by: charles (07Aug00) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "actorNode.h" 00016 #include "config_physics.h" 00017 #include "physicsObject.h" 00018 00019 #include "transformState.h" 00020 00021 TypeHandle ActorNode::_type_handle; 00022 00023 //////////////////////////////////////////////////////////////////// 00024 // Function : ActorNode 00025 // Access : public 00026 // Description : Constructor 00027 //////////////////////////////////////////////////////////////////// 00028 ActorNode:: 00029 ActorNode(const string &name) : 00030 PhysicalNode(name) { 00031 _contact_vector = LVector3::zero(); 00032 add_physical(new Physical(1, true)); 00033 _mass_center = get_physical(0)->get_phys_body(); 00034 _mass_center->set_active(true); 00035 #ifndef NDEBUG 00036 _mass_center->set_name(name); 00037 #endif 00038 _ok_to_callback = true; 00039 _transform_limit = 0.0; 00040 } 00041 00042 //////////////////////////////////////////////////////////////////// 00043 // Function : ActorNode 00044 // Access : public 00045 // Description : Copy Constructor. 00046 //////////////////////////////////////////////////////////////////// 00047 ActorNode:: 00048 ActorNode(const ActorNode ©) : 00049 PhysicalNode(copy) { 00050 _contact_vector = LVector3::zero(); 00051 _ok_to_callback = true; 00052 _mass_center = get_physical(0)->get_phys_body(); 00053 _transform_limit = copy._transform_limit; 00054 } 00055 00056 //////////////////////////////////////////////////////////////////// 00057 // Function : ~ActorNode 00058 // Access : public 00059 // Description : destructor 00060 //////////////////////////////////////////////////////////////////// 00061 ActorNode:: 00062 ~ActorNode() { 00063 } 00064 00065 //////////////////////////////////////////////////////////////////// 00066 // Function : update_transform 00067 // Access : public 00068 // Description : this sets the transform generated by the contained 00069 // Physical, moving the node and subsequent geometry. 00070 // i.e. copy from PhysicsObject to PandaNode 00071 //////////////////////////////////////////////////////////////////// 00072 void ActorNode:: 00073 update_transform() { 00074 LMatrix4 lcs = _mass_center->get_lcs(); 00075 00076 // lock the callback so that this doesn't call transform_changed. 00077 _ok_to_callback = false; 00078 set_transform(TransformState::make_mat(lcs)); 00079 _ok_to_callback = true; 00080 } 00081 00082 //////////////////////////////////////////////////////////////////// 00083 // Function : test_transform 00084 // Access : private 00085 // Description : this tests the transform to make sure it's within 00086 // the specified limits. It's done so we can assert 00087 // to see when an invalid transform is being applied. 00088 //////////////////////////////////////////////////////////////////// 00089 void ActorNode:: 00090 test_transform(const TransformState *ts) const { 00091 LPoint3 pos(ts->get_pos()); 00092 nassertv(pos[0] < _transform_limit); 00093 nassertv(pos[0] > -_transform_limit); 00094 nassertv(pos[1] < _transform_limit); 00095 nassertv(pos[1] > -_transform_limit); 00096 nassertv(pos[2] < _transform_limit); 00097 nassertv(pos[2] > -_transform_limit); 00098 } 00099 00100 //////////////////////////////////////////////////////////////////// 00101 // Function : transform_changed 00102 // Access : private, virtual 00103 // Description : node hook. This function handles outside 00104 // (non-physics) actions on the actor 00105 // and updates the internal representation of the node. 00106 // i.e. copy from PandaNode to PhysicsObject 00107 //////////////////////////////////////////////////////////////////// 00108 void ActorNode:: 00109 transform_changed() { 00110 PandaNode::transform_changed(); 00111 00112 // this callback could be triggered by update_transform, BAD. 00113 if (!_ok_to_callback) { 00114 return; 00115 } 00116 00117 // get the transform 00118 CPT(TransformState) transform = get_transform(); 00119 00120 if (_transform_limit > 0.0) { 00121 test_transform(transform); 00122 } 00123 00124 // extract the orientation 00125 if (_mass_center->get_oriented() == true) { 00126 _mass_center->set_orientation(transform->get_quat()); 00127 } 00128 00129 // apply 00130 _mass_center->set_position(transform->get_pos()); 00131 } 00132 00133 00134 //////////////////////////////////////////////////////////////////// 00135 // Function : write 00136 // Access : Public 00137 // Description : Write a string representation of this instance to 00138 // <out>. 00139 //////////////////////////////////////////////////////////////////// 00140 void ActorNode:: 00141 write(ostream &out, unsigned int indent) const { 00142 #ifndef NDEBUG //[ 00143 out.width(indent); out<<""; out<<"ActorNode:\n"; 00144 out.width(indent+2); out<<""; out<<"_ok_to_callback "<<_ok_to_callback<<"\n"; 00145 out.width(indent+2); out<<""; out<<"_mass_center\n"; 00146 _mass_center->write(out, indent+4); 00147 PhysicalNode::write(out, indent+2); 00148 #endif //] NDEBUG 00149 }