### rotate direction of gravity

#### raghav52

• Posts: 6
i know that you can change the direction of gravity vertically or horizontally in stencyl. but what i want is a behavior to rotate the direction of gravity clockwise or anticlockwise by  when the right/left arrow key is down.if it is possible, how do we do it?i also want the the actor to rotate by the same amount.

« Last Edit: March 08, 2013, 04:23:27 am by raghav52 »

#### Hectate

• Posts: 4643
Well Horizontal Gravity + Vertical Gravity = Diagonal Gravity. After that it's just setting it appropriately based on the angle (and having the actor match that).
 :: Patience is a Virtue,But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

#### raghav52

• Posts: 6
@ hectate - how do you do that?

#### Hectate

• Posts: 4643
TLDR warning: if you skip to the end, yes you'll have a solution. If you read this with the intent to learn, however, you'll be better prepared for the next time you have a related issue.

Calculating the exact values to use to implement a diagonal gravity is a relatively straightforward math exercise. If you've never had to address this type of question before though, it has a notably non-obvious solution. Let's take the following hypothetical scenario to determine the exact formulas we need and create a solution for you.

We want gravity as 5 meters per second (m/s) in our scene, regardless of direction. While that's rather slow in Stencyl scale, it's a good value to use because it's a nice integer that we can do math on without getting too complicated or into extremely high values.

If we want vertical (Y) gravity or horizontal (X) gravity alone, we could simply set one or the other to 5 (or -5) to achieve this speed. As soon as we diagonal motion however, a problem presents itself in that both values need to be less than 5 m/s; but in a way that adds up to 5 m/s overall. Below is an image that uses 45 degrees to demonstrate the issue.

Imagine that the green line represents the distance traveled by an object in one second. As you know, we intend that it should travel 5 meters, so therefore the green line (horizontal movement) is 5 meters distance. Looking at the red line, however, we can see that if we just give an object 5 m/s gravity both horizontally and vertically (thus the square shape), it will actually travel farther than 5 meters overall! The right half of the image, with the quarter-circle, gives you a clue that the distance (which our speed is just distance over one second of time) would be constrained within a circle from the starting point. So our problem then is to: Given an angle (the direction you wish gravity to go in) and a distance (the speed of that gravity), determine the X (horizontal gravity speed) and Y (vertical gravity speed) of a point on the circumference of that circle. Now we're getting somewhere!

So here's the real key to the question. Notice that now our green line - the correct, 5 m/s distance - is angled. Notice as well that the blue lines describe the X and Y distance to the end of the green line. The three of them form a triangle, which means that by using what we already know about the triangle, we can determine things that we don't know. In this case, that means (like we described the problem above) we can use the angle (30 degrees in this image) and length of the hypotenuse (the green line) to determine the length of the blue lines. One final picture!

So now I've broken the triangle down into individual parts. We know two of them (the angle and the length of the hypotenuse) which are yellow and green, respectively. We want the length of the blue and red line. A quick review of geometry formulas reveal the following:
1. The red line is known as the "adjacent side" because it is next to the known angle.  The formula is:
Quote
or in our example...
Quote
X = Cos(30) * 5
X = .866~ * 5
X = 4.33~
So we would want our X gravity to be about 4.33 m/s in this example.

2. The blue line is known as the "opposite" side since it is opposite of the known angle. Its formula is:
Quote
Opposite = Sine(Angle) * Hypotenuse
or again in our example
Quote
Y = Sin(30) * 5
Y = .5 * 5
Y = 2.5
so our Y gravity would be 2.5 m/s in this example.

Applying both amounts of force, 4.33 and 2.5 m/s, will cause our object to travel diagonally (at 30 degrees anyway) for a distance of 5 meters in a second's time.

So after all that, here is the task for you to implement this. Create an attribute to hold the angle you want the gravity to go in, and another for the amount of force that gravity will be. Then, set both your horizontal and vertical gravity using the formula above with those two attributes inserted where needed. Any time you want to change the direction of your gravity, all you will have to do is change that angle. Obviously, if you want to increase/decrease the gravity force then you can adjust that attribute as well.

Good luck!
 :: Patience is a Virtue,But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

#### raghav52

• Posts: 6
thx a lot hectate.

#### P01

• Posts: 155
Thank you very much Hectate for that thorough explanation! The diagrams really help me visualize and comprehend the math behind the solution and for someone who is poor at math that really helps.

#### Hectate

• Posts: 4643
I'm glad it helped! Keep in mind as well that these same formulas and ideas work for other things besides just gravity. In fact, pretty much anytime you have to do math on a diagonal line (finding an angle, distance, etc) you're going to be turning it into a triangle and doing math on it that way.

Good luck!
 :: Patience is a Virtue,But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.