@goldage5 you haven't really given me much to work off, but here goes:
Thanks! That fits perfectly with my game.
Not Telling
02 December 2012 - 08:53 PM
@goldage5 you haven't really given me much to work off, but here goes:
01 December 2012 - 10:55 PM
07 November 2012 - 08:17 PM
04 October 2012 - 06:33 PM
24 September 2012 - 07:08 PM
Sure! Let's start with Newton's law:
As shown by this equation, the force which gravity has on an object is equal to
the constant of gravity, multiplied by the two masses multiplied by each other and
divided by their distance squared.
So, we already have the values we want to work with: you can easily assign "mass" variables
to your objects based on their apparent size, you can set a constant for gravity (DON'T use GM's -
set your own variable, this is just for the math), and you can know the distance squared by doing
power(point_distance(x1,y1,x2,y2),2).
Essentially, you need to set up a loop in which one object adds force to itself based on doing this
same equation with all of the other objects in the room. You can find all other gravity-inducing bodies
by doing something like this:{ grav = 0.25; var a, b, m1, m2, dist; for (a = 0; a < instance_number(obj_planet); a += 1) { b = instance_find(obj_planet,a); if !(b == id or b == noone) { m1 = mass; m2 = b.mass; dist = power(point_distance(x,y,b.x,b.y),2); motion_add(point_direction(x,y,b.x,b.y),grav*((m1*m2)/dist)); } } }
First, I set "grav" to 0.25, representing our constant for gravity (which, again, cannot
be the one set by Game Maker.) Then, I run through all of the instances of "obj_planet"
in the room - you would replace this with whatever you want to apply gravity to. If the instance
found is the instance doing the checking or "noone" (another GM constant), then the loop should
ignore it, as otherwise we will receive errors. Then, we get the mass of the first and second objects
(you should have set this already), and the distance squared. Finally, we run the formula. Please note
that this code assumes some kind of collision detection - if dist is 0, then you will get a division by 0.
This code also assumes that the object running it is an instance of obj_planet.
How does this work for you?
Community Forum Software by IP.Board
Licensed to: YoYo Games Ltd

Find content