• Create Account

Need scary sound effects or creepy audio loops for your next horror-themed game? Check out Highscore Vol.3 - The Horror Edition in our marketplace. 50 sounds and 10 loops for only \$9.99!

# Flying physics: acceleration while diving/ascending

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1PiCroft  Members   -  Reputation: 135

Like
0Likes
Like

Posted 08 February 2012 - 07:45 AM

Hi,

I've got a small prototype involving a character that flies across a 2d landscape. The character in question isn't a plane and I'm not requiring any advanced physics such as lift, gravity etc.

The character in question currently moves by having a set speed and between frames, I calculate the angle I need to turn from the default and set the character's heading appropriately by rotating the movement vector. While this works, the character has a very uniform movement style and speed that isn't very interesting.

Due to the way I've designed it so far, I'm having trouble creating a system where the character accelerates/decelerates in a fluid way that isn't either horrible to look at or a nightmare to code and I was wondering if anyone might suggest a nice method/way of thinking about the problem that might work?

Please let me know if I need to clarify anything.

### #2before-it-was-popular  Members   -  Reputation: 244

Like
0Likes
Like

Posted 08 February 2012 - 08:29 AM

So I take it no gravity means top-down? One clarification: how is the angle you need to turn to calculated? Does the player press left or right to rotate continuously, or does he press a direction to instantly face that direction - or is it something different?

### #3PiCroft  Members   -  Reputation: 135

Like
0Likes
Like

Posted 08 February 2012 - 09:00 AM

So I take it no gravity means top-down? One clarification: how is the angle you need to turn to calculated? Does the player press left or right to rotate continuously, or does he press a direction to instantly face that direction - or is it something different?

Sorry, I'll try and clarify:

Its actually side-on, the character by default hovers at a certain height and when a button is pressed, they dive down. When the button is released, the character returns to their original height.

The angle is calculated using this code:

//We need to calculate absolute angles between character current heading and upper limit/ground
//get vector to upper y
zVec2f vecToUpper, vecToLower;

vecToUpper.x = getPosition().x + 5.0f;
vecToUpper.y = baseline_y_upper;
vecToUpper = vecToUpper - getPosition();
vecToUpper.normalise();
//angle from straight-up to vector pointing towards upper limit
float UpperVecAng = vecToUpper.getAngle();

//get vector to lower y
vecToLower.x = getPosition().x + 5.0f;
vecToLower.y = baseline_y_lower;
vecToLower = vecToLower - getPosition();
vecToLower.normalise();
//angle from straight-up to vector pointing towards ground
float LowerVecAng = vecToLower.getAngle();

zVec2f heading = moveDir;

//calculate difference in current heading and upper/lower trajectories
float UpperAngDiff = UpperVecAng - headingAng;
float LowerAngDiff = LowerVecAng - headingAng;


Once I have the difference between the character heading and the upper/lower limit, I reset the character heading back to default, calculate a turn based on a turning speed value and set the new angle to that.

The only input the player makes is pressing a button to make the character start diving, if they press nothing, the character just flies forwards and bobs at the maximum height (or turns upwards and flies towards the max height if they are below it).

### #4Olof Hedman  Crossbones+   -  Reputation: 1665

Like
1Likes
Like

Posted 08 February 2012 - 09:27 AM

how do you want it to move?

My first thought is to separate y and x-movement. x-movement could be constant or slightly accelerating (endless runner style)
when you press, you start to accelerate the y-speed, making it "go down" faster and faster while holding the button.
Maybe you want a "lower limit" where it will start decellerating instead so it can't go below a fixed height too.
Releasing the button to reverse it. (start accelerating upward until its "close enough" to the top, then decellerate so it levels out at the max height)

This would give it a bit "sinus"-like movement.

### #5PiCroft  Members   -  Reputation: 135

Like
0Likes
Like

Posted 08 February 2012 - 09:36 AM

how do you want it to move?

My first thought is to separate y and x-movement. x-movement could be constant or slightly accelerating (endless runner style)
when you press, you start to accelerate the y-speed, making it "go down" faster and faster while holding the button.
Maybe you want a "lower limit" where it will start decellerating instead so it can't go below a fixed height too.
Releasing the button to reverse it. (start accelerating upward until its "close enough" to the top, then decellerate so it levels out at the max height)

This would give it a bit "sinus"-like movement.

This is actually what i had in mind, my worry was that the x-movement would remain constant and it would simply replicate my current problem, only limiting it to x-axis movement. I suppose I could try accelerating x-movement by using y-speed as some kind of input but I'm not entirely sure how this would work.

### #6Olof Hedman  Crossbones+   -  Reputation: 1665

Like
1Likes
Like

Posted 09 February 2012 - 03:15 AM

In a normal sinus-wave motion, the x-speed is constant.

In any case, separating the two, would make it easier to tweak until it behaves like you want.

If you implement it with constant x first, then you can simply add a factor you multiply the y-acceleration with, before applying it to the x-speed.
Then x-acceleration would depend on y-acceleration in a tweakable way.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS