Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Top Down 2D Racer - Physics & Drift

Author
Message
Funnell7
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 30th Jul 2015 22:47
I remember now why I stopped working on my 2D Top Down Racer (think Micro Machines), I couldn't work out how you would apply lateral(?) force so the car 'drifts' around corners... Well I thought I'd have another go at it, and I can't for the life of me figure it out (even with some extensive googling), I'm in desperate need of some help

I only got as far as controlling the car using Velocity, and altering the direction with the angle;



I wanted to keep it simple by using a single body (i.e. not separate forces per wheel)... Would anyone be happy to help me out with this? Would be eternally grateful

Using AppGameKit V2 Tier 1
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 30th Jul 2015 22:54 Edited at: 30th Jul 2015 22:56
Vectors are your friend

There's a great article on just this subject in "Game Programming Gems vol 4".

It's not cheap but it's definitely worth a read.

AGK V2 user - Tier 1 (mostly)
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 30th Jul 2015 23:02 Edited at: 30th Jul 2015 23:03
you should use
SetSpritePhysicsForce
and
SetSpritePhysicsTorque

for gliding its SetSpritePhysicsFriction

i think apply the force at top at car is best for sliding around corners.

edit the SetSpritePhysicsVelocity direct can have side effects,
means skip the collision or something.

AGK 108 (B)19 + AppGameKit (Steam) V2 Beta .. : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
Funnell7
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 30th Jul 2015 23:27 Edited at: 30th Jul 2015 23:28
I could kick myself sometimes! Setting velocity was indeed the wrong thing to do, I changed it to 'impulse' (and added some damping) and it has made an immediate difference... I will take a look at Force and Torque also, but for now, impulse seems to do the trick...



Thank you so much Marcus, I appreciate it...

Thank you also Scraggle for the book reference...

Using AppGameKit V2 Tier 1
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 31st Jul 2015 00:34 Edited at: 31st Jul 2015 00:38
ok,

impulse is similar you kick a ball once.
a force is better.

Torque u need to turn your car left/right.
this SetSpriteAngle overwrite the physics too.

also interesting is this command.
(now i add the impulse from the sprite center.)
GetWorldXFromSprite
GetWorldYFromSprite

AGK 108 (B)19 + AppGameKit (Steam) V2 Beta .. : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
Conjured Entertainment
AGK Developer
19
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 31st Jul 2015 04:33 Edited at: 31st Jul 2015 04:51
Thanks for sharing guys.
I am going to give the force/torque a try too.

I haven't been using Sin or Cos or Physics for my Monster Smash cars, so my cars don't drift either. (they slide a bit in the mud though)
I have been using Angle/Position, and comparing X Y co-ordinates for collisions.


Coding things my way since 1981 -- Currently using AppGameKit V2 Tier 1
lilpissywilly
AGK Developer
14
Years of Service
User Offline
Joined: 10th Sep 2010
Location: Office Chair
Posted: 1st Aug 2015 00:11
I have an old WIP where I think I have acvieved this. I'm on my phone right now but if you want I can explain how I did it at a later time




My hovercraft is full of eels
Funnell7
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 1st Aug 2015 00:45
@lilpissywilly, that looks great! Would be amazing if you could explain if you find the time, thanks

Using AppGameKit V2 Tier 1
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 1st Aug 2015 01:04
I did something similar years ago in DBPro, without physics. Mine was taken to the extreme as it was spacecraft changing direction.

In short I had 2 vectors. The direction I am facing now, and the direction I was facing before I steered. In each game cycle you take a percentage of each vector and add them together. At the end of each cycle, you turn the "old direction vector" towards the "new direction" vector.

The speed at which you turn the vector determines the speed at which you regain traction. The percentage of each vector you apply determines the drift. So adding 90% of the old vector will be like turning an oil tanker in the ocean. Adding 10% of the old vector would be closer to a car drifting.

The thing I was proud of is that it's the only slightly complex maths I ever worked out by myself!

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
lilpissywilly
AGK Developer
14
Years of Service
User Offline
Joined: 10th Sep 2010
Location: Office Chair
Posted: 1st Aug 2015 10:31 Edited at: 1st Aug 2015 10:53
ok, from memory this, I'll dig up code from an older computer if needed.


I set the car bodies with quite high in both normal damping and angulardamping, I think around 5.0. This means you need to use quite strong forces to move the car, but will prevent it looking like the car is on ice.

I then attached a pair of wheels to the back of the car with their own damping, the front pair is attached to an axle and get the angle from the steering angle. I applied the force running the car in the angle of the front tires. Must be a lot of force due to the physicsdamping.


In short how I made the car drift:
I compared the angles between where the car is going and where it is supposed to be going.


So, I normalized a float between 1.0-0.3 after comparing angles of the car and the angle of where the car is actually travelling. The "traction offset" of 1.0 would mean the car has perfect traction and the car would not drift, for example.

I then applied that float to the cars angulardamping and bodydamping variables, making it lose traction. I also set the same normalized float to the speed of the car in some way, or maybe it was the acceleration, so that the less "traction" you have the slower the car moves.

I also had a check for if the car is on dirt or on the track, and if the car is on dirt the "normalized traction float" would top out at maybe 0.6 instead of 1.0.


I hope this isn't too confusing, I just woke up and it feels like it's confusing to me


edit: the tire tracks are made with the same "normalized traction float", if it's under a certain value, let's say 0.5 (don't remember) black squares are set to where the back wheels are (array of sprites), and the alpha of them is the "normalized traction float" inverted



edit 2, clarification:

Let's say the car is traveling straight up, the wheels are pointed straight, angle 0. The force on the car body is applied at angle 0 because the wheels point that way.

The wheels then point to angle 25 when the player steers right ( the wheels are only allowed +- 20-30 degrees compared to the car body ), the body of the car will start to steer right, but not fast enough for there to be a difference between the angle the force is applied and the angle the body of the car is actually travelling, and the delta between these angles is what I used to make the car lose traction in those instances.

This means the speed of the car will also come into play, as a body travelling slower will be more likely to follow the direction of the force applied, where as a body travelling very fast will carry on going with momentum.

My hovercraft is full of eels
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st Aug 2015 11:29
I actually posted this quite a while ago, by playing with the values you can get realistic drift using physics:
http://forum.thegamecreators.com/?m=forum_view&t=197957&b=41

Using AppGameKit V2 Tier 1
lilpissywilly
AGK Developer
14
Years of Service
User Offline
Joined: 10th Sep 2010
Location: Office Chair
Posted: 1st Aug 2015 13:20
Time flies innit baxy

My hovercraft is full of eels
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 18th Aug 2015 16:48
Hey,
I remembered this post from a few years ago but only just found it again.
You might find some useful info in it.

AGK V2 user - Tier 1 (mostly)
bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:

Login to post a reply

Server time is: 2024-09-29 03:21:00
Your offset time is: 2024-09-29 03:21:00