How would you go about creating physics objects like windmills and pendulums? These objects are fixed on the screen, but should spin or swing when hit by another object. [import]uid: 52127 topic_id: 9631 reply_id: 309631[/import]
View the documentation on physics joints.
http://developer.anscamobile.com/content/game-edition-physics-joints [import]uid: 31262 topic_id: 9631 reply_id: 35100[/import]
Yes, I’ve seen the doc, but after reading through all the different joints it’s not clear to me which one(s) to use to create these objects. [import]uid: 52127 topic_id: 9631 reply_id: 35116[/import]
function new(x,y)
--x and y are the global coordinates of the pendulum
local obj = display.newGroup();
local pivot = display.newCircle( 0, 0, 5 );
obj:insert(pivot);
physics.addBody ( pivot, "static", {density = 1, friction = 1.0, bounce = 0.3} );
local arm = display.newImageRect("images/pendulumArm.png", 4, 41);
obj:insert(arm);
physics.addBody ( arm, "dynamic", {density = 1, friction = 1.0, bounce = 0.3} );
physics.newJoint( "pivot", arm, pivot, x, y );
return obj
end
This just results in the arm dropping across the screen in a circular arc. It doesn’t stay attached to the pivot point. [import]uid: 52127 topic_id: 9631 reply_id: 35134[/import]
I just gave your arm variable an x and y value and it worked fine. Note: I moved all objects out 50 pixels so I could see them better.
[code]
local obj = display.newGroup();
local pivot = display.newCircle( 50, 0, 5 );
obj:insert(pivot);
physics.addBody ( pivot, “static”, {density = 1, friction = 1.0, bounce = 0.3} );
–local arm = display.newRect(50,5,4, 41);
local arm = display.newImageRect(“pendulumArm.png” ,4, 41);
arm.x=50
arm.y=21
obj:insert(arm);
physics.addBody ( arm, “dynamic”, {density = 1, friction = 1.0, bounce = 0.3} );
physics.newJoint( “pivot”, pivot, arm, 50, 0 );
[/code] [import]uid: 31262 topic_id: 9631 reply_id: 35259[/import]
Yes, thanks, I’ve gotten it to work by changing some x and y values. Next question though - why do other physics bodies fly right through the pendulum? Both the pivot and the arm bodies are added to the system, yet other objects do not interact with them.
Also, how can you set the damping of the pendulum? Even if I set the friction of both the pivot and the arm to 0, it still dampens the swinging. I guess this is because of gravity… but pendulums aren’t supposed to stop swinging. [import]uid: 52127 topic_id: 9631 reply_id: 35270[/import]
I added this code to the drag platforms project and I was able to drag other objects into them and have them successfully interact. Maybe it’s an issue with your other objects?
About the damping:
pendulumjoint = physics.newJoint( "pivot", pivot, arm, 50, 0 );
pendulumjoint.dampingRatio = 0
Try that [import]uid: 31262 topic_id: 9631 reply_id: 35287[/import]
The objects that I fire at it fly through it, but then interact with other objects.
dampingRatio has no effect. The docs say “For use with Distance joints.” I guess you could figure out the period of the pendulum and apply a tiny force at certain times to keep it swinging at the same height. I wish there was an easier way though…
[import]uid: 52127 topic_id: 9631 reply_id: 35290[/import]
I tested the dampingRatio when I posted that and it made the pendulum swing for at leaast 5 minutes before I turned it off. It did work for me. It doesn’t swing at the original speed the entire time though. It seemed to slow down then keep swinging at a speed similar to a real pendulum. This is the only time I have ever used this setting so I don’t have any other experience with it. [import]uid: 31262 topic_id: 9631 reply_id: 35295[/import]
Here’s my interpretation of a wind mill which reacts to forces against it. Here, a four-armed windmill sits in the centre of the screen. Tapping on the screen creates balls which drop. The balls will land on the windmill if dropped above it.
A pivot joint motor is used to continuously, smoothly and slowly rotate the windmill, but the maxMotorTorque is set low enough to allow balls landing on the windmill to force it to rotate backwards. Once the balls fall off, it will eventually continue it’s rotation.
The key here was simply to play with the value of the maxMotorTorque property…
[EDIT] I’ve just extended the code to add a pendulum. This also uses the motor on a pivot joint, but using a timer the motorSpeed property is reversed when the rotation of the pendulum passes a threshold (in this case + or - 50 degrees)…
[lua]-- WINDMILL
local physics = require(“physics”)
physics.start()
physics.setDrawMode( “hybrid” ) – normal, hybrid, debug
local anchor = display.newCircle( 0,0,5 )
anchor.alpha = .1
physics.addBody( anchor, “static”, { friction=0, bounce=0, density=0 } )
local hub = display.newCircle( display.contentCenterX, display.contentCenterY, 40 )
physics.addBody( hub, “dynamic”, { friction=1, bounce=.1, density=3, radius=40 } )
hub.fixture = physics.newJoint( “pivot”, anchor, hub, hub.x, hub.y )
local tap = function( event )
local ball = display.newCircle( event.x, event.y, 20 )
physics.addBody( ball, “dynamic”, { friction=1, bounce=.1, density=1, radius=20 } )
function ball:timer( event )
if (ball.y > 1500) then
timer.cancel( ball.t )
ball.t = nil
ball:removeSelf()
end
end
ball.t = timer.performWithDelay( 10000, ball, 3 )
end
local addarm = function( hub, rot )
local arm = display.newRect( 0, 0, 20, 100 )
arm.x, arm.y = display.contentCenterX, display.contentCenterY - 80
physics.addBody( arm, “dynamic”, { friction=1, bounce=.1, density=1 } )
arm.connect = physics.newJoint( “weld”, hub, arm, hub.x, hub.y )
hub.rotation = hub.rotation + rot
end
for i=1, 4 do
addarm( hub, 90 )
end
hub.fixture.isMotorEnabled = true
hub.fixture.motorSpeed = 20
hub.fixture.maxMotorTorque = 40
Runtime:addEventListener( “tap”, tap )
– PENDULUM
local pend = display.newRect( 0, 0, 10, 100 )
pend.x, pend.y = 100, display.contentCenterY
physics.addBody( pend, “dynamic”, { friction=1, bounce=.1, density=3 } )
pend.axis = physics.newJoint( “pivot”, anchor, pend, pend.x, pend.y-pend.height/2 )
pend.axis.isMotorEnabled = true
pend.axis.motorSpeed = 20
pend.axis.maxMotorTorque = 40
function pend:timer( event )
if (pend.rotation > 50 or pend.rotation < -50) then
pend.axis.motorSpeed = 1 - pend.axis.motorSpeed
end
end
pend.pulse = timer.performWithDelay( 500, pend, 0 )[/lua]
matt [import]uid: 8271 topic_id: 9631 reply_id: 35360[/import]
Wow works great Matt. Glad this is now in the database as I will need something like this later on. [import]uid: 31262 topic_id: 9631 reply_id: 35384[/import]
Trying out your code…
For me the windmill rotates only a little, very slowly, and only for a few seconds, even if I change the motor speed. And the pendulum doesn’t move at all. What am I missing?
EDIT - it’s working now. Didn’t know that you had to comment out either the pendulum or windmill. Good stuff. My only note would be that the pendulum doesn’t have a completely natural “sway” like it normally would under normal physics. It should pick up speed as it swings down and slow down as it sways upward, like a weight attached to a string. [import]uid: 52127 topic_id: 9631 reply_id: 35419[/import]
The pendulum and windmill should work together - they do on my Win sim and work just fine. Perhaps copy the code again?
The only issue I see is that the pendulum gets caught on the windmill, but that’s not really a problem.
I know what you mean about the free swing nature, but I’m leaving that as an exercise. It’s not a big modification, tbh. [import]uid: 8271 topic_id: 9631 reply_id: 35423[/import]
Can you explain why the anchor is needed? Why can’t you just have a hub and arms for the windmill? [import]uid: 52127 topic_id: 9631 reply_id: 35425[/import]
For me the windmill rotates only a little, very slowly, and only for a few seconds, even if I change the motor speed. And the pendulum doesn’t move at all. What am I missing?
@ jn19:
it helps for me to change the simulator from iphone to iphone4 [import]uid: 42078 topic_id: 9631 reply_id: 35426[/import]
still struggling with this. What if I’m using an image of a windmill instead of a vector version?
local obj = display.newImageRect ( "images/windmill.png", 80, 80 );
physics.addBody( obj, "static", physicsData );
This displays the image on the screen, and objects bounce off of it. But how do I make it spin with a motor and torque?
obj.fixture.motorSpeed = 10
obj.fixture.maxMotorTorque = 40
fixture is undefined, but how do I define it? [import]uid: 52127 topic_id: 9631 reply_id: 35430[/import]
In my code fixture is not undefined. I would copy the code again, sounds like you’re missing a bit.
FYI, I was using the Samsung Galaxy Tab sim on Win XP Pro. [import]uid: 8271 topic_id: 9631 reply_id: 35431[/import]
Not using your code - I’m just using the code I posted. I’m wondering how to do it using an image. [import]uid: 52127 topic_id: 9631 reply_id: 35433[/import]
Look at line 14 of my code… [import]uid: 8271 topic_id: 9631 reply_id: 35436[/import]
Oh, and I use the anchor object because I’m trying to get into the habit of not using static objects. This is because I read in a recent post that ragdoll objects will hit a static object and break apart, but when they hit a dynamic object held in place with a weld to a static object, the maths works and the ragdoll does not fly apart.
Quick and dirty description, but that’s the essence. [import]uid: 8271 topic_id: 9631 reply_id: 35437[/import]