# uselessChiP

Member

8

165 Neutral

• Rank
Newbie
1. ## Modelling 3D Movement Physics

I think i've improved the code, now looks like this:    void Update() { MouseRotation(); Movement(); } private void Movement() { Vector3 moveDir = Input.GetAxisRaw("Horizontal") * transform.right + Input.GetAxisRaw("Vertical") * transform.forward; velocity += (moveDir * acceleration); velocity.x = Mathf.Clamp(velocity.x, -maxSpeed, maxSpeed); velocity.z = Mathf.Clamp(velocity.z, -maxSpeed, maxSpeed); if (Mathf.Abs(velocity.x) - friction > 0) velocity.x -= Mathf.Sign(velocity.x) * friction; else velocity.x = 0; if (Mathf.Abs(velocity.z) - friction > 0) velocity.z -= Mathf.Sign(velocity.z) * friction; else velocity.z = 0; controller.Move(velocity * Time.deltaTime); } private void MouseRotation() { rotX += Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime; rotX += Input.GetAxisRaw("Arrow H") * mouseSensitivityX * Time.deltaTime; rotX = rotX % 360; rotY += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime; rotY += Input.GetAxis("Arrow V") * mouseSensitivityY * Time.deltaTime; rotY = Mathf.Clamp(rotY, -maxMouseVerticalRotation, maxMouseVerticalRotation); transform.rotation = Quaternion.Euler(0, rotX, 0); camera.rotation = Quaternion.Euler(-rotY, rotX, 0); }   Now the problem is that the character moves only on the 4 main axis (forward, backwards, left, right) and at 45 degrees between them.   For example in this image:         The character moves forwards (red arrow) even if it is facing the green arrow.   Do you know what's wrong with the code?
2. ## Modelling 3D Movement Physics

Hi, i'm trying to model a movement system for a fps like game, i want something like the movement in borderlands 2. I don't know very much about physics and i'm having a hard time finding something useful online.   This is what i got so far (i'm using unity and the player uses a character controller): void Update() { MouseRotation(); Movement(); } private void Movement() { float h = Input.GetAxisRaw("Horizontal"); float v = Input.GetAxisRaw("Vertical"); bool isMoving = (h != 0 || v != 0) ? true : false; if (controller.isGrounded) { if (isMoving) speed += acceleration ; speed -= friction; if (speed < 0) speed = 0; if (Input.GetButtonDown("Jump")) moveDirection.y = jumpSpeed; } else { if (isMoving) speed += airAcceleration; moveDirection.y -= gravity * Time.deltaTime; } if (speed > maxSpeed) speed = maxSpeed; moveDirection = new Vector3(h * speed, moveDirection.y, v * speed); // moveDirection.y -= gravity * Time.deltaTime; localMoveDir = transform.TransformDirection(moveDirection); controller.Move(localMoveDir * Time.deltaTime); } private void MouseRotation() { rotX += Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime; rotY += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime; rotY = Mathf.Clamp(rotY, -maxMouseVerticalRotation, maxMouseVerticalRotation); camera.rotation = Quaternion.Euler(-rotY, rotX, 0); transform.rotation = Quaternion.Euler(0, rotX, 0); } (this code does not work properly cause when i don't press anything the speed goes instantly to 0)   The two main thing that i thing are missing are that, when i press, for example, forward and then back, the character changes direction abruptly and, the most important thing, I think that the speed in borderlands is affected by the mouse rotation, when for example i rotate 180 degree the character slows down and then start again gaining speed. I'm not sure how to do this (especially the mouse rotation part).   Can you give me some suggestion on how i could model a movement system almost identical to the one borderlands use?   Thanks.

4. ## My opengl program is really slow, what's the problem?

Hi, sorry for the late reply. So i tried to change the code so the get uniform location is called only once(i was confused about the getuniformlocation, i did not realize that it actully gives me the uniform position, so thanks now it's more clear), in the constructor of my program class like this: Program::Program() : _ID(0) { _ID = LoadShaders("shaders/Blinn-Phong.vs", "shaders/Blinn-Phong.fs"); _cam = uniform("camera"); _mod = uniform("model"); _rough = uniform("roughness"); _kd = uniform("Kd"); _ks = uniform("Ks"); _lightPos = uniform("lightPos"); _int = uniform("intensity"); _camPos = uniform("cameraPos"); } GLint Program::uniform(const GLchar* uniformName) const { if(!uniformName) throw std::runtime_error("uniformName is NULL"); GLint uniform = glGetUniformLocation(_ID, uniformName); if(uniform == -1) throw std::runtime_error(std::string("Program uniform not found: ") + uniformName); return uniform; }   (the uniform == -1 is still there but i'll change it)   and then set the uniforms without getting again the location:  GLint Program::uniform(ShProp type) const { switch(type) { case CAMERA: return _cam; case MODEL: return _mod; case ROUGHNESS: return _rough; case KD: return _kd; case KS: return _ks; case LIGHTPOS: return _lightPos; case INTENSITY: return _int; case CAMERAPOS: return _camPos; default: return -1; } } void Program::setUniform(ShProp type, GLint v0) { glUniform1i(uniform(type), v0); } void Program::setUniform(ShProp type, const glm::mat4& m) { glUniformMatrix4fv(uniform(type), 1, GL_FALSE, glm::value_ptr(m)); } void Program::setUniform(ShProp type, const glm::vec3& v) { glUniform3fv(uniform(type), 1, glm::value_ptr(v)); } void Program::setUniform(ShProp type, GLfloat v0) { glUniform1f(uniform(type), v0); }   (for now the code is like this because i'm testing)   I also removed the asserts with the glGetIntegerv call but i'm continuing to have 13 fps, and this despite where i'm looking, they never go down or up. So I have the same problem.   As for the index buffer part, I tried using unsigned shorts but after that my rabbit mesh was all messed up so i changed it back to unsigned int.
5. ## My opengl program is really slow, what's the problem?

I think the hardware it's fast enough, i have an amd hd 7950 gpu and a intel i5 3570k cpu both overclocked. 1) thanks for the advice, i will use unsigned short for the indices, in this test my model has more faces than the maximum unsigned short supported but i think i will have lighter models so it should work. 2) the drawing method is called one time per frame per model in the scene, the shader-> use() is as follows: void Program::use() const { glUseProgram(_ID); }     where ID is a GLuint representing the shader compiled and linked.   This is how the setUniform() works: GLint Program::uniform(const GLchar* uniformName) const { if(!uniformName) throw std::runtime_error("uniformName is NULL"); GLint uniform = glGetUniformLocation(_ID, uniformName); if(uniform == -1) throw std::runtime_error(std::string("Program uniform not found: ") + uniformName); return uniform; } void Program::setUniform(const GLchar* uniformName, GLint v0) { assert(isInUse()); glUniform1i(uniform(uniformName), v0); } void Program::setUniform(const GLchar* uniformName, const glm::mat4& m) { assert(isInUse()); glUniformMatrix4fv(uniform(uniformName), 1, GL_FALSE, glm::value_ptr(m)); } void Program::setUniform(const GLchar* uniformName, const glm::vec3& v) { assert(isInUse()); glUniform3fv(uniform(uniformName), 1, glm::value_ptr(v)); } void Program::setUniform(const GLchar* uniformName, GLfloat v0) { assert(isInUse()); glUniform1f(uniform(uniformName), v0); }   and these are the other parts used in the method:   bool Program::isInUse() const { GLint currentProgram = 0; glGetIntegerv(GL_CURRENT_PROGRAM, &currentProgram); return (currentProgram == (GLint)_ID); } void Program::stopUsing() const { assert(isInUse()); glUseProgram(0); }   So yes, for every model, every frame i set and unset the program to be used and set the uniform variables. If this is the problem, where it's better to set the uniforms?