Symbroson

01-05-2018, 04:47 PM

Hi there,

I am currently working on my first 3D game with OpenGL, but I just came til the controls. There appeared one problem I really couldn't solve:

The player (a cube) should move in the direction I am looking to. Sounds simple right? My approach was to rotate the accelerated velocity vector relatively to the view directon and add this to the position vector. The code looks like this:

#define X 0

#define Y 1

#define Z 2

#define vFromH(v, a) sqrt(-2*a*v)

Object3D* player;

const float

bounce = 0.2f, //bounciness

a[3] = { 40, 20, 40 }, //acceleration (force)

vmax[3] = { 5, 100, 5 }, //max velocity

vJump = vFromH(5, -a[Y]); //jump strength

int8_t ad[3] = { 0, -1, 0 }; //acc direction

glm::vec3 v(0); //velocity

void move() {

glm::vec3 dv(

getForce(X),

getForce(Y),

getForce(Z)

);

v += glm::rotate(dv, player->rot.y, glm::vec3(0, -1, 0));

player->pos += v * Time::diff; //float Time::diff: delta time in seconds

}

float getForce(uint8_t d) {

float dv;

if (ad[d]) { //accelerate

dv = ad[d] * a[d] * Time::diff;

if (abs(v[d] + dv) > vmax[d]) {

if (v[d] + dv > 0) return vmax[d] - v[d];

return -vmax[d] - v[d];

}

} else { //brake

if (!v[d]) return 0;

dv = a[d] * Time::diff;

if (v[d] > 0) dv = -dv;

if (v[d] * (v[d] + dv) < 0) return -v[d]; //if change of sign -> v = 0

}

return dv;

}

The important line is v += glm::rotate(dv, player->rot.y, glm::vec3(0, -1, 0)); in void move()

The Problem is, that the player is moving in a spiral shape if I try to move and my looking angle is over 90 degrees. If the angle is smaller he's moving like I'm not rotated (in x, z direction)

If I remove the rotation and replace it with 'v += dv;' it is working as expected (in x,z direction).

My Keyboard input just changes ad[...] which is the acceleration vector. if ad[n] == 0 the player is braking.

I hope these Informations are enough to understand my problem. Case not I've attached the source files of my VS Project (I couldn't attach the whole project behause the external header and lib files were too large)

All important stuff regarding the game happens in Game.cpp (especially line 47)

Note that I am learning from this tutorial site: http://www.opengl-tutorial.org

They also have a moving example (http://www.opengl-tutorial.org/beginners-tutorials/tutorial-6-keyboard-and-mouse) but it is a bit different to the way I am doing it because I am using acceleration.

Many thanks in advance,

Symbroson

I am currently working on my first 3D game with OpenGL, but I just came til the controls. There appeared one problem I really couldn't solve:

The player (a cube) should move in the direction I am looking to. Sounds simple right? My approach was to rotate the accelerated velocity vector relatively to the view directon and add this to the position vector. The code looks like this:

#define X 0

#define Y 1

#define Z 2

#define vFromH(v, a) sqrt(-2*a*v)

Object3D* player;

const float

bounce = 0.2f, //bounciness

a[3] = { 40, 20, 40 }, //acceleration (force)

vmax[3] = { 5, 100, 5 }, //max velocity

vJump = vFromH(5, -a[Y]); //jump strength

int8_t ad[3] = { 0, -1, 0 }; //acc direction

glm::vec3 v(0); //velocity

void move() {

glm::vec3 dv(

getForce(X),

getForce(Y),

getForce(Z)

);

v += glm::rotate(dv, player->rot.y, glm::vec3(0, -1, 0));

player->pos += v * Time::diff; //float Time::diff: delta time in seconds

}

float getForce(uint8_t d) {

float dv;

if (ad[d]) { //accelerate

dv = ad[d] * a[d] * Time::diff;

if (abs(v[d] + dv) > vmax[d]) {

if (v[d] + dv > 0) return vmax[d] - v[d];

return -vmax[d] - v[d];

}

} else { //brake

if (!v[d]) return 0;

dv = a[d] * Time::diff;

if (v[d] > 0) dv = -dv;

if (v[d] * (v[d] + dv) < 0) return -v[d]; //if change of sign -> v = 0

}

return dv;

}

The important line is v += glm::rotate(dv, player->rot.y, glm::vec3(0, -1, 0)); in void move()

The Problem is, that the player is moving in a spiral shape if I try to move and my looking angle is over 90 degrees. If the angle is smaller he's moving like I'm not rotated (in x, z direction)

If I remove the rotation and replace it with 'v += dv;' it is working as expected (in x,z direction).

My Keyboard input just changes ad[...] which is the acceleration vector. if ad[n] == 0 the player is braking.

I hope these Informations are enough to understand my problem. Case not I've attached the source files of my VS Project (I couldn't attach the whole project behause the external header and lib files were too large)

All important stuff regarding the game happens in Game.cpp (especially line 47)

Note that I am learning from this tutorial site: http://www.opengl-tutorial.org

They also have a moving example (http://www.opengl-tutorial.org/beginners-tutorials/tutorial-6-keyboard-and-mouse) but it is a bit different to the way I am doing it because I am using acceleration.

Many thanks in advance,

Symbroson