[PHYSICS] Implement cool elastic rope

Hi!

I’m trying to implement elastic rope as in this game: http://www.physicsgames.net/game/Gen.html to be able to control many circles to make it get round difficulties. Game uses Box2D too.

I’ve wrote this code:
[lua]local physics = require( “physics” )
physics.start()
physics.setGravity(0, 0)
physics.setScale( 30 )
–physics.setDrawMode(“debug”)

rootJoint = display.newImageRect(“bacteria.png”, 20, 20)
rootJoint.x = 160
rootJoint.y = 600

physics.addBody( rootJoint, “kinematic” )

colony = display.newImageRect(“generator_colony.png”, 80, 80)
colony.x = 160
colony.y = 400

physics.addBody( colony, “static”, { density=3.0, friction = 0, bounce=0, radius = 40 } )

bacterias = {}

for i = 1, 40 do
bacterias[i] = display.newImageRect(“bacteria.png”, 20, 20)
bacterias[i].x = 160 + math.random(-30, 30)
bacterias[i].y = 750 + math.random(-30, 30)
bacterias[i].isFixedRotation = true

physics.addBody( bacterias[i], { density=1.0, friction = 0, bounce=0, radius = 10 } )
local dist = physics.newJoint( “distance”, bacterias[i], rootJoint, bacterias[i].x, bacterias[i].y, rootJoint.x, rootJoint.y )
end

transition.to(rootJoint, {time = 5000, x = 160, y = 0})[/lua]

I use rootJoint to control all others. I translate it thought big sircle so small can get round it.
But there is a problem: some circles just stuck at big circle and don’t want to get round it or do this too slowly. Friction is 0.
How can I do same effect as in that game?
Help please. [import]uid: 94357 topic_id: 16524 reply_id: 316524[/import]

@Art,
Physics is the easiest way to do things. However if you grapple with Math and Vectors, it is much easier than using built in physics.

Here’s an example video of what I was experimenting with http://iphone.oz-apps.com/elasticity-example-in-coronasdk

this is using absolutely *no* built-in physics

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16524 reply_id: 61749[/import]

Hi JayantV!

Thanks for the answer. Your demo is really cool, but I’m not sure if that circle can bound with other objects and get round difficulties. I used physics only for this reason. [import]uid: 94357 topic_id: 16524 reply_id: 61809[/import]

Hi Art,
Thanks that you like it.

The reason that we all use Corona is to overcome the learning curve and doing all the difficult math and calculations ourselves. However the beauty of Corona is that if you know how to, you can kind of push it to the limits from within the sandbox.

Now, if you can get your hear around vector math, you can definitely make the game like that, from what I see in English terms

If the circles are within a distance from the main circle, then the lines are drawn to show attraction and the balls get pulled towards the main circle. Getting this data is easy, it is calculating the distance between the centre points of two circles

Attraction is moving the circle’s vector towards the main circle With the velocity of the main circle’s movement.

I can go on and describe this to you, but the question is, do you want to delve into vector math and do this yourself?

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16524 reply_id: 61834[/import]

Hi JayantV,
Thanks you describe it to me. Using vectors is a good idea, but I want to detail explain the situation I have. The main mission I want to do is the ability to control group of little circles to be able to move them in any part of the map, while they are collide with each other ( because I don’t want them to cover each other ) and with other objects if needed ( you can run my code to see it approximately ). The first idea I had is to use translate.to for each little circle, but it was a bad idea because I can’t do collision and don’t know how to calculate their fly paths to do them not to collide with other objects on the map. So I chose to do them physics objects and control them by elastic rope. I created root invisible circle that can fly anywhere without collision and bind circles to it with distance joint. Now I can control them all just do transition.to on the root circle. Little circles can fly out of other objects by colliding with their border. But I have problem I described above.
With the elastic rope made of vector math I can control all of the circles, but how they will collide with each other and how they will get round other objects? May be I don’t understand what you mean.

Thanks and sorry for my poor English. [import]uid: 94357 topic_id: 16524 reply_id: 61840[/import]

Art, if you read up on vector math, all your questions will be answered. I understand where you are coming from and your questions are all valid. I cannot go into how wonderful they are and how easy they are, as they have a magnitude and direction…

Have you worked with vectors in Maths before? If you haven’t then this will make no sense to you whatsoever till you read about them.
Here’s a link on Wikipedia http://en.wikipedia.org/wiki/Vector_space

There are some wonderful books on Vector Math and Physics, you will be surprised that collisions are much better and precise, after all all physics engines Box2D, Chipmunk, all 3D engines use vectors too.

so if you are new to Geometry and Math as many developers are, or were a bit shy of that while in school, you need to brush up on a lot…

However as I said, I will repeat, if you think that you can manage it better with the in-build engine, then that should be good enough.

I will emphasize the fact that *ONLY IF* you are very good with Math(Geometry and Algebra) try to go the vector way. You can recreate the same game, probably that’s how they might have made it too, using vectors in Flash and ActionScript.

cheers,

?:slight_smile:
[import]uid: 3826 topic_id: 16524 reply_id: 61842[/import]

I think I’m good with Math as I studied in technical school and university. But I never program it. But if you say that I can make the same with it then it’s probably the best solution. Thanks.

Edit: By the way: I decompiled that game and see that it uses Distance and Pivot joints in Box2D. [import]uid: 94357 topic_id: 16524 reply_id: 61845[/import]