Skip to content

Commit 8a6e921

Browse files
committedOct 26, 2015
Fix jittering sounds on entities (fixes #2974)
1 parent 2c25107 commit 8a6e921

File tree

3 files changed

+40
-59
lines changed

3 files changed

+40
-59
lines changed
 

Diff for: ‎src/collision.cpp

+36-57
Original file line numberDiff line numberDiff line change
@@ -379,16 +379,14 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
379379

380380
int loopcount = 0;
381381

382-
while(dtime > BS*1e-10)
383-
{
382+
while(dtime > BS * 1e-10) {
384383
//TimeTaker tt3("collisionMoveSimple dtime loop");
385384
ScopeProfiler sp(g_profiler, "collisionMoveSimple dtime loop avg", SPT_AVG);
386385

387386
// Avoid infinite loop
388387
loopcount++;
389-
if(loopcount >= 100)
390-
{
391-
warningstream<<"collisionMoveSimple: Loop count exceeded, aborting to avoid infiniite loop"<<std::endl;
388+
if (loopcount >= 100) {
389+
warningstream << "collisionMoveSimple: Loop count exceeded, aborting to avoid infiniite loop" << std::endl;
392390
dtime = 0;
393391
break;
394392
}
@@ -404,8 +402,7 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
404402
/*
405403
Go through every nodebox, find nearest collision
406404
*/
407-
for(u32 boxindex = 0; boxindex < cboxes.size(); boxindex++)
408-
{
405+
for (u32 boxindex = 0; boxindex < cboxes.size(); boxindex++) {
409406
// Ignore if already stepped up this nodebox.
410407
if(is_step_up[boxindex])
411408
continue;
@@ -415,26 +412,22 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
415412
int collided = axisAlignedCollision(
416413
cboxes[boxindex], movingbox, speed_f, d, dtime_tmp);
417414

418-
if(collided == -1 || dtime_tmp >= nearest_dtime)
415+
if (collided == -1 || dtime_tmp >= nearest_dtime)
419416
continue;
420417

421418
nearest_dtime = dtime_tmp;
422419
nearest_collided = collided;
423420
nearest_boxindex = boxindex;
424421
}
425422

426-
if(nearest_collided == -1)
427-
{
423+
if (nearest_collided == -1) {
428424
// No collision with any collision box.
429425
pos_f += speed_f * dtime;
430426
dtime = 0; // Set to 0 to avoid "infinite" loop due to small FP numbers
431-
}
432-
else
433-
{
427+
} else {
434428
// Otherwise, a collision occurred.
435429

436430
const aabb3f& cbox = cboxes[nearest_boxindex];
437-
438431
// Check for stairs.
439432
bool step_up = (nearest_collided != 1) && // must not be Y direction
440433
(movingbox.MinEdge.Y < cbox.MaxEdge.Y) &&
@@ -448,67 +441,56 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
448441
float bounce = -(float)bouncy_values[nearest_boxindex] / 100.0;
449442

450443
// Move to the point of collision and reduce dtime by nearest_dtime
451-
if(nearest_dtime < 0)
452-
{
444+
if (nearest_dtime < 0) {
453445
// Handle negative nearest_dtime (can be caused by the d allowance)
454-
if(!step_up)
455-
{
456-
if(nearest_collided == 0)
446+
if (!step_up) {
447+
if (nearest_collided == 0)
457448
pos_f.X += speed_f.X * nearest_dtime;
458-
if(nearest_collided == 1)
449+
if (nearest_collided == 1)
459450
pos_f.Y += speed_f.Y * nearest_dtime;
460-
if(nearest_collided == 2)
451+
if (nearest_collided == 2)
461452
pos_f.Z += speed_f.Z * nearest_dtime;
462453
}
463-
}
464-
else
465-
{
454+
} else {
466455
pos_f += speed_f * nearest_dtime;
467456
dtime -= nearest_dtime;
468457
}
469458

470459
bool is_collision = true;
471-
if(is_unloaded[nearest_boxindex])
460+
if (is_unloaded[nearest_boxindex])
472461
is_collision = false;
473462

474463
CollisionInfo info;
475-
if (is_object[nearest_boxindex]) {
464+
if (is_object[nearest_boxindex])
476465
info.type = COLLISION_OBJECT;
477-
}
478-
else {
466+
else
479467
info.type = COLLISION_NODE;
480-
}
468+
481469
info.node_p = node_positions[nearest_boxindex];
482470
info.bouncy = bouncy;
483471
info.old_speed = speed_f;
484472

485473
// Set the speed component that caused the collision to zero
486-
if(step_up)
487-
{
474+
if (step_up) {
488475
// Special case: Handle stairs
489476
is_step_up[nearest_boxindex] = true;
490477
is_collision = false;
491-
}
492-
else if(nearest_collided == 0) // X
493-
{
494-
if(fabs(speed_f.X) > BS*3)
478+
} else if(nearest_collided == 0) { // X
479+
if (fabs(speed_f.X) > BS * 3)
495480
speed_f.X *= bounce;
496481
else
497482
speed_f.X = 0;
498483
result.collides = true;
499484
result.collides_xz = true;
500485
}
501-
else if(nearest_collided == 1) // Y
502-
{
503-
if(fabs(speed_f.Y) > BS*3)
486+
else if(nearest_collided == 1) { // Y
487+
if(fabs(speed_f.Y) > BS * 3)
504488
speed_f.Y *= bounce;
505489
else
506490
speed_f.Y = 0;
507491
result.collides = true;
508-
}
509-
else if(nearest_collided == 2) // Z
510-
{
511-
if(fabs(speed_f.Z) > BS*3)
492+
} else if(nearest_collided == 2) { // Z
493+
if (fabs(speed_f.Z) > BS * 3)
512494
speed_f.Z *= bounce;
513495
else
514496
speed_f.Z = 0;
@@ -517,10 +499,10 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
517499
}
518500

519501
info.new_speed = speed_f;
520-
if(info.new_speed.getDistanceFrom(info.old_speed) < 0.1*BS)
502+
if (info.new_speed.getDistanceFrom(info.old_speed) < 0.1 * BS)
521503
is_collision = false;
522504

523-
if(is_collision){
505+
if (is_collision) {
524506
result.collisions.push_back(info);
525507
}
526508
}
@@ -532,8 +514,7 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
532514
aabb3f box = box_0;
533515
box.MinEdge += pos_f;
534516
box.MaxEdge += pos_f;
535-
for(u32 boxindex = 0; boxindex < cboxes.size(); boxindex++)
536-
{
517+
for (u32 boxindex = 0; boxindex < cboxes.size(); boxindex++) {
537518
const aabb3f& cbox = cboxes[boxindex];
538519

539520
/*
@@ -545,23 +526,21 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
545526
546527
Use 0.15*BS so that it is easier to get on a node.
547528
*/
548-
if(
549-
cbox.MaxEdge.X-d > box.MinEdge.X &&
550-
cbox.MinEdge.X+d < box.MaxEdge.X &&
551-
cbox.MaxEdge.Z-d > box.MinEdge.Z &&
552-
cbox.MinEdge.Z+d < box.MaxEdge.Z
553-
){
554-
if(is_step_up[boxindex])
555-
{
529+
if (cbox.MaxEdge.X - d > box.MinEdge.X && cbox.MinEdge.X + d < box.MaxEdge.X &&
530+
cbox.MaxEdge.Z - d > box.MinEdge.Z &&
531+
cbox.MinEdge.Z + d < box.MaxEdge.Z) {
532+
if (is_step_up[boxindex]) {
556533
pos_f.Y += (cbox.MaxEdge.Y - box.MinEdge.Y);
557534
box = box_0;
558535
box.MinEdge += pos_f;
559536
box.MaxEdge += pos_f;
560537
}
561-
if(fabs(cbox.MaxEdge.Y-box.MinEdge.Y) < 0.15*BS)
562-
{
538+
if (fabs(cbox.MaxEdge.Y - box.MinEdge.Y) < 0.15 * BS) {
563539
result.touching_ground = true;
564-
if(is_unloaded[boxindex])
540+
541+
if (is_object[boxindex])
542+
result.standing_on_object = true;
543+
if (is_unloaded[boxindex])
565544
result.standing_on_unloaded = true;
566545
}
567546
}

Diff for: ‎src/collision.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,15 @@ struct collisionMoveResult
5757
bool collides;
5858
bool collides_xz;
5959
bool standing_on_unloaded;
60+
bool standing_on_object;
6061
std::vector<CollisionInfo> collisions;
6162

6263
collisionMoveResult():
6364
touching_ground(false),
6465
collides(false),
6566
collides_xz(false),
66-
standing_on_unloaded(false)
67+
standing_on_unloaded(false),
68+
standing_on_object(false)
6769
{}
6870
};
6971

Diff for: ‎src/localplayer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d,
344344
}
345345
}
346346

347-
if(!touching_ground_was && touching_ground){
347+
if(!result.standing_on_object && !touching_ground_was && touching_ground) {
348348
MtEvent *e = new SimpleTriggerEvent("PlayerRegainGround");
349349
m_gamedef->event()->put(e);
350350

1 commit comments

Comments
 (1)

HybridDog commented on Oct 31, 2015

@HybridDog
Contributor

If you stand on an object and it suddenly moves up, you fall through it, don't you?
Maybe you could assign the player to it if he/she is standing on it.

Please sign in to comment.