Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 12 May 2007
Offline Last Active Dec 06 2013 04:14 AM

Posts I've Made

In Topic: Flying physics: acceleration while diving/ascending

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.

In Topic: Flying physics: acceleration while diving/ascending

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();
			//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();
			//angle from straight-up to vector pointing towards ground
			float LowerVecAng = vecToLower.getAngle();

			zVec2f heading = moveDir;

			float headingAng = heading.getAngle();

			//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).

In Topic: "Dipping" physics: flying object swooping and then returning to previ...

08 November 2011 - 03:50 AM

I was right, the rotations were being added every frame, resetting the rotation each time works :P

Sorry for being mysterious :wink:, to clear things up: there's no gravity required, the object in question is an insect that bobs up and down at a certain height and when a button is pressed, it swoops down to a lower height and bobs up and down at that height.

I am in fact looking to make the insect speed up during dives/rises, but at the moment I'm just kind of experimenting with ways of doing things and seeing what feels right, so for the time being there is no real "right" way to do it

In Topic: "Dipping" physics: flying object swooping and then returning to previ...

08 November 2011 - 03:22 AM

Thanks guys, this is a lot of help!

I think I know why my object isn't behaving as I thought - the rotate() function is relative to the current position, so when it gets and angle of, say, 4.1 radians, its rotating that amount every frame which may be why its flying about all over the place.

I'm going to try resetting the rotation back to a specific default value every frame and rotating from there and see if it fixes the problem.

In Topic: "Dipping" physics: flying object swooping and then returning to previ...

07 November 2011 - 10:10 AM


I'm trying to create a simple 2D game where an object is flying at a particular height at constant velocity. When I press a button, the object starts to dive, the dive getting steeper the longer you hold the button. When you release the button, the object levels out, then starts to climb back up to its original height.

Could someone give me some math pointers for making this mechanic?

I wouldn't go with splitting the velocity into horizontal and vertical. In fact, if you keep horizontal as it is and increse the vertical part, the total velocity will have higher magnitude, which probably won't be what you want.

I would solve your problem by having a velocity vector v (which will be horizontal during the initial flying at a constant height).
When you hold a dive button, I would simply rotate the vector down. The longer you hold the button, the bigger the overall rotation, but with a limit at some defined angle (for example 70 degrees to not let it dive too steep)
When you don't hold the button, I would gradually rotate the vector back to the initial horizontal orientation, then compare the current height with the target height and if it is smaller, rotate the vector further upwards to make the object ascend to the required target height.

Your actual position is then simply updated every frame as
p = p + v * dt;
where dt is the timestep between frames, p is a position vector (2D) and v is the velocity vector (2D).

If you always change the rotation gradually (so not for example from 0 to 40 degrees in one step), the resulting movement will be nicely smooth (the object's central point would draw a smooth curve).

Please let me know if this verbal explanation isn't clear and/or if you would prefer some equations.

Btw, it of course all depends on what kind of movement are you trying to get, what kind of object it is etc. Maybe you really want it to dive at higher overall velocity than when it flies horizontal?The solution with rotating velocity vector would suit something like an airplane, where the rotation even can be nicely visualised (the whole airplane rotates).

Thatnks, this is closer to what I want.

The idea is that the object will have 2 heights: at default, the object will move towards the upper height (default behaviour) and when diving, it will move towards the lower height.

I have the velocity and I'm adding it to the position as you suggested, and I am getting movement forward (because the default velocity is forwards, but I'm having difficulty correctly calculating the rotation.

//if diving, set target y to lower limit
			zVec2f target;

			target.x = getPosition().x;
			target.x += 5.0f;

				target.y = baseline_y_lower;
				target.y = baseline_y_upper;

			zVec2f delta = (getPosition()-target);
			float TurnDir = delta.getAngle();

			float rotDif = zRadRelative(TurnDir-physOrientatiation.xa.getAngle());

			//set final velocity
			zVec2f velocity = physOrientatiation.xa;
			setPosition(getPosition() + velocity * ev.getTime().dt);
			//rotate sprite

The above code makes my object fly in weird directions. I'm not entirely sure why though :/

Edit: physOrientatiation.xa.rotate(rotDif); rotates to an absolute value in radians I believe, so when it calculates the difference in angle between the current heading and the target point, the value returned is the total angle needed to turn, not the angle needed to turn this frame.

That being said, I'm wondering why this is causing such odd behaviour since, if the object turns directly towards the target in 1 frame, the next frame the difference should be tiny or non-existant, meaning that it should snap to facing the right direction immediately and then continue correctly, but instead it floats off in odd directions.