mirror of
https://github.com/panda3d/panda3d.git
synced 2025-10-03 10:22:45 -04:00
work in progress, working on jump clipping against a wall
This commit is contained in:
parent
ce06ce38a7
commit
51d2276dad
@ -245,12 +245,16 @@ handle_entries() {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// This is the part where the node actually gets moved:
|
||||||
CPT(TransformState) trans = def._target.get_transform();
|
CPT(TransformState) trans = def._target.get_transform();
|
||||||
LVecBase3f pos = trans->get_pos();
|
LVecBase3f pos = trans->get_pos();
|
||||||
pos += net_shove * trans->get_mat();
|
pos += net_shove * trans->get_mat();
|
||||||
def._target.set_transform(trans->set_pos(pos));
|
def._target.set_transform(trans->set_pos(pos));
|
||||||
def.updated_transform();
|
def.updated_transform();
|
||||||
|
|
||||||
|
// We call this to allow derived classes to do other
|
||||||
|
// fix-ups as they see fit:
|
||||||
|
apply_net_shove(def, net_shove, force_normal);
|
||||||
apply_linear_force(def, force_normal);
|
apply_linear_force(def, force_normal);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -261,10 +265,22 @@ handle_entries() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
// Function: CollisionHandlerPusher::apply_linear_force
|
// Function: CollisionHandlerPusher::apply_net_shove
|
||||||
// Access: Protected, Virtual
|
// Access: Protected, Virtual
|
||||||
// Description:
|
// Description: This is an optional hook for derived classes to do
|
||||||
|
// some work with the ColliderDef and the force vector.
|
||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
void CollisionHandlerPusher::
|
void CollisionHandlerPusher::
|
||||||
apply_linear_force(ColliderDef &, const LVector3f &) {
|
apply_net_shove(ColliderDef &def, const LVector3f &net_shove,
|
||||||
|
const LVector3f &force_normal) {
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
// Function: CollisionHandlerPusher::apply_linear_force
|
||||||
|
// Access: Protected, Virtual
|
||||||
|
// Description: This is an optional hook for derived classes to do
|
||||||
|
// some work with the ColliderDef and the force vector.
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
void CollisionHandlerPusher::
|
||||||
|
apply_linear_force(ColliderDef &def, const LVector3f &force_normal) {
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,7 @@ PUBLISHED:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual bool handle_entries();
|
virtual bool handle_entries();
|
||||||
|
virtual void apply_net_shove(ColliderDef &def, const LVector3f &net_shove, const LVector3f &force_normal);
|
||||||
virtual void apply_linear_force(ColliderDef &def, const LVector3f &force);
|
virtual void apply_linear_force(ColliderDef &def, const LVector3f &force);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -15,3 +15,36 @@
|
|||||||
// panda3d@yahoogroups.com .
|
// panda3d@yahoogroups.com .
|
||||||
//
|
//
|
||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
|
INLINE void PhysicsCollisionHandler::
|
||||||
|
set_almost_stationary_speed(float speed) {
|
||||||
|
_almost_stationary_speed = speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
INLINE float PhysicsCollisionHandler::
|
||||||
|
get_almost_stationary_speed() {
|
||||||
|
return _almost_stationary_speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
INLINE void PhysicsCollisionHandler::
|
||||||
|
set_static_friction_coef(float coef) {
|
||||||
|
_static_friction_coef = coef;
|
||||||
|
}
|
||||||
|
|
||||||
|
INLINE float PhysicsCollisionHandler::
|
||||||
|
get_static_friction_coef() {
|
||||||
|
return _static_friction_coef;
|
||||||
|
}
|
||||||
|
|
||||||
|
INLINE void PhysicsCollisionHandler::
|
||||||
|
set_dynamic_friction_coef(float coef) {
|
||||||
|
_dynamic_friction_coef = coef;
|
||||||
|
}
|
||||||
|
|
||||||
|
INLINE float PhysicsCollisionHandler::
|
||||||
|
get_dynamic_friction_coef() {
|
||||||
|
return _dynamic_friction_coef;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -32,6 +32,9 @@ TypeHandle PhysicsCollisionHandler::_type_handle;
|
|||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
PhysicsCollisionHandler::
|
PhysicsCollisionHandler::
|
||||||
PhysicsCollisionHandler() {
|
PhysicsCollisionHandler() {
|
||||||
|
_almost_stationary_speed = 0.1f;
|
||||||
|
_static_friction_coef=0.9f;
|
||||||
|
_dynamic_friction_coef=0.5f;
|
||||||
set_horizontal(false);
|
set_horizontal(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,6 +47,46 @@ PhysicsCollisionHandler::
|
|||||||
~PhysicsCollisionHandler() {
|
~PhysicsCollisionHandler() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
// Function: PhysicsCollisionHandler::apply_friction
|
||||||
|
// Access:
|
||||||
|
// Description: The vel parameter will be modified in place to
|
||||||
|
// account for friction.
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
void PhysicsCollisionHandler::
|
||||||
|
apply_friction(ColliderDef &def, LVector3f& vel, const LVector3f& force, float angle) {
|
||||||
|
if (vel!=LVector3f::zero()) {
|
||||||
|
float friction_coefficient=0.0f;
|
||||||
|
// Determine the friction:
|
||||||
|
if (fabs(force.dot(LVector3f::unit_z())<0.5f)) {
|
||||||
|
// ...The force is nearly horizontal, it is probably a wall.
|
||||||
|
physics_debug(" wall friction");
|
||||||
|
friction_coefficient=0.0f;
|
||||||
|
} else if (vel.length()<_almost_stationary_speed) {
|
||||||
|
physics_debug(" static friction");
|
||||||
|
friction_coefficient=_static_friction_coef;
|
||||||
|
} else {
|
||||||
|
physics_debug(" dynamic friction");
|
||||||
|
friction_coefficient=_dynamic_friction_coef;
|
||||||
|
}
|
||||||
|
// Apply the friction:
|
||||||
|
physics_debug(" vel pre friction "<<vel<<" len "<<vel.length());
|
||||||
|
float friction=friction_coefficient*angle;
|
||||||
|
physics_debug(" friction "<<friction);
|
||||||
|
if (friction<0.0f && friction>1.0f) {
|
||||||
|
cerr<<"\n\nfriction error "<<friction<<endl;
|
||||||
|
friction=1.0f;
|
||||||
|
}
|
||||||
|
#if 0
|
||||||
|
float dt=ClockObject::get_global_clock()->get_dt();
|
||||||
|
vel *= (1.0f-friction) * dt * dt;
|
||||||
|
#else
|
||||||
|
vel *= 1.0f-friction;
|
||||||
|
#endif
|
||||||
|
physics_debug(" vel post friction "<<vel<<" len "<<vel.length());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
// Function: PhysicsCollisionHandler::apply_linear_force
|
// Function: PhysicsCollisionHandler::apply_linear_force
|
||||||
// Access: Protected, Virtual
|
// Access: Protected, Virtual
|
||||||
@ -51,7 +94,15 @@ PhysicsCollisionHandler::
|
|||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
void PhysicsCollisionHandler::
|
void PhysicsCollisionHandler::
|
||||||
apply_linear_force(ColliderDef &def, const LVector3f &force) {
|
apply_linear_force(ColliderDef &def, const LVector3f &force) {
|
||||||
CollisionHandlerPusher::apply_linear_force(def, force);
|
}
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
// Function: PhysicsCollisionHandler::apply_net_shove
|
||||||
|
// Access: Protected, Virtual
|
||||||
|
// Description:
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
void PhysicsCollisionHandler::
|
||||||
|
apply_net_shove(ColliderDef &def, const LVector3f& net_shove, const LVector3f &force) {
|
||||||
|
CollisionHandlerPusher::apply_net_shove(def, net_shove, force);
|
||||||
if (force == LVector3f::zero()) {
|
if (force == LVector3f::zero()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -66,6 +117,7 @@ apply_linear_force(ColliderDef &def, const LVector3f &force) {
|
|||||||
}
|
}
|
||||||
physics_debug("apply_linear_force() {");
|
physics_debug("apply_linear_force() {");
|
||||||
physics_debug(" vel "<<vel<<" len "<<vel.length());
|
physics_debug(" vel "<<vel<<" len "<<vel.length());
|
||||||
|
physics_debug(" net_shove "<<net_shove<<" len "<<net_shove.length());
|
||||||
physics_debug(" force "<<force<<" len "<<force.length());
|
physics_debug(" force "<<force<<" len "<<force.length());
|
||||||
LVector3f old_vel=vel;
|
LVector3f old_vel=vel;
|
||||||
|
|
||||||
@ -77,7 +129,7 @@ apply_linear_force(ColliderDef &def, const LVector3f &force) {
|
|||||||
//NodePath np(def._node);
|
//NodePath np(def._node);
|
||||||
//CPT(TransformState) trans = np.get_net_transform();
|
//CPT(TransformState) trans = np.get_net_transform();
|
||||||
//adjustment=adjustment*trans->get_mat();
|
//adjustment=adjustment*trans->get_mat();
|
||||||
physics_debug(" adjustment trn "<<adjustment<<" len "<<adjustment.length());
|
//physics_debug(" adjustment trn "<<adjustment<<" len "<<adjustment.length());
|
||||||
|
|
||||||
adjustment=adjustment*actor->get_physics_object()->get_lcs();
|
adjustment=adjustment*actor->get_physics_object()->get_lcs();
|
||||||
physics_debug(" adjustment lcs "<<adjustment<<" len "<<adjustment.length());
|
physics_debug(" adjustment lcs "<<adjustment<<" len "<<adjustment.length());
|
||||||
@ -94,37 +146,16 @@ apply_linear_force(ColliderDef &def, const LVector3f &force) {
|
|||||||
physics_debug(" positive contact");
|
physics_debug(" positive contact");
|
||||||
adjustment*=adjustmentLength;
|
adjustment*=adjustmentLength;
|
||||||
physics_debug(" adjustment mul "<<adjustment<<" len "<<adjustment.length());
|
physics_debug(" adjustment mul "<<adjustment<<" len "<<adjustment.length());
|
||||||
|
|
||||||
// This adjustment to our velocity will not reflect us off the surface,
|
// This adjustment to our velocity will not reflect us off the surface,
|
||||||
// but will deflect us parallel (or tangent) to the surface:
|
// but will deflect us parallel (or tangent) to the surface:
|
||||||
vel+=adjustment;
|
if (normalize(net_shove).dot(LVector3f::unit_z())>0.5) {
|
||||||
physics_debug(" vel "<<vel<<" len "<<vel.length());
|
vel=LVector3f::zero();
|
||||||
if (vel!=LVector3f::zero()) {
|
|
||||||
const float almostStationary=0.1f;
|
|
||||||
float frictionCoefficient=0.0f;
|
|
||||||
// Determine the friction:
|
|
||||||
if (vel.length()<almostStationary) {
|
|
||||||
physics_debug(" static friction");
|
|
||||||
frictionCoefficient=0.9f;
|
|
||||||
} else {
|
|
||||||
physics_debug(" dynamic friction");
|
|
||||||
frictionCoefficient=0.5f;
|
|
||||||
}
|
|
||||||
// Apply the friction:
|
|
||||||
physics_debug(" vel pre friction "<<vel<<" len "<<vel.length());
|
|
||||||
float friction=frictionCoefficient*angle;
|
|
||||||
physics_debug(" friction "<<friction);
|
|
||||||
if (friction<0.0f && friction>1.0f) {
|
|
||||||
cerr<<"\n\nfriction error "<<friction<<endl;
|
|
||||||
friction=1.0f;
|
|
||||||
}
|
|
||||||
#if 0
|
|
||||||
float dt=ClockObject::get_global_clock()->get_dt();
|
|
||||||
vel *= (1.0f-friction) * dt * dt;
|
|
||||||
#else
|
|
||||||
vel *= 1.0f-friction;
|
|
||||||
#endif
|
|
||||||
physics_debug(" vel post friction "<<vel<<" len "<<vel.length());
|
|
||||||
}
|
}
|
||||||
|
//vel+=adjustment;
|
||||||
|
physics_debug(" vel+adj "<<vel<<" len "<<vel.length());
|
||||||
|
|
||||||
|
//apply_friction(def, vel, force, angle);
|
||||||
} else if (adjustmentLength==0.0f) {
|
} else if (adjustmentLength==0.0f) {
|
||||||
physics_debug(" brushing contact");
|
physics_debug(" brushing contact");
|
||||||
} else {
|
} else {
|
||||||
@ -132,9 +163,15 @@ apply_linear_force(ColliderDef &def, const LVector3f &force) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifndef NDEBUG //[
|
#ifndef NDEBUG //[
|
||||||
if (vel.length() > old_vel.length()) {
|
if (IS_THRESHOLD_EQUAL(vel.length(), old_vel.length(), 0.0001f)) {
|
||||||
|
// This is a check to see if vel is staying the same:
|
||||||
|
physics_debug(" vel is about the same length: "<<vel.length()<<" ~ "<<old_vel.length());
|
||||||
|
} else if (vel.length() > old_vel.length()) {
|
||||||
// This is a check to avoid adding engergy:
|
// This is a check to avoid adding engergy:
|
||||||
physics_debug(" vel.length() > old_vel.length() "<<vel.length()<<" > "<<old_vel.length());
|
physics_debug(" vel got larger "<<vel.length()<<" > "<<old_vel.length());
|
||||||
|
} else {
|
||||||
|
// This is a check to avoid loosing engergy:
|
||||||
|
physics_debug(" vel got smaller "<<vel.length()<<" < "<<old_vel.length());
|
||||||
}
|
}
|
||||||
if (vel.length() > 10.0f) {
|
if (vel.length() > 10.0f) {
|
||||||
// This is a check to see if the velocity is higher than I expect it
|
// This is a check to see if the velocity is higher than I expect it
|
||||||
|
@ -34,7 +34,23 @@ PUBLISHED:
|
|||||||
PhysicsCollisionHandler();
|
PhysicsCollisionHandler();
|
||||||
virtual ~PhysicsCollisionHandler();
|
virtual ~PhysicsCollisionHandler();
|
||||||
|
|
||||||
|
// These setters and getter are a bit of a hack:
|
||||||
|
INLINE void set_almost_stationary_speed(float speed);
|
||||||
|
INLINE float get_almost_stationary_speed();
|
||||||
|
|
||||||
|
INLINE void set_static_friction_coef(float coef);
|
||||||
|
INLINE float get_static_friction_coef();
|
||||||
|
|
||||||
|
INLINE void set_dynamic_friction_coef(float coef);
|
||||||
|
INLINE float get_dynamic_friction_coef();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
float _almost_stationary_speed;
|
||||||
|
float _static_friction_coef;
|
||||||
|
float _dynamic_friction_coef;
|
||||||
|
|
||||||
|
void apply_friction(ColliderDef &def, LVector3f &vel, const LVector3f& force, float angle);
|
||||||
|
virtual void apply_net_shove(ColliderDef &def, const LVector3f &net_shove, const LVector3f &force_normal);
|
||||||
virtual void apply_linear_force(ColliderDef &def, const LVector3f &force);
|
virtual void apply_linear_force(ColliderDef &def, const LVector3f &force);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user