Physics gears game

Hi all,
I’m new to Corona SDK.
I’m making a game like Geared (http://appstorehq-production.s3.amazonaws.com/geared-iphone-66020.320x480.1255052212.88927.jpg) and I have some problems.

Create, destroy and drag gear is fine, the problem is to transmit the force to the other gears when “collide” and engage the gear teeth as happens in “Geared”.
It lags me gear movement is moving and should be maintained and convey the same rotational force to the gear you are in touch.

Can you help?

thank you very much

Sorry For my English

I would guess that Geared doesn’t actually use a physics engine to simulate the movement of the gears.  Each gear would be a pretty complex physics body in order to model all of the gear teeth.  If the gears are all circular, I don’t think it’s necessary to use the physics engine.  Instead, you could detect when the gears are interlocking yourself (based on the distance between the gear centers and their radii), and you could write your own code to cause a gear to rotate when it is interlocked with another gear that’s rotating.

  • Andrew

Thanks Andrew,

I found this

physics.newJoint( "gear", object1, object2, joint1, joint2 ) in Corona Docs

but i can’t find an example and i don’t understand the function.

Thanks

Ah that’s a good point, I’d forgotten that Box2D has a concept of a gear joint, which is meant to do all of the stuff a gear would without the messiness of modeling the individual teeth.  That’s definitely a good option versus my first suggestion of not using the physics engine at all.

You might want to read the Box2D documentation about gear joints: http://www.box2d.org/manual.html.  The diagram is helpful in understanding how it works.

Basically, a gear joint creates a behind-the-scenes “linkage” between two joints.

In your case, you’d set up your gears as circles with a pivot joint attaching them to the background.  Then, when two circles intersect, you’d create a gear joint between them.  Presumably, there’s also an API (appears to be undocumented) to set the gear ratio between them (which you would calculate based on the size of your circles).

  • Andrew

Sorry i don’t understand the mhetod and the mode of doing this :frowning:

Hmm, I think gear joints may be broken or not actually implemented, as this simple test case generates a runtime error.

[lua]

local physics = require( “physics” )

physics.start()

physics.setDrawMode(“hybrid”)

local gear1 = display.newRect(100,100, 100,100)

local anchor1 = display.newRect(gear1.x, gear1.y, 1,1)

gear1:setFillColor(255,0,0)

local gear2 = display.newRect(300,100, 100,100)

local anchor2 = display.newRect(gear2.x, gear2.y, 1,1)

gear2:setFillColor(0,255,0)

physics.addBody(gear1, “dynamic”)

physics.addBody(gear2, “dynamic”)

physics.addBody(anchor1, “static”)

physics.addBody(anchor2, “static”)

local joint1 = physics.newJoint(“pivot”, gear1, anchor1, gear1.x, gear1.y)

local joint2 = physics.newJoint(“pivot”, gear2, anchor2, gear2.x, gear2.y)

local gearJoint = physics.newJoint(“gear”, gear1, gear2, joint1, joint2)    – This line generates an error, indicating that the second argument is supposed to be a joint (contrary to the API).  Changing the order of the arguments generates another error

[/lua]

I have the same error.

Can you tell how to implement your first idea ?

Thanks you very much!

I’m going crazy, not how to implement this type of gameplay.
Someone can help me?

Hi @sephirot36,

I don’t believe the “gear” joint is fully implemented yet. I’ll make note of that in the documentation.

But for your scenario, I think @AukStudios is correct: you should try to find a way to “interlock” the gears and then apply normal rotation to the new gear… not expect that a complex rendered gear will correctly “drive” the next gear.

Probably the most tricky part will be figuring out the angle of the new gear to make it appear that it’s interlocking with the gear teeth. You’ll need to know the number of teeth on the original gear, the rotation (angle) of that gear, and then move the new gear into an angle where the teeth appear to be interlocked… and then start rotating the new gear.

You can try to make these into actual gears, and create the bodies from multiple sub-bodies. In your image of “Geared”, each body would probably be composed of about 12-15 parts. Then, I would suggest that you make these bodies very light (in density), and also remove all friction and bounce.

Hope this helps,

Brent

Hi all!

Now i have this.

https://docs.google.com/file/d/0B-88Zk7c3DwlU2d4ZFA1ZW1pczQ/edit?usp=sharing

This is my current code, if you can see it.
I removed the gravity, if You place a gear side by side the two will move.
What I like is that all were moving with the same angular velocity, because if I put a small gear can not move to a big one.

thank you all.

I would guess that Geared doesn’t actually use a physics engine to simulate the movement of the gears.  Each gear would be a pretty complex physics body in order to model all of the gear teeth.  If the gears are all circular, I don’t think it’s necessary to use the physics engine.  Instead, you could detect when the gears are interlocking yourself (based on the distance between the gear centers and their radii), and you could write your own code to cause a gear to rotate when it is interlocked with another gear that’s rotating.

  • Andrew

Thanks Andrew,

I found this

physics.newJoint( "gear", object1, object2, joint1, joint2 ) in Corona Docs

but i can’t find an example and i don’t understand the function.

Thanks

Ah that’s a good point, I’d forgotten that Box2D has a concept of a gear joint, which is meant to do all of the stuff a gear would without the messiness of modeling the individual teeth.  That’s definitely a good option versus my first suggestion of not using the physics engine at all.

You might want to read the Box2D documentation about gear joints: http://www.box2d.org/manual.html.  The diagram is helpful in understanding how it works.

Basically, a gear joint creates a behind-the-scenes “linkage” between two joints.

In your case, you’d set up your gears as circles with a pivot joint attaching them to the background.  Then, when two circles intersect, you’d create a gear joint between them.  Presumably, there’s also an API (appears to be undocumented) to set the gear ratio between them (which you would calculate based on the size of your circles).

  • Andrew

Sorry i don’t understand the mhetod and the mode of doing this :frowning:

Hmm, I think gear joints may be broken or not actually implemented, as this simple test case generates a runtime error.

[lua]

local physics = require( “physics” )

physics.start()

physics.setDrawMode(“hybrid”)

local gear1 = display.newRect(100,100, 100,100)

local anchor1 = display.newRect(gear1.x, gear1.y, 1,1)

gear1:setFillColor(255,0,0)

local gear2 = display.newRect(300,100, 100,100)

local anchor2 = display.newRect(gear2.x, gear2.y, 1,1)

gear2:setFillColor(0,255,0)

physics.addBody(gear1, “dynamic”)

physics.addBody(gear2, “dynamic”)

physics.addBody(anchor1, “static”)

physics.addBody(anchor2, “static”)

local joint1 = physics.newJoint(“pivot”, gear1, anchor1, gear1.x, gear1.y)

local joint2 = physics.newJoint(“pivot”, gear2, anchor2, gear2.x, gear2.y)

local gearJoint = physics.newJoint(“gear”, gear1, gear2, joint1, joint2)    – This line generates an error, indicating that the second argument is supposed to be a joint (contrary to the API).  Changing the order of the arguments generates another error

[/lua]

I have the same error.

Can you tell how to implement your first idea ?

Thanks you very much!

I’m going crazy, not how to implement this type of gameplay.
Someone can help me?

Hi @sephirot36,

I don’t believe the “gear” joint is fully implemented yet. I’ll make note of that in the documentation.

But for your scenario, I think @AukStudios is correct: you should try to find a way to “interlock” the gears and then apply normal rotation to the new gear… not expect that a complex rendered gear will correctly “drive” the next gear.

Probably the most tricky part will be figuring out the angle of the new gear to make it appear that it’s interlocking with the gear teeth. You’ll need to know the number of teeth on the original gear, the rotation (angle) of that gear, and then move the new gear into an angle where the teeth appear to be interlocked… and then start rotating the new gear.

You can try to make these into actual gears, and create the bodies from multiple sub-bodies. In your image of “Geared”, each body would probably be composed of about 12-15 parts. Then, I would suggest that you make these bodies very light (in density), and also remove all friction and bounce.

Hope this helps,

Brent

Hi all!

Now i have this.

https://docs.google.com/file/d/0B-88Zk7c3DwlU2d4ZFA1ZW1pczQ/edit?usp=sharing

This is my current code, if you can see it.
I removed the gravity, if You place a gear side by side the two will move.
What I like is that all were moving with the same angular velocity, because if I put a small gear can not move to a big one.

thank you all.

Developer of Geared here (Bryan Mitchell).  I’m actually working on my next Geared game using the Corona SDK.  aukStudios is correct in saying that I didn’t use a physics engine to handle the gears in either Geared or Geared 2.  It was written in Objective C using OpenGL, and I handled all of the touches, collisions, teeth alignment, and gravity on my own.  

The concept isn’t particularly complex when you really start thinking about what gears do.   The biggest hint I could give would be that you should imagine gears as circles with and “infinite” amount of friction.  My game detects collisions by taking the distance between the center-points and comparing that to the sum of the radii of the two gears.

If you work with that approach for the physics, and align the sprites after all of the physics is said and done you’ll have yourself working “gear physics”.  Tooth alignment is an entirely separate part of my game, and comes after dealing with how the gears spin each other and land on each other.  

For Geared 3 (If that’s what it’s going to be called), I AM actually using the physics engine in Corona SDK.  However, I’m still working with them as circles, and still writing a lot of customized code to assist the physics engine in dealing with these “unique circles.”

Here is a little preview of the new physics.  I’m using the old artwork while I code it to save time while the artist draws new art.  This is actually only about 300 lines of code in Corona SDK.

https://www.youtube.com/watch?v=MZH4cMUpdkI

edit:

I see the project files you posted contain an interface very similar to my game.  I hope that as your game progresses and takes shape that your game becomes something other than a clone of mine.  Admittedly, I wouldn’t be that happy about that.  It would be great to see a new and unique approach to gear based games available on the app store, and I wish you the best of luck.