calculating vehicle from ground

General discussion about Box2D tutorials
Post Reply
vkreal
Posts: 66
Joined: Sun Jan 13, 2013 7:29 pm

calculating vehicle from ground

Post by vkreal »

I am trying to figure best way to calculate distance between b2body(vehicle) and ground when off the ground moving at high velocity. Any thought you guys can throw my way?
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: calculating vehicle from ground

Post by iforce2d »

Cast a ray directly downwards from the body position, would be the most typical way.
vkreal
Posts: 66
Joined: Sun Jan 13, 2013 7:29 pm

Re: calculating vehicle from ground

Post by vkreal »

ray cast might cause performance problem(lower framerate) since i have to do it every step. My problem i am trying to solve is my vehicle traveling down hill at high velocity and catches some air. When this happens i would like to zoom back so the ground still shows in the view port.

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

Re: calculating vehicle from ground

Post by iforce2d »

I really doubt a few raycasts each step will slow anything down.
http://www.youtube.com/watch?v=C-ScURIRTGA

In this game, I used 10 downward raycasts to sample an area in front of the rider, to help determine a good zoom distance, similar to what you are talking about. It's also used to set his head angle in a realistic manner:
http://www.youtube.com/watch?v=OL66G3JLYvA

Compared with all the other work that Box2D is doing every step, a raycast is almost nothing. Best way to find out is give it a try ;)
vkreal
Posts: 66
Joined: Sun Jan 13, 2013 7:29 pm

Re: calculating vehicle from ground

Post by vkreal »

AWESOME! that''s what I am looking to achieve from your example game. I will give it a try.

Thanks for your help!
vkreal
Posts: 66
Joined: Sun Jan 13, 2013 7:29 pm

Re: calculating vehicle from ground

Post by vkreal »

I am curious why 10 downward raycasts to sample an area. why won't one straight down work? I am using one raycast straight down to sample distances to ground but i see the zooming kind of jerky. I think it might be results from the raycast sometimes not hitting target(not sure). I going to try more raycasts maybe that will solve jerky zoom problem.


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

Re: calculating vehicle from ground

Post by iforce2d »

More samples gives a better uh... sample of the ground. For example in the upper image here, one sample will result in a sudden change as the ray goes over the edge of the drop, even though the bike has only moved a tiny bit. In the lower image, one ray has dropped over the edge in exactly the same way, but the average height of the points has not changed so much.

Yes, more samples will probably fix it. I think I also changed the spacing between rays to sample a larger area ahead when moving faster.
Attachments
surface sampling.png
surface sampling.png (165.88 KiB) Viewed 22076 times
Post Reply