2D Car control ?
BlitzMax Forums/BlitzMax Programming/2D Car control ?
| ||
* EDIT - Example updated Has anyone had a crack at 2D overhead car physics? What I have below is a basic 2D car control concept (No external media required) The problem is, I don't know how to implement 'skidding' (if the car turns sharply at high speed for example) The car needs to slide across the road until grip is re-gained (through a combination of loss of speed and user turning into the skid) I have tried to get my head around various online tutorials but get lost in the maths CONTROLS =============== Use CURSORS to control steering and speed Use SPACE to act as handbrake NOTES ================ The little white stick which moves indicated where the turn of the wheels |
| ||
CBA to write code but you basically need two vectors. One for the direction the car is facing (vF), and one for the direction the car is moving (vM). When the player turns, you turn the vector vF. vM is constantly updated so it will gradually turn towards vF at rate tR, and this will give an inertia effect. By varying the rate tR you can simulate different track conditions - a lower rate for ice, a medium rate for water/dirt and a high rate for tarmac/dry. That's the gist of it. It'll take some tweaking to get the parameters to 'feel' right. |
| ||
Rob Farley posted some excellent fake physics here... http://www.blitzbasic.com/Community/posts.php?topic=72826#813991 It's B3D but easy enough to implement in 2D. It was a big help for my game New Star GP. |
| ||
there was a blitzmax physics mod with a car example, physlite at gooeyblob.com |
| ||
Thanks all so far One part I cannot get my head around is how to calculate the turning circle of the car Look at this for example. Imagine the wheels are turned say, 40 degrees clockwise. The car would pivot somewhere to the right of its back/right wheel How do you calculate something like this? |
| ||
Look at this for example. Imagine the wheels are turned say, 40 degrees clockwise. The car would pivot somewhere to the right of its back/right wheel Don't forget that the car is moving too -- It's really the front wheels that pull the car around the curve, not the rear. There is no sideways movement of the front wheels, after all. |
| ||
Yep. Its the actual pivot point location I don't know how to calculate If the wheels were turned just 1 degree then the car would barely rotate during its course of movement Equally, if the wheels were at full lock (clockwise in this example) the car would be rotating at a point pretty close to the rear right wheel If the wheels were 90 degrees I imagine the pivot point would be directly under the rear/right wheel The question is, how do you calculate the correct 'pivot point' (as indicated by the RED circle in the photo below? I think at least two things need taking into account: 1) the amount of turn on the front wheels 2) the distance from the rear to the front wheel |
| ||
I think you can get the path of the car like this... |
| ||
I posted a conversion of Marco Monster's car physics code in the archives years ago : http://blitzbasic.com/codearcs/codearcs.php?code=135 It allows full sliding power oversteer, understeer, 2 and 4 wheel drive to the front or rear wheels etc... I've got a massively tweaked version of this code in a few small projects at the moment and it does allow for some pretty impressive car physics, albeit only on a 2d plane (there is no height aspect involved, so no jumps and cars won't increase speed down hills etc..). |
| ||
I am just skimming through the code Vorderman. Do you know if the code handles reversing of the car? |
| ||
I can't remember about that particular code but my current version certainly does, so it should be pretty easy to add to that older code. IIRC I just limit the speed to a low negative and then reduce the effect of the steering so the car doesn't turn too fast. I've also put reverse onto a seperate button so you can brake to a stop without auto-reversing. |
| ||
Many thanks. I have a BlitzMax version running Q1) Got any good parameter settings there? To me, the defaults make the sliding way too sensitive Even travelling in a straight line makes the car slip Also, when the car slides around it seems to stop abruptly after 90 degrees or so Are you still chipping away on your SCR remake? |
| ||
there are a few bugs in the advanced. The simpler one works great though. here is the simpler one in a OO: on the advanced one these two variables are calculated but not used at all. car_COGx# car_COGy# it seems to me that there still needs something to be calculated. |
| ||
Nice Jesse. I was going to OOP it too However, the example still seems to suffer the exact same issues as Vordermans I have taken your example and applied a reference grid and "Parameter Editor" (so that you can goof around with the cars general parameters) By default the car seems to be running on extremely smooth ice and I want to avoid this 'uncontrollable' feel I do want the car to skid but under much harder force. I just don't know how to apply more grip to the car (or indeed make the road feel like tarmac) MULTI2 is about the closest one for containing the skidding The car gets a bit wobbly/shaky though after its stops depending on what the parameters are tweaked to Also, as Vordermans, when you apply the handbrake (SPACEBAR in my example here) the car whips around quickly to 90 degrees or so with a sudden halt. This looks unrealistic to me The general "feel" I am after is that as demonstrated by my first simple example at the top of this thread |
| ||
I've no idea about parameters specific to this code, as I wrote it years ago and my current code is pretty well modified with most things having been tweaked and new parameters added, and I'd rather not hand that code out to everyone as it's taken a long time to develop. Here are my current settings params, no idea how many of them the old code uses though - However with some tweaking you certainly should be able to achieve a solid nicely-handling car - as inspiration, have a look at this short (and poor-quality!) video of a drift car in my current test app : http://webspace.mypostoffice.co.uk/~james.kett/B3D_drift.flv |
| ||
Great-looking car control in the video. Definitely the kind of control I'm aiming for Things are starting to take shape although the car is suffering from the shivers when I brake to a complete stop I am thinking this is down to floating point variables being divided into tiny fractions BTW, +1 for the donut in the video! |
| ||
I think the shivering is caused by the car's angle parameter being updated with fluctuating amounts when the speed is very low. I use this piece of code in the physics routine to prevent it happening - note that this takes into account whether the player has lots of throttle on or not, so the player can still accelerate away if they want to. |
| ||
I'll take a look at that Vordeman. Thanks Also, I spotted things like: If( velocity_x# = 0 ) ..... Since the velocity_x# is a float it is very unlikely to equal '0' I changed these occurrences to: If Int(velocity_x) = 0 ..... Which seems to have stabilized the car completely when stopping Nice working example |
| ||
I went looking for the original source code from: Marco Monster, and I found it. Obviously it's in c and I tried to convert it to Bmax but I can't get it to work the same. Maybe it's because I can't get the delta to work the same way. if anyone wants to brain-storm it, here are the files(the converted blitzmax,the original c file, and the original executable): http://www.filefront.com/14674693/cardemo2.zip |
| ||
That's definately coming on Jim, I never tried a realtime parameter editor like that, it's pretty cool. I have always just altered the params in a text file then re-run the app to test them, which is very crude by comparison - I'm really tempted to add the param editor to my drift test program and see what else I can get it to do... |
| ||
Jim, I've tried the last code you posted and it controlls pretty decent. but: Which seems to have stabilized the car completely when stopping have you tried to turn left or right while stopped?, it goes crazy. |
| ||
Vorderman, Feel free to use my Parameter Editor The current design is for floats only though. I written it specifically to avoid constantly tweaking variables then re-compiling the code. A chore I always find a PITA One nice thing, it can handle negative as well as positive ranges All control is via the Left mouse button (when the editor is active) you can even use the mouse wheel to fine tune values (roll wheel over slider) I have also captured the Left mouse button so that the slider is still under control even when the mouse accidentally drifts out of the slider zone The basics for using the parameter editor are: Add the (float) variable to the editor in which you are interested in editing. This is done via param.Add VarPtr(myvar) , label$ , LowVal# , HighVal# The parameters are: VarPtr(myvar) ' the(float) variable which you want to change label$ ' an onscreen descriptive tag LowVal# ' lowest allowable value (can accept negatives) HiVal# ' highest allowable value In the main loop, you need to update any LIVE changes via param.Update By default, the editor is hidden. To show it (or toggle it on/off) I tend to use this in the main loop. Which, does exactly as it says on the tin (toggles the editor on or off): If MouseHit(2) then Param.ToggleEditor Here is a simple example Global mycarspeed#=20 ' Let's allow LIVE manipulation of mycarspeed# ' The lowest value will be restricted to -10 ' the highest value is restricted to +40 param.add VarPtr(mycarspeed) , "Car Speed" , -10 , 40 ' main loop While blahhblahhblah If MouseHit(2) then Param.ToggleEditor Param.Udate Flip Wend The SOURCE |
| ||
Some how it seems like if it skids a lot easier. The original demo seems more realistic. It's practically a 1 to 1 conversion but some how I feel I might have screwed it up somewhere. |
| ||
** EDIT Separate post here (I think I broke the previous one with too much code) Jesse, have you tried to turn left or right while stopped?, it goes crazy Never spotted that. Thanks. Fixed below I tried your new converted cardemo code and it seems pretty good although a lot faster than the SDL C demo Some how it seems like if it skids a lot easier. The original demo seems more realistic Yep. I think it's a timing issue of sortsChanging Flip(0) to plain old Flip looks a lot closer to the SDL version |
| ||
Hi Jim Brown, on your very first script above, how hard would it be to convert it to BlitzPlus? or is it possible. I know more about plus then max. Your script is what i've been looking for as examples to a realistic car going forward or reverse with brakes. I was looking at your script and I see the maxspeed or speed input values. Is there anyways we can add more speed values? Like I'm trying to add key 1 0-10mph, key 2 11-20, key 3 21-30, etc... like gear shifter? Thanks. |