Local gravity i.e. Planets/Moons

Get help using Construct 2

Post » Sun Nov 20, 2011 2:49 pm

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!
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
68
S
8
G
6
Posts: 930
Reputation: 9,358

Post » Sun Nov 20, 2011 4:33 pm

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
[code]moon.X = earth.X + cos(moon.angle)*moon.distance
moon.Y = earth.Y + sin(moon.angle)*moon.distance[/code]

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 :

[code]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[/code]

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
Yann2011-11-20 16:39:53
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Sun Nov 20, 2011 5:20 pm

[quote]Make an object rotate around another via events - LINK[/quote]
From the How do I FAQ.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,998
Reputation: 57,791

Post » Sun Nov 20, 2011 10:54 pm

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.
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
68
S
8
G
6
Posts: 930
Reputation: 9,358

Post » Mon Nov 21, 2011 1:07 pm

Hi Yann,

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

Nintendo Wii U Developer using Construct 2
B
68
S
8
G
6
Posts: 930
Reputation: 9,358

Post » Mon Nov 21, 2011 5:46 pm

You have to add the physics behavior to the object to access those expressions.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,998
Reputation: 57,791

Post » Mon Nov 21, 2011 7:29 pm

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
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
68
S
8
G
6
Posts: 930
Reputation: 9,358

Post » Mon Nov 21, 2011 8:28 pm

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
Yann2011-11-21 20:39:53
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Mon Nov 21, 2011 10:17 pm

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
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
68
S
8
G
6
Posts: 930
Reputation: 9,358

Post » Mon Nov 21, 2011 11:45 pm

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
Last edited by Yann on Sun Sep 14, 2014 10:59 pm, edited 1 time in total.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Next

Return to How do I....?

Who is online

Users browsing this forum: currypuff, makotto and 21 guests