Physics wheels turning each other

I want to achieve the effect of wheels turning each other like gears.
I have created some wheels; circular physics bodies that have pivot joints to a static body. The wheels are positioned so they are touching. I apply isMotorEnabled true on one of them with a motorSpeed and it rotates as expected…
However I would expect the wheel that touches the motorised wheel to turn in the opposite direction but it doesn’t. Now the bizarre thing is that if I create another non-jointed dynamic physics object and position it where the two wheels touch, it will fall away and leave the wheels turning and interacting as I would expect them to.
Can anyone shed any light on this for me? I want to achieve this affect without having to place obsolete bodies on every point a wheel contacts another.

local physics = require("physics")  
physics.start()  
  
local \_W = display.contentWidth  
local \_H = display.contentHeight  
  
--\> Board  
local board = display.newRect(0, 0, \_W, \_H)  
board:setFillColor(140, 140, 140)  
physics.addBody( board, "static", { friction=0.5, bounce=0.3 } )  
  
--\> Wheel 1  
local wheel1 = display.newCircle( \_W/2, \_H/2, 50 )  
wheel1:setFillColor(128,128,128)  
physics.addBody( wheel1, { density=5, friction=5, bounce=0, radius=5 } )  
wheel1Joint = physics.newJoint( "pivot", board, wheel1, wheel1.x, wheel1.y, 0, 0 )  
wheel1Joint.isMotorEnabled = true  
wheel1Joint.motorSpeed = 100  
wheel1Joint.maxMotorTorque = 100000  
  
--\> Wheel 2  
local wheel2 = display.newCircle( \_W/2, \_H/2+75, 25 )  
wheel2:setFillColor(128,128,128)  
physics.addBody( wheel2, { density=5, friction=5, bounce=0, radius=25 } )  
wheel2Joint = physics.newJoint( "pivot", board, wheel2, wheel2.x, wheel2.y, 0, 0 )  
  
--\> Box --\> Note: without the box the wheels don't interact, very strange  
local box = display.newRect(\_W/2-20, \_H/2+50, 40, 40)  
box:setFillColor(140, 140, 140)  
physics.addBody( box, { density=5, friction=5, bounce=0} )  
  

Please help.
[import]uid: 62617 topic_id: 16627 reply_id: 316627[/import]

I very much want to get turning circular bodies interact with each other in this way. Please does anyone have a solution to this??? [import]uid: 7059 topic_id: 16627 reply_id: 62370[/import]

Ok I figured this out. You will see in the example I provided there are some ODD things with joints!

