Sunday, February 25, 2024
HomeGame Developmentlibgdx - Box2D problem with setLimits on a RevoluteJoint

libgdx – Box2D problem with setLimits on a RevoluteJoint


I’ve a high down second view, a revolute be part of and a “wheel” connected to the joint. I am setting the bounds of the joint to comply with the angle route of the mouse step by step. I am additionally attempting to deal with a case the place the mouse strikes throughout the -PI,PI boundary (south) in order that the wheel does not must rotate all the way in which across the joint to the brand new route when the mouse crosses it.

The difficulty I am having is on the boundary, the joint begins bouncing and is not reaching the angle I am setting, even once I’m normalizing newAngle to be contained in the -PI,PI vary.

I made a small video showcasing the issue I am having. The underside-center wheel (and all the opposite wheels on the skin) have the difficulty, the central wheel does not have the swivel property so it’s working as meant rotating the good distance across the joint.

I am utilizing the next code:

// route is the angle of the mouse place

wheels.forEach {
    if (it.steering) {
        val desiredAngle = route.coerceIn(-it.maximumSteeringAngleRadians, it.maximumSteeringAngleRadians)

        var jointAngle = it.joint?.jointAngle ?: 0f

        if (it.swivel) {
            // Change jointAngle as to be inside 180 levels of the goal
            val angleDifference = desiredAngle - jointAngle
            if (abs(angleDifference) > MathUtils.PI) {
                jointAngle += signal(angleDifference) * MathUtils.PI2
            }
        }

        var newAngle = MathUtils.lerp(jointAngle, desiredAngle, it.steeringSpeedRadians)
        if (abs(newAngle) > MathUtils.PI) {
            // Normalize newAngle to be within the -PI to PI vary
            newAngle -= signal(newAngle) * MathUtils.PI2
        }

        it.joint?.setLimits(newAngle, newAngle)
    }
}

// ...

I do know there is a lerpAngle operate inside MathUtils however the problem does not appear to lie with the angle lerping. I’ve tried it for the sake of experimentation and the problems persists (as proven in this different video, the one distinction is that lerpAngle goes from 0 to PI * 2 so the boundary is north), additionally I do not all the time need the wheel to take the shortest “path” so I deal with the lerp myself.

I’ve made a repository to breed the difficulty in a managed and restricted setting. If in case you have concepts I am all ears.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments

Arnavsijapati on Planet of Lana – Beta Demo
Jai Kishor Upadhyay on Planet of Lana – Beta Demo