Yesterday I posted a question about simulating gravity and the reply seemed to be ‘use particle candy’. I haven’t shelled out for it yet, but I wanted to understand at least a rudimentary model for the physics of the thing, so I wrote a couple of sample apps this morning implementing (by accident) elastic rope (inadvertently the opposite of gravity) and gravity/magnetism. I’m not sure what the distinguishing features would be between gravity and magnetism, but the effect is fairly convincing (I think) for such a small piece of code. If anyone can best me please do - I’d love to see it done with ‘proper’ maths (mine is rubbish, so I make do with what I got)…
Great magnetism function, it comes in superbly handy in my code right now, thanks for sharing! [import]uid: 10284 topic_id: 4331 reply_id: 17020[/import]
Have the sensor apply a force to any object it encounters directed towards the centre of the gravity well. [import]uid: 5354 topic_id: 4331 reply_id: 17251[/import]
Actually, if I’m brutally honest, that didn’t use a sensor, but calculated the distance and applied linear force when the distance fell below a threshold. A sensor would have been better, as it does use the physics engine. [import]uid: 8271 topic_id: 4331 reply_id: 17264[/import]
[lua]-- Create pockets
local pocket = {}
for i = 1, 3 do
for j = 1, 2 do
local index = j + ((i-1) * 2) – a counter from 1 to 6
– Add objects to use as collision sensors in the pockets
local sensorRadius = 20
pocket[index] = display.newCircle( -389 + (515*j), -436 + (474*i), sensorRadius )
– (Uncomment the line below to make the pocket sensors visible)
–pocket[index]:setFillColor( 255, 0, 255 )
I’d like to be sure as to where we’ll be putting ‘exert force’ commands?
would that be in the ‘inPocket’ method from the example?
So if I understand correctly, a sensor detects the collision when it enters the sensor radius?
Then is the owness on the developers to write the math that says ‘exert more force as you get closer?’
You need to calculate the distance and angle between the 2 objects which will then allow you to calculate a force in the x and y directions at the objects reference.
Basically its trig, look in the Code Exchange area for the Joystick, it uses the same maths to calc angle and dist of the users touch to the centre of the joystick
M [import]uid: 5354 topic_id: 4331 reply_id: 17269[/import]
I hate to bring back a dead thread, but i’ve given it a fair amount of searching. I have been looking into putting magnetism or gravity of sorts into my little prototype, but I was hoping to find some code sample on using the sensors to help create that sense of gravity. I’ve taken a look at the one you posted, Horacebury and it looks great but I’m kind of leaning towards using the most out of the physics engine. Any advice? [import]uid: 92059 topic_id: 4331 reply_id: 57339[/import]
If you’re taking about making a gravitas type game where objects get attracted towards multiple points on the screen, then I’m not sure but I would start with setting the world gravity to 0,0 and using timers to call applyForce on the objects.
If you mean something else then I’m not sure - what are you trying to do? [import]uid: 8271 topic_id: 4331 reply_id: 57916[/import]
Well, I have a catapult launcher-type game code foundation with my own assets and a few twists. In the game, I have static objects that have their own “gravity,” and I’m using Pythagorean theorem to calculate distance and then it applies a pulling force to the catapulted object towards the static object when it gets within a certain distance of the static object giving it a sense of magnetism.
Basically, I want it to mimic the effect of gravity in that the closer you are to it, the stronger its gravitational pull will be. If you could take a look at this diagram I whipped up real quick: http://i54.tinypic.com/2czva8j.jpg
you can see that zones 2 and 3 should have more “pull” than zone 1, and 3 more so than 2, but with the pythagoran theorem code I have in, zones 1, 2 and 3, all have the same amount of “pull” force. I know I could make a false sense of gravity using the diameter information of my objects, but I wanted to know how I could use the physics engine, in this case, the sensors, to do that kind of math for me.
[import]uid: 92059 topic_id: 4331 reply_id: 58297[/import]
Basically, I used a circle object as a sensor to detect when the force should be applied to the moving object. The amount of force to use is relative to the object’s distance from the edge of the circle. To get that distance you need to draw the line from the centre of the circle, through the moving object’s centre to the edge of the circle.
In short, calculate the distance (using pythagorus) from the circle centre and subtract that from the radius of the circle. That’s your amount of force.
The fan demo does this, I just hope the math is clear enough in the code. It’s fast but possibly not tidy. Also, this is the most I get into math, so there is possibly a more efficient way of doing it.
Matt [import]uid: 8271 topic_id: 4331 reply_id: 58327[/import]
Hi horacebury… I know this an old thread, but it’s become quite relevant to an issue I’ve got… I hope you can help.
I’m trying to simulate magnetism (the link above is now ‘dead’)… I know Box 2D makes good reference to ‘radial gravity’, but Corona’s API seems to have ignored that particular property.
I’ve also seen that particle candy uses ‘attraction fields’, which is great, but I’d like to learn the principles myself.
In a nutshell, do you have a live link to the code mentioned above, or any other pointers?
All the best
Mark [import]uid: 176868 topic_id: 4331 reply_id: 136763[/import]
Hi horacebury… I know this an old thread, but it’s become quite relevant to an issue I’ve got… I hope you can help.
I’m trying to simulate magnetism (the link above is now ‘dead’)… I know Box 2D makes good reference to ‘radial gravity’, but Corona’s API seems to have ignored that particular property.
I’ve also seen that particle candy uses ‘attraction fields’, which is great, but I’d like to learn the principles myself.
In a nutshell, do you have a live link to the code mentioned above, or any other pointers?
All the best
Mark [import]uid: 176868 topic_id: 4331 reply_id: 136763[/import]