First, these 2 must match (don’t ask why I don’t understand it myself!

  
--Notice the -85, on the refpoint2?  
  
local refpoint2 = display.newCircle (\_W/2, \_H/2 - 85, 5)  
physics.addBody( refpoint2, "static")  
refpoint2:setFillColor (255,0,0)  
refpoint2.alpha = .1  
ui:insert(refpoint2)  
--notice the -85 on the myjoint2?   
  
local myJoint2 = physics.newJoint( "pivot", refpoint2, gears2, \_W/2, \_H/2 -85)  
  

Basically, when you create a joint, you have to do some wierd offsets to get things to line up. Experiment and you will see. In your example, I don’t know what was going on so I started from scrath. You could just do circles with same radius and get same results below. However, you have to be in debug mode to see the physics working. That’s why I included a gear :slight_smile:
Have fun.
First you will need the gear.png from this project

http://developer.anscamobile.com/sample-code/getting-started

I will tell you that your circle WILL work with enough friction (you know like 9999999 on both)

Here is a faked out way of doing it. Basically, applying motors going in opposite directions.

Now, if you really want them to act correctly, you will need physicsEd ($20) physicseditor.de to get the gears to become physics bodies.

I have my example with the gears, and I can touch them and pull on ropes which pull gears, which rotates them etc without motors.

So here you go, do with you want with the code. Let me know if you need other stuff.

Now, if you run it in “debug” you will see that the gears are really nothing more than circles. I overlapped them so if you were to apply mass amounts of friction and add some touch events for rotating they would work :slight_smile:
ng
<br>display.setStatusBar( display.HiddenStatusBar )<br>local physics = require("physics")<br>physics.start( true )<br>physics.setGravity(0,14)<br>physics.setPositionIterations(64)<br>physics.setDrawMode( "normal" )<br>--shorcut variables to call the content width and height<br>local _W = display.contentWidth<br>local _H = display.contentHeight <br><br>--Group to separate groups and make things like removing objects easier<br>local game = display.newGroup()<br>game.x = 0<br>game.y = 0<br><br>local ui = display.newGroup()<br>ui.x = 0<br>ui.y = 0<br><br>--make sure the Y on the ref point matches the joint Y <br>local refpoint = display.newCircle (_W/2,_H/2 +200, 10)<br>physics.addBody( refpoint, "static")<br>refpoint:setFillColor (255,0,0)<br>refpoint.alpha = .1<br>ui:insert(refpoint)<br><br>--local variable - the image used to generate physics data from PhysicsEd<br>--In other words, the image name used should be the same as the local!!<br>local gears = display.newImage("gear.png")<br>gears.x = refpoint.x<br>gears.y = refpoint.y <br>gears.rotation = 8<br>physics.addBody( gears, "dynamic", { density=5.0, friction=99999999, bounce=0.1, radius=150} ) <br>gears.isBullet = true<br>gears:setReferencePoint(display.CenterReferencePoint)<br>game:insert(gears)<br>local refpoint2 = display.newCircle (_W/2, _H/2 - 85, 5)<br>physics.addBody( refpoint2, "static")<br>refpoint2:setFillColor (255,0,0)<br>refpoint2.alpha = .1<br>ui:insert(refpoint2)<br><br>local gears2 = display.newImage("gear.png")<br>gears2.x = refpoint2.x <br>gears2.y = refpoint2.y <br>physics.addBody( gears2, "dynamic", { density=5.0, friction= 99999999, bounce=0.1, radius=150} )<br>gears2.isBullet = true<br>gears2:setReferencePoint(display.CenterReferencePoint)<br>game:insert(gears2)<br><br>local myJoint = physics.newJoint( "pivot", refpoint, gears, _W/2, _H/2 +200)<br>local myJoint2 = physics.newJoint( "pivot", refpoint2, gears2, _W/2, _H/2 -85)<br><br>myJoint.isMotorEnabled = true<br>myJoint.motorSpeed = 300<br>myJoint.maxMotorTorque = 1000<br><br>myJoint2.isMotorEnabled = true<br>myJoint2.motorSpeed = -300<br>myJoint2.maxMotorTorque = 1000<br><br><br> [import]uid: 61600 topic_id: 16627 reply_id: 62428[/import]

Thanks for your reply and example…
The thing is I need the wheels to interact with each other without having to apply motors to each one. The idea is that one ‘master’ wheel would be draggable or have a motor. I’ll have another play with it later but in your example I commented out the joint2 motor lines of code and despite the insanely high friction settings, the other wheel did nothing.

See in my example that there is only one motor and yet both the wheels move, so long as the dynamic unjointed box is placed where the wheels collide. Hmmm… [import]uid: 62617 topic_id: 16627 reply_id: 62662[/import]

Thanks for your reply and example…
The thing is I need the wheels to interact with each other without having to apply motors to each one. The idea is that one ‘master’ wheel would be draggable or have a motor. I’ll have another play with it later but in your example I commented out the joint2 motor lines of code and despite the insanely high friction settings, the other wheel did nothing.

See in my example that there is only one motor and yet both the wheels move, so long as the dynamic unjointed box is placed where the wheels collide. Hmmm… [import]uid: 62617 topic_id: 16627 reply_id: 62663[/import]

@juliusbangert
Ok let me as you something. Are you after JUST round objects? What will the player see?

I have some ideas for you, but you will need PHYSICSED for this:
Ok here we go:

If you are after displaying CIRCLES on screen and want them to turn and react to one another then you can do this:

Grab a gear.png from the sample I pointed to earlier (or make your own whatever).

Go into physicsEd and auto trace the shape, publish to corona and save it as whatever you want, ie “gears.lua” (whatever you want really)

Then you will require that lua file (physicsED explains it in the output file you named)
–Then make a display.newCircle and place that on screen with a radius of 1 or something really small

–Once you have that squared away - then create your local display object for the gear.png using the naming convention in the physics editor.

–Then join those together (like you already have in the wheel example).

–Then you place the circle you want the player to see on top of all of that and weld it to gear

Make sure you test after each display object insert.

–Once you see what you want on screen, add a motor to it and test the gear and make sure your circle that’s placed on top is moving. Make sure you do “debug” for your draw so you can see the physics bodies, or you will be flying blind!

–once that is working, then add another gear. The great thing is once you traced it out, you can reuse the physicsEd code and place another gear on screen, do the same process here - but don’t add the motor.

Then you will have to set the beginning rotation by -10 degrees, and (mess with it to get the “gears” to line up) make sure the “gears” overlap so the teeth are connected.
so that would be

gear.rotation = 10

So at this point you should have in order

gear 1

-The refpoint (where things will be joined)

-The gear (with an offset rotation of + or - X degrees)

-The circle

gear 2

-The refpoint (where things will be joined)

-The gear (no rotation necessary here, just do it on one)

-The circle
Once you have all that, test the motor and also make sure your position iterations are at least 32 (see my sample code, those make collision more accurate, also the isBullet helps too!_.

Once things are working, then change the alpha on the gears to 0

so something like gear.alpha = 0 under each gear display object.
Then change the debug draw back to normal and what you should see is the circles reacting to the ONE motor on of those circles, and the other spinning the correct direction. You should NOT see the gear.
That’s exactly what I did, and it should work for you.
I’m sure you will have questions, so let me know I’ll keep an eye out :slight_smile:

ng

[import]uid: 61600 topic_id: 16627 reply_id: 62693[/import]

Thanks for your help nicholasclayg. I know the physics editor and have used it many times before but for this I wanted to created the wheels dynamically with different sizes. I also liked the simplicity and light weightness of just using circles… but alas it is a glitch in the physics engine.
I ended up writing a nice little function that you feed a “number of teeth” variable and it builds a gear body using some nice maths and iterations. [import]uid: 62617 topic_id: 16627 reply_id: 65068[/import]

@juliusbangert

The easy way is to make 2 different size circles, and then get a gear.png and resize it -then change the alpha to 0, and weld the circle on top of the gear. Then you will see the circle, but not the gear and still get the effect. You won’t need a function to feed teeth, the teeth will just be there “invisible”

There are many little hacks you can do to achieve the same thing - I always think less is more is the way to go.

It does show the simplicity - and the limitations of the engine. Corona SDK does allow some pretty complex stuff, but I’m starting to push the limits and finding a few walls :slight_smile:
ng [import]uid: 61600 topic_id: 16627 reply_id: 65122[/import]