can u fix my jump (he is jumping only up, but need to forward (like in gta san andreas…
- User
can u fix my jump (he is jumping only up, but need to forward (like in gta san andreas or vice city or gta 3)): void Jump() { if (!m_isJumping && !m_isFalling) { m_isJumping = true; m_jumpVelocity = sqrt(2 * m_gravity * m_jumpHeight); } } inline void Player::Update(float deltaTime) { if (m_ProcessMoveToPoint) { if (m_agentIndex == -1 || !m_crowd) return; dtCrowdAgent* agent = m_crowd->getEditableAgent(m_agentIndex); if (!agent) return; // Simulate falling if the agent is not on the navmesh glm::vec3 currentPos(agent->npos[0], agent->npos[1], agent->npos[2]); // Check if the agent is above the ground const dtQueryFilter* filter = m_crowd->getFilter(agent->params.queryFilterType); dtPolyRef nearestRef; float nearestPos[3]; dtStatus status = m_crowd->getNavMeshQuery()->findNearestPoly(¤tPos[0], m_crowd->getQueryExtents(), filter, &nearestRef, nearestPos); // } glm::vec3 currentPosition = GetPosition(); glm::vec3 newPosition = currentPosition; bool positionChanged = false; // Apply gravity if (m_isJumping) { m_jumpVelocity -= m_gravity * deltaTime; newPosition.y += m_jumpVelocity * deltaTime; positionChanged = true; if (m_jumpVelocity <= 0) { m_isJumping = false; m_isFalling = true; m_fallVelocity = 0; } } else if (m_isFalling) { m_fallVelocity = std::min(m_fallVelocity + m_gravity * deltaTime, 15.0f); newPosition.y -= m_fallVelocity * deltaTime; positionChanged = true; } // Check for ground collision float rayLength = 3.0f; // Adjust this to match your world's height glm::vec3 rayStart = currentPosition + glm::vec3(0, 0.1f, 0); glm::vec3 rayEnd = rayStart - glm::vec3(0, rayLength, 0); auto hitResult = m_world->Raycast(rayStart, rayEnd); if (hitResult) { glm::vec3 hitPosition = *hitResult; float groundY = hitPosition.y; if (newPosition.y <= groundY + 0.1f) { newPosition.y = groundY + 0.1f; // Keep slightly above ground if (m_isJumping || m_isFalling) { m_isJumping = false; m_isFalling = false; m_fallVelocity = 0; positionChanged = true; } } else if (!m_isFalling && !m_isJumping) { m_isFalling = true; positionChanged = true; } } else if (!m_isFalling && !m_isJumping) { m_isFalling = true; positionChanged = true; } // Apply the new position if (positionChanged) { SetPosition(newPosition); } }, also helper function: inline void Player::SetRotation(const glm::quat& rotation) { m_rotation = rotation; // Update look-at based on new rotation glm::vec3 currentPos = GetPosition(); m_lookAt = currentPos + glm::rotate(m_rotation, glm::vec3(0.0f, 0.0f, -1.0f)); } inline glm::quat Player::GetRotation() const { return m_rotation; } inline void Player::SetPosition(const glm::vec3& newPosition) { m_position = newPosition; } inline glm::vec3 Player::GetPosition() const { return m_position; } inline void Player::SetLookAt(const glm::vec3& target) { m_lookAt = target; glm::vec3 currentPos = GetPosition(); // Calculate direction on XZ plane glm::vec3 direction = target - currentPos; direction.y = 0.0f; // Ignore Y component direction = glm::normalize(direction); // Calculate the angle between the forward vector and the direction glm::vec3 forward(0.0f, 0.0f, -1.0f); // Assuming forward is -Z float angle = std::atan2(direction.x, direction.z); // Create a quaternion for rotation around Y-axis m_rotation = glm::angleAxis(angle, glm::vec3(0.0f, 1.0f, 0.0f)); } inline glm::vec3 Player::GetLookAt() const { return m_lookAt; }, jump distance is 4.0f and need to check collision forward to player. Here how i render it (player collision box, ,maybe it's helped: glm::vec3 dimensions(1.0f, 1.8f, 0.5f); // length (front-to-back), height, width float vertices[] = { // Front face (facing positive X) dimensions.x / 2, 0.0f, -dimensions.z / 2, // bottom-right dimensions.x / 2, 0.0f, dimensions.z / 2, // bottom-left dimensions.x / 2, dimensions.y, dimensions.z / 2, // top-left dimensions.x / 2, dimensions.y, -dimensions.z / 2, // top-right // Back face (facing negative X) -dimensions.x / 2, 0.0f, -dimensions.z / 2, // bottom-left -dimensions.x / 2, 0.0f, dimensions.z / 2, // bottom-right -dimensions.x / 2, dimensions.y, dimensions.z / 2, // top-right -dimensions.x / 2, dimensions.y, -dimensions.z / 2, // top-left }; unsigned int indices[] = { // Front face 0, 1, 2, 2, 3, 0, // Right face (facing positive Z) 1, 5, 6, 6, 2, 1, // Back face 5, 4, 7, 7, 6, 5, // Left face (facing negative Z) 4, 0, 3, 3, 7, 4, // Top face 3, 2, 6, 6, 7, 3, // Bottom face 4, 5, 1, 1, 0, 4 };)
- Assistant