Local gravity i.e. Planets/Moons

  • Hi guys!

    Sorry if this is a duplicate thread. I did do a number of searches, but couldn't find anything. I'm trying to create a local gravity to create a game in a style similar to Solar 1 & 2, where you position planets/moons, and objects will rotate around them in a controlled orbit, or will be pulled towards them.

    I've been looking at the Demos Paper Planets and Moon Shield, and if I could get my head around them, I think I'd have all I need. I'm starting to wonder whether these are created using the physics behaviour or not.

    Hope someone can help!

  • For MoonShield I moved the moon with events and the asteroids with physics :

    basically for the moon I increase an angle private variable (I have the distance from the earth in another pv) and then I just position them with

    moon.X = earth.X + cos(moon.angle)*moon.distance
    moon.Y = earth.Y + sin(moon.angle)*moon.distance

    For the asteroids it's a bit more complicated. I calculate the gravity force of each asteroids toward each moons and the earth

    Moons, Earth and Asteroids have all a "mass" private variable (so I can tweak it)

    And I just have to :

    foreach asteroid
       /// Calculating the gravity vector toward the earth following the gravity formula G*m1*m2/d?  (G is multiplied when I apply force at the end)
       asteroid.xVector = (asteroid.mass* earth.mass/distance(asteroid.X,asteroid.Y,earth.X,earth.Y)^2)*cos(angle(asteroid.X,asteroid.Y,earth.X,earth.Y)
       asteroid.yVector = (asteroid.mass* earth.mass/distance(asteroid.X,asteroid.Y,earth.X,earth.Y)^2)*sin(angle(asteroid.X,asteroid.Y,earth.X,earth.Y)
    
       /// adding the other gravity vector
       foreach moon
          ADD (asteroid.mass* moon.mass/distance(asteroid.X,asteroid.Y,moon.X,moon.Y)^2)*cos(angle(asteroid.X,asteroid.Y,moon.X,moon.Y)
          ADD (asteroid.mass* moon.mass/distance(asteroid.X,asteroid.Y,moon.X,moon.Y)^2)*sin(angle(asteroid.X,asteroid.Y,moon.X,moon.Y)  
    
       /// Applying the result gravity vector
       Every Tick asteroid Apply physics force (Clamp(asteroid.xVector*gFactor*dt,minVect,maxVect),Clamp(Self.yVector*gFactor*dt,minVect,maxVect)) at image point 0

    And that's all, the physics behavior use the force I calculated to create the movement.

    Oh and I clamped the vector because the asteroids tended to go to fast and the gFactor is a global variable used to tweak the gravity a bit for better gameplay

  • ake an object rotate around another via events - LINK

    From the How do I FAQ.

  • Thanks Kyatric, I've bookmarked the page and will look into them! There's loads on there which will be very useful for what I'm doing.

    Yann - I'll be sure to look into this! It's a little above my head at the minute, but I'm sure I'll pick it up. Trying to set an array to the distance between the two objects so that I can calculate the rotation. It's all a fun learning process.

  • Hi Yann,

    Is xVector & yVector part of an array or a variable that you have created? I can't seem to see it anywhere.

  • You have to add the physics behavior to the object to access those expressions.

  • Thanks Kyatric. I have the physics behaviour added, but I can't see vector anywhere. I'm looking at the actions, and can't see it there. Can you tell me if it's in a sub category? I've used the search function within the program, and the help button takes me to a 404 page <img src="smileys/smiley5.gif" border="0" align="middle" />

  • xVector and yVector are indeed just mere private variables

    So is mass

    Basically a force is what make things accelerate or decelerate.

    If you don't apply any force to an object, it's speed will never change

    A force is generally modeled as a vector, because as a vector, a force has a direction and a quantifiable value (how much) which is represented by the length (norm/magnitude) of the vector.

    A vector in 2D has two composants often described as X and Y

    In construct (classic or 2) you can't store a vector in one variable, you need one variable by composant. It is the purpose of xVector and yVector.

    To calculate each composant of a vector oriented you have to find orientation and length.

    gravity = G*m1*m2/d? gives you the length

    the orientation is just the direction toward the considered object.

    so you just have to find the unit vector (vector whose length = 1) representing this direction and multiply it by the length calculated above ('cause 1 * gravity = gravity hoho)

    the unit vector is simply calculated by

    cos(angle)

    sin(angle)

    angle = angle(source.X,source.Y,target.X,target.Y) (neat function in construct)

    so just have to do a

    xVector = gravity * cos(angle)

    yVector = gravity * sin(angle)

    I also added many times this kind of calculation because adding vector is the same as adding its composant

  • So close!!! I just can't see what I'm missing! I have given the items mass, and the asteroid just seems to pick a direction and shoots off. I think it's close. At one point I had the asteroid drag over to the sun in the centre, but I was due to a code glitch. I wish I could just set the gravity to a local point, or have the mass affect the gravity.

    I have uploaded the file I've been working on. The arrow keys control the moon/planet. http://dl.dropbox.com/u/50465867/Gravity.capx

  • Well you made a lot of mistake

    The bigger one is that you forgot to set world gravity to 0 (easy to miss)

    Others are in how you use foreach and how you nest things

    Also you added gravity to mass at some point

    And you didn't need to use an Array object, global variables are better

    etc, etc, etc

    Just compare I hope you'll understand

    gravity.capx

  • I could have sworn I'd set the gravity to 0. Looking at this, I realise I knew nothing about sub-events and blank events containing actions. Need to figure out how that works. Starting to think I've jumped into the deep end, but this really is fascinating. The physics engine is purely used for collisions and forward momentum. Everything else is handled by maths. It's a lot to get my head around, but I'll do my best!

    Thank you for all your help on this. I'll have a tweak on the figures until I understand what each one does, then I'll have a look at improving the graphics and making a game out of this.

    Thanks again!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • OK, I just saw this thread (sorry for reviving it) but I feel I must contribute:

    Lets say you want a crate to be pulled by one or more moons ;)

    -Set the world gravity to 0

    -Physics behaviour in all the objects in with the gravity force will act (in this case, the crates).

    -Events:

         Each 0.5 seconds (for better performance)

         Apply Force On CRATE Toward position MOON.X, MOON.Y->GLOBAL_VARIABLE * (CRATE.mass + MOON.mass) / ((CRATE.X - MOON.X)*(CRATE.X - MOON.X) + (CRATE.Y - MOON.Y)*(CRATE.Y - MOON.Y))

    (I have optimized the gravitation formula a bit)

    GLOBAL_VARIABLE should be replaced by any global variable you want, it can (and probably should) even replace the mass+mass on top, and will probably be a BIG value.

    Hope it helps.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)