0
\$\begingroup\$

Right now my player can move forward and backward using

case 'a':case 'A':
    speed.x = lx * 0.1;
    speed.z = lz * 0.1;
    break;
case 'b':case 'B':
    speed.x = -(lx * 0.1);    
    speed.z = -(lz * 0.1);
    break;

Ly is equal sin(vertAngle);

Lz is equal -cos(horAngle);

What should i do for sideways movement?

\$\endgroup\$
2
  • 3
    \$\begingroup\$ Hint: If the vector (lx,lz) points in the direction of the character, the vector (-lz,lx) points to the character's left. \$\endgroup\$
    – user10968
    Jun 24, 2014 at 3:47
  • \$\begingroup\$ vertAngle += ninetyDegrees; horAngle += ninetyDegrees; \$\endgroup\$ Jun 24, 2014 at 3:47

1 Answer 1

1
\$\begingroup\$

Since no one answered, I'll write a more complete answer here:

Hint: If the vector (lx,lz) points in the direction of the character, the vector (-lz,lx) points to the character's left.

So something like:

case 'c':case 'C':
    speed.x =-lz * 0.1;
    speed.z = lx * 0.1;
    break;
case 'd':case 'D':
    speed.x = lz * 0.1;    
    speed.z =-lx * 0.1;
    break;

should work, where "c" moves left. If you want to move forward AND strafe, you can just say "speed.x=0; speed.z=0" and the beginning, and replace each "=" with a "+=". If speed-weirdness bothers you (if speed.x=1 and speed.z=1, the total speed is actually 1.41), you can normalize the speed after this procedure. "normalization" means reducing the length to one, so the full code would be something like:

speed.x=0;
speed.z=0;
case 'a':case 'A':
    speed.x += lx;
    speed.z += lz;
    break;
case 'b':case 'B':
    speed.x += -lx;    
    speed.z += -lz;
    break;
case 'c':case 'C':
    speed.x +=-lz; //left movement
    speed.z += lx;
    break;
case 'd':case 'D': //right movement
    speed.x += lz;    
    speed.z +=-lx;
    break;
dist=sqrt(speed.x*speed.x+speed.z*speed.z); //total speed
if(dist>0){ //prevent against division by zero
    speed.x=(speed.x/dist)*.1; //ensure the total speed is precisely 0.1
    speed.z=(speed.z/dist)*.1;
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .