- cycle = sin(M_PI + M_PI * (cycle-cl_bobup_side.value)/(1.0 - cl_bobup_side.value));
- // bob is proportional to velocity in the xy plane
- // (don't count Z, or jumping messes it up)
- bob = xyspeed * cl_bob_side.value;
- bob = bob*0.3 + bob*0.7*cycle;
- //vieworg[1] += bound(-7, bob, 4);
- //vieworg[0] += bound(-7, bob, 4);
+ cycle = cos(M_PI + M_PI * (cycle-0.5)/0.5);
+ bob = bound(0, cl_bob2.value, 0.05) * cycle;
+
+ // this value slowly decreases from 1 to 0 when we stop touching the ground.
+ // The cycle is later multiplied with it so the view smooths back to normal
+ if (cl.onground && !cl.cmd.jump) // also block the effect while the jump button is pressed, to avoid twitches when bunny-hopping
+ cl.bob2_smooth = 1;
+ else
+ {
+ if(cl.bob2_smooth > 0)
+ cl.bob2_smooth -= bound(0, cl_bob2smooth.value, 1);
+ else
+ cl.bob2_smooth = 0;
+ }
+
+ // calculate the front and side of the player between the X and Y axes
+ AngleVectors(viewangles, forward, right, up);
+ // now get the speed based on those angles. The bounds should match the same value as xyspeed's
+ side = bound(-400, DotProduct (cl.velocity, right) * cl.bob2_smooth, 400);
+ front = bound(-400, DotProduct (cl.velocity, forward) * cl.bob2_smooth, 400);
+ VectorScale(forward, bob, forward);
+ VectorScale(right, bob, right);
+ // we use side with forward and front with right, so the bobbing goes
+ // to the side when we walk forward and to the front when we strafe
+ VectorMAMAM(side, forward, front, right, 0, up, bob2vel);
+ vieworg[0] += bob2vel[0];
+ vieworg[1] += bob2vel[1];