Hover craft tutorial

General discussion about Box2D tutorials
Post Reply
blakey87
Posts: 28
Joined: Wed Jan 09, 2013 7:57 pm

Hover craft tutorial

Post by blakey87 »

Hi iForce, thanks for helping me out over at the hovercraft tutorial,

I feel I might be able to make a cool anti gravity game out of it which would be sweet.

I just got a couple of hurdles I'm struggling with, I have the ship rotating based on the angle of the body below it,I am using AngularImpulse, as this gives the feel I am looking for when the ship rotates.
http://screencast.com/t/j6brecQtsX

Rather then using world gravity I use the the hovercrafts body gravity scale, that way it allows the hover craft to drive up side down.

Background aside,

The video below shows most of the above.
http://screencast.com/t/HWzTEbKJ

First issue, when the craft goes up the slope using apply force, it seems to stick to the ground below, is there anyway of preventing this?

When the ship goes down a slope, it does not really hover above the slope if its going to fast?

Solving or improving the above two issues, would make me far more comfortable going forward, I think I could live with the issues but the levels I create will have to be more basic, I would be really great full for your help and insight in how I could deal with this.

Thanks!
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Hover craft tutorial

Post by iforce2d »

I'm not really sure what's happening, but it seems very strange that the display at the top says "Distance above ground: 2.207" and so on, when the body is clearly scraping along the ground. I would expect that value to be zero when the body is fully touching the ground. Since that distance is being used to calculate the strength of the repelling force (at least that's how I originally did it) then it would make sense that it doesn't repel away from the ground because it thinks it's already well above the ground.

Then again, if you have changed the output shown in that display, this could be irrelevant.
blakey87
Posts: 28
Joined: Wed Jan 09, 2013 7:57 pm

Re: Hover craft tutorial

Post by blakey87 »

Ah, that's a good point, the output is the same, I never noticed the issue with the distance when its on a slope, its weird as when I drag it along the ground with the mouse, the distance goes into the minuses even though its not changing.

A good starting point would be making sure the distance is more accurately calculated when moving on a slope, I'll have a go, if you got any other ideas let em know. Thanks
blakey87
Posts: 28
Joined: Wed Jan 09, 2013 7:57 pm

Re: Hover craft tutorial

Post by blakey87 »

Further investigation shows the linear velocity y actually increases as it goes up the slope which makes complete sense, which it turn is throwing of the distance calculated via this line off.

distanceAboveGround += 0.25f * m_hovercarBody->GetLinearVelocity().y;

So I'm guessing I need some sort of way of negating the effects of linear velocity on slopes perhaps?
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Hover craft tutorial

Post by iforce2d »

ah, I see. That only works in the example because the ground is always assumed to be straight downwards. I guess you would have to use the velocity relative to the current ground normal.
blakey87
Posts: 28
Joined: Wed Jan 09, 2013 7:57 pm

Re: Hover craft tutorial

Post by blakey87 »

iforce2d wrote:ah, I see. That only works in the example because the ground is always assumed to be straight downwards. I guess you would have to use the velocity relative to the current ground normal.
Any tips on doing that, I have some brute force ideas, but a nice elegant solution would be nice, to keeps things as clean as possible.


Also I think there is a bug in the hovercraft example, as it seems to use the look ahead distance rather then the actual distance, unless its meant to be this way
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Hover craft tutorial

Post by iforce2d »

You can get the ground normal from the raycast, and project the current velocity onto that (example for projection: http://www.iforce2d.net/b2dtut/top-down-car , see also: http://en.wikipedia.org/wiki/Vector_projection). That would give you the velocity relative to the ground.

To call something a 'bug' usually means there is some undesired behavior... is that the case here, or does the code just seem odd? If I remember correctly, if you don't use some look-ahead the body will bounce up and down forever (unless you have some linear damping).
blakey87
Posts: 28
Joined: Wed Jan 09, 2013 7:57 pm

Re: Hover craft tutorial

Post by blakey87 »

Wow that was easier then I thought it would be, I don't fully understand the ins and outs, but I got it working.
For those interested what I did to fix was as iForce said, use the the normal from the raycast,

I then created a method based on
http://www.iforce2d.net/b2dtut/top-down-car

Code: Select all

	 b2Vec2 getLateralVelocity( b2Body *m_body , const b2Vec2 normalIn ) {
		  b2Vec2 currentNormal = m_body->GetWorldVector( normalIn );
		  return b2Dot( currentNormal, m_body->GetLinearVelocity() ) * currentNormal;
	  }
From there it should straight forward what you need to replace.

About the bug, what I meant was the actual output in the debug, as it seems to be using the look ahead value as the actual distance above the ground, that seemed to be the reason for the in-correct height you spotted when it was on the slope , but I might be mis-understanding something.

It seems to work really well now.
Thanks for your help! :D
Post Reply