Panda3D
actorNode.cxx
1 // Filename: actorNode.cxx
2 // Created by: charles (07Aug00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "actorNode.h"
16 #include "config_physics.h"
17 #include "physicsObject.h"
18 
19 #include "transformState.h"
20 
21 TypeHandle ActorNode::_type_handle;
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function : ActorNode
25 // Access : public
26 // Description : Constructor
27 ////////////////////////////////////////////////////////////////////
29 ActorNode(const string &name) :
30  PhysicalNode(name) {
31  _contact_vector = LVector3::zero();
32  add_physical(new Physical(1, true));
33  _mass_center = get_physical(0)->get_phys_body();
34  _mass_center->set_active(true);
35  #ifndef NDEBUG
36  _mass_center->set_name(name);
37  #endif
38  _ok_to_callback = true;
39  _transform_limit = 0.0;
40 }
41 
42 ////////////////////////////////////////////////////////////////////
43 // Function : ActorNode
44 // Access : public
45 // Description : Copy Constructor.
46 ////////////////////////////////////////////////////////////////////
48 ActorNode(const ActorNode &copy) :
49  PhysicalNode(copy) {
50  _contact_vector = LVector3::zero();
51  _ok_to_callback = true;
52  _mass_center = get_physical(0)->get_phys_body();
53  _transform_limit = copy._transform_limit;
54 }
55 
56 ////////////////////////////////////////////////////////////////////
57 // Function : ~ActorNode
58 // Access : public
59 // Description : destructor
60 ////////////////////////////////////////////////////////////////////
63 }
64 
65 ////////////////////////////////////////////////////////////////////
66 // Function : update_transform
67 // Access : public
68 // Description : this sets the transform generated by the contained
69 // Physical, moving the node and subsequent geometry.
70 // i.e. copy from PhysicsObject to PandaNode
71 ////////////////////////////////////////////////////////////////////
72 void ActorNode::
74  LMatrix4 lcs = _mass_center->get_lcs();
75 
76  // lock the callback so that this doesn't call transform_changed.
77  _ok_to_callback = false;
78  set_transform(TransformState::make_mat(lcs));
79  _ok_to_callback = true;
80 }
81 
82 ////////////////////////////////////////////////////////////////////
83 // Function : test_transform
84 // Access : private
85 // Description : this tests the transform to make sure it's within
86 // the specified limits. It's done so we can assert
87 // to see when an invalid transform is being applied.
88 ////////////////////////////////////////////////////////////////////
89 void ActorNode::
90 test_transform(const TransformState *ts) const {
91  LPoint3 pos(ts->get_pos());
92  nassertv(pos[0] < _transform_limit);
93  nassertv(pos[0] > -_transform_limit);
94  nassertv(pos[1] < _transform_limit);
95  nassertv(pos[1] > -_transform_limit);
96  nassertv(pos[2] < _transform_limit);
97  nassertv(pos[2] > -_transform_limit);
98 }
99 
100 ////////////////////////////////////////////////////////////////////
101 // Function : transform_changed
102 // Access : private, virtual
103 // Description : node hook. This function handles outside
104 // (non-physics) actions on the actor
105 // and updates the internal representation of the node.
106 // i.e. copy from PandaNode to PhysicsObject
107 ////////////////////////////////////////////////////////////////////
108 void ActorNode::
109 transform_changed() {
110  PandaNode::transform_changed();
111 
112  // this callback could be triggered by update_transform, BAD.
113  if (!_ok_to_callback) {
114  return;
115  }
116 
117  // get the transform
118  CPT(TransformState) transform = get_transform();
119 
120  if (_transform_limit > 0.0) {
121  test_transform(transform);
122  }
123 
124  // extract the orientation
125  if (_mass_center->get_oriented() == true) {
126  _mass_center->set_orientation(transform->get_quat());
127  }
128 
129  // apply
130  _mass_center->set_position(transform->get_pos());
131 }
132 
133 
134 ////////////////////////////////////////////////////////////////////
135 // Function : write
136 // Access : Public
137 // Description : Write a string representation of this instance to
138 // <out>.
139 ////////////////////////////////////////////////////////////////////
140 void ActorNode::
141 write(ostream &out, unsigned int indent) const {
142  #ifndef NDEBUG //[
143  out.width(indent); out<<""; out<<"ActorNode:\n";
144  out.width(indent+2); out<<""; out<<"_ok_to_callback "<<_ok_to_callback<<"\n";
145  out.width(indent+2); out<<""; out<<"_mass_center\n";
146  _mass_center->write(out, indent+4);
147  PhysicalNode::write(out, indent+2);
148  #endif //] NDEBUG
149 }
virtual void write(ostream &out, unsigned int indent=0) const
Write a string representation of this instance to <out>.
virtual void write(ostream &out, unsigned int indent=0) const
Write a string representation of this instance to <out>.
void set_position(const LPoint3 &pos)
Vector position assignment.
Definition: physicsObject.I:33
static const LVector3f & zero()
Returns a zero-length vector.
Definition: lvector3.h:270
void update_transform()
this sets the transform generated by the contained Physical, moving the node and subsequent geometry...
Definition: actorNode.cxx:73
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
bool get_oriented() const
See set_oriented().
Graph node that encapsulated a series of physical objects.
Definition: physicalNode.h:31
virtual ~ActorNode()
destructor
Definition: actorNode.cxx:62
void set_active(bool flag)
Process Flag assignment.
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:451
virtual void write(ostream &out, unsigned int indent=0) const
Write a string representation of this instance to <out>.
Definition: actorNode.cxx:141
Defines a set of physically modeled attributes.
Definition: physical.h:40
ActorNode(const string &name="")
Constructor.
Definition: actorNode.cxx:29
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
Like a physical node, but with a little more.
Definition: actorNode.h:30
virtual LMatrix4 get_lcs() const
returns a transform matrix to this object&#39;s local coordinate system.
void set_transform(const TransformState *transform, Thread *current_thread=Thread::get_current_thread())
Sets the transform that will be applied to this node and below.
Definition: pandaNode.cxx:1267