Panda3D
Loading...
Searching...
No Matches
physicsObject.cxx
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file physicsObject.cxx
10 * @author charles
11 * @date 2000-06-13
12 */
13
14#include "physicsObject.h"
15
16ConfigVariableDouble PhysicsObject::_default_terminal_velocity
17("default_terminal_velocity", 400.0f);
18
19TypeHandle PhysicsObject::_type_handle;
20
21/**
22 * Default Constructor
23 */
26 _terminal_velocity(_default_terminal_velocity),
27 _mass(1.0f),
28 _process_me(false),
29 _oriented(true)
30{
31 _position.set(0.0f, 0.0f, 0.0f);
32 _last_position = _position;
33 _velocity.set(0.0f, 0.0f, 0.0f);
34 _orientation.set(1.0 ,0.0f, 0.0f, 0.0f);
35 _rotation = LRotation::ident_quat();
36}
37
38/**
39 * copy constructor
40 */
42PhysicsObject(const PhysicsObject& copy) {
43 operator=(copy);
44}
45
46/**
47 * Destructor
48 */
52
53/**
54 *
55 */
56const PhysicsObject &PhysicsObject::
57operator =(const PhysicsObject &other) {
58 _process_me = other._process_me;
59 _mass = other._mass;
60 _position = other._position;
61 _last_position = other._last_position;
62 _velocity = other._velocity;
63 _orientation = other._orientation;
64 _rotation = other._rotation;
65 _terminal_velocity = other._terminal_velocity;
66 _oriented = other._oriented;
67
68 return *this;
69}
70
71/**
72 * dynamic copy.
73 */
75make_copy() const {
76 return new PhysicsObject(*this);
77}
78
79/**
80 * Adds an impulse and/or torque (i.e. an instantanious change in velocity)
81 * based on how well the offset and impulse align with the center of mass (aka
82 * position). If you wanted to immitate this function you could work out the
83 * impulse and torque and call add_impulse and add_torque respectively.
84 * offset and force are in local coordinates.
85 */
87add_local_impact(const LPoint3 &offset_from_center_of_mass,
88 const LVector3 &force) {
89 nassertv(!offset_from_center_of_mass.is_nan());
90 nassertv(!force.is_nan());
92 _orientation.xform(offset_from_center_of_mass),
93 _orientation.xform(force));
94}
95
96/**
97 * Adds an impulse and/or torque (i.e. an instantanious change in velocity)
98 * based on how well the offset and impulse align with the center of mass (aka
99 * position). If you wanted to immitate this function you could work out the
100 * impulse and torque and call add_impulse and add_torque respectively.
101 * offset and force are in global (or parent) coordinates.
102 */
104add_impact(const LPoint3 &offset,
105 const LVector3 &force) {
106 nassertv(!offset.is_nan());
107 nassertv(!force.is_nan());
108 LVector3 a = offset;
109 LVector3 b = force;
110 a.normalize();
111 b.normalize();
112 a = a.cross(b);
113 PN_stdfloat angle = a.length();
114 if (angle) {
115 LRotation torque(0, 0, 0, 0);
116 PN_stdfloat spin = force.length()*0.1; // todo: this should account for
117 // impact distance and mass.
118 a.normalize();
119 assert(IS_THRESHOLD_EQUAL(a.length(), 1.0f, 0.001f));
120 torque.set_from_axis_angle(spin, a);
121 add_torque(torque);
122 }
123 LVector3 impulse = (1.0f - angle) * force;
124 add_impulse(impulse);
125}
126
127/**
128 * returns a transform matrix to this object's local coordinate system.
129 */
131get_lcs() const {
132 LMatrix4 m = LMatrix4::translate_mat(_position);
133 if (_oriented) {
134 m=m*_orientation;
135 }
136 nassertr(!m.is_nan(), m);
137 return m;
138}
139
140/**
141 * returns a transform matrix that represents the object's willingness to be
142 * forced.
143 */
145get_inertial_tensor() const {
146 return LMatrix4::ident_mat();
147}
148
149/**
150 * Write a string representation of this instance to <out>.
151 */
153output(std::ostream &out) const {
154 #ifndef NDEBUG //[
155 out<<"PhysicsObject";
156 #endif //] NDEBUG
157}
158
159/**
160 * Write a string representation of this instance to <out>.
161 */
163write(std::ostream &out, int indent) const {
164 #ifndef NDEBUG //[
165 out.width(indent);
166 out<<""<<"PhysicsObject "<<_name<<"\n";
167 out.width(indent+2); out<<""; out<<"_position "<<_position<<"\n";
168 out.width(indent+2); out<<""; out<<"_last_position "<<_last_position<<"\n";
169 out.width(indent+2); out<<""; out<<"_velocity "<<_velocity<<"\n";
170 out.width(indent+2); out<<""; out<<"(implicit velocity "<<get_implicit_velocity()<<")\n";
171 out.width(indent+2); out<<""; out<<"_orientation "<<_orientation<<"\n";
172 out.width(indent+2); out<<""; out<<"(hpr "<<_orientation.get_hpr()<<")\n";
173 out.width(indent+2); out<<""; out<<"_rotation "<<_rotation<<"\n";
174 out.width(indent+2); out<<""; out<<"_terminal_velocity "<<_terminal_velocity<<"\n";
175 out.width(indent+2); out<<""; out<<"_mass "<<_mass<<"\n";
176 out.width(indent+2); out<<""; out<<"_process_me "<<_process_me<<"\n";
177 out.width(indent+2); out<<""; out<<"_oriented "<<_oriented<<"\n";
178 #endif //] NDEBUG
179}
This is a convenience class to specialize ConfigVariable as a floating- point type.
A body on which physics will be applied.
virtual PhysicsObject * make_copy() const
dynamic copy.
PhysicsObject()
Default Constructor.
virtual void output(std::ostream &out) const
Write a string representation of this instance to <out>.
virtual ~PhysicsObject()
Destructor.
virtual void add_local_impact(const LPoint3 &offset_from_center_of_mass, const LVector3 &impulse)
Adds an impulse and/or torque (i.e.
virtual LMatrix4 get_inertial_tensor() const
returns a transform matrix that represents the object's willingness to be forced.
virtual LMatrix4 get_lcs() const
returns a transform matrix to this object's local coordinate system.
void add_torque(const LRotation &torque)
Adds an torque force (i.e.
virtual void write(std::ostream &out, int indent=0) const
Write a string representation of this instance to <out>.
void add_impulse(const LVector3 &impulse)
Adds an impulse force (i.e.
get_implicit_velocity
Velocity Query over the last dt.
virtual void add_impact(const LPoint3 &offset_from_center_of_mass, const LVector3 &impulse)
Adds an impulse and/or torque (i.e.
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition indent.cxx:20
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.