coronalabs / corona

Solar2D Game Engine main repository (ex Corona SDK)
https://solar2d.com/
MIT License
2.53k stars 273 forks source link

Fix the motorSpeed of the Wheel Joint always being set to a value of 1 #701

Closed kan6868 closed 3 months ago

kan6868 commented 6 months ago

Issue: https://github.com/coronalabs/corona/assets/70838508/8b2cc0b4-dc26-49f5-b014-4bd296f5c313

Fixed: https://github.com/coronalabs/corona/assets/70838508/e76b467e-bd65-404b-a840-6dcfa0e116b1

Code - Test:

local physics = require("physics")
physics.start()
physics.setContinuous(true)

local textWheel = display.newText("Wheel Joint", 100, 50, nil, 20)

local textPivot = display.newText("Pivot Joint", 300, 50, nil, 20)

local wheel1 = display.newRect(100, 220, 30, 30)
physics.addBody(wheel1, "dynamic", { bounce = 0, density = 1 })

local body = display.newRect(100, 150, 30, 30)
physics.addBody(body, "static", { bounce = 0, density = 1 })
body:setFillColor(1, 0, 1)

local ground = display.newRect(display.contentCenterX, display.actualContentHeight, display.actualContentWidth, 100)

physics.addBody(ground, "static", { bounce = 0 })

local wheel2 = display.newRect(300, 220, 30, 30)
physics.addBody(wheel2, "dynamic", { bounce = 0, density = 1 })

local body2 = display.newRect(300, 150, 30, 30)
physics.addBody(body2, "static", { bounce = 0, density = 1 })
body2:setFillColor(1, 0, 1)

local joint = physics.newJoint("wheel", body, wheel1, wheel1.x, wheel1.y, 0, 10)
joint.springFrequency = 15
joint.springDampingRatio = 0.5

joint.isMotorEnabled = true

joint.motorSpeed = 1000
joint.maxMotorTorque = 1000000

local joint2 = physics.newJoint("pivot", body2, wheel2, wheel2.x, wheel2.y)

joint2.isMotorEnabled = true
joint2.motorSpeed = 1000

joint2.maxMotorTorque = 1000000
kan6868 commented 6 months ago

I had some confusion about runtime.GetFPS. It's similar to 1 / (physics.GetTimeStep() * physics.GetTimeScale()). So I reverted the commit 0c869d3.

zero-meta commented 6 months ago

I think motorSpeed for Wheel Joint should be radians per seconds, the same as the Revolute Joint.

if ( lua_isnumber( L, 3 ) )
{
    Rtt_Real valueRadians = Rtt_RealDegreesToRadians( luaL_toreal( L, 3 ) );
    joint->SetMotorSpeed( Rtt_RealToFloat( valueRadians ) );
}
davidearle commented 6 months ago

Nice job on the fix! I had a PR (#641) suggesting a solution for the axis issue. Glad to see progress on this.

kan6868 commented 6 months ago

OK, I'll do some tests on it. And push a new commit.

kan6868 commented 6 months ago

Nice job on the fix! I had a PR (#641) suggesting a solution for the axis issue. Glad to see progress on this.

Your solution about the axis is correct when normalizing values to 1 or -1.

I'm also encountering an issue with the axis of the wheel joint; it seems more like a piston force along the axis.

However, this might necessitate changes to the previous project's attributes: springDampingRatio and springFrequency.

We need some opinions from @Shchvova , and it should be in a new pull request.

kan6868 commented 4 months ago

Nice job on the fix! I had a PR (#641) suggesting a solution for the axis issue. Glad to see progress on this.

Your solution about the axis is correct when normalizing values to 1 or -1.

I'm also encountering an issue with the axis of the wheel joint; it seems more like a piston force along the axis.

However, this might necessitate changes to the previous project's attributes: springDampingRatio and springFrequency.

We need some opinions from @Shchvova , and it should be in a new pull request.

I have added a parameter isNormalizeAxis at the end of the method for creating a wheel joint. This allows normalizing the axis range from -1 to 1 without affecting other projects.

local wheelJoint = physics.newJoint( "wheel", bodyA, bodyB, anchor_x, anchor_y, axisX, axisY, [isNormalizeAxis])