I made this like 3 months ago, I guess some people want it.
http://yogware.bluegillstudios.com/blogfiles/CarPakke2.zip
By the way there may be some bug in the build / web player, I have no idea what is up with that.
And here is a version of the Wheel.js script with comments.
Show/Hide
var mass = 1.00;
var wheelRadius = 0.00;
var suspensionRange = 0.00;
var suspensionForce = 0.00;
var suspensionDamp = 0.00;
var compressionFrictionFactor = 0.00;
var sidewaysFriction = 0.00;
var sidewaysDamp = 0.00;
var sidewaysSlipVelocity = 0.00;
var sidewaysSlipForce = 0.00;
var sidewaysSlipFriction = 0.00;
var sidewaysStiffnessFactor = 0.00;
var forwardFriction = 0.00;
var forwardSlipVelocity = 0.00;
var forwardSlipForce = 0.00;
var forwardSlipFriction = 0.00;
var forwardStiffnessFactor = 0.00;
var frictionSmoothing = 1.00;
private var hit : RaycastHit;
private var parent : Rigidbody;
parent = transform.root.rigidbody;
private var graphic : Transform;
graphic = transform.FindChild("Graphic");
private var wheelCircumference = 0.00;
wheelCircumference = wheelRadius * Mathf.PI * 2;
private var usedSideFriction = 0.00;
private var usedForwardFriction = 0.00;
private var sideSlip = 0.00;
private var forwardSlip = 0.00;
var car : Car;
var driven = false;
var speed = 0.00;
var brake = false;
var skidbrake = false;
function FixedUpdate ()
{
down = transform.TransformDirection(Vector3.down);
// the object this script is attached to does not move. This ray is like imagining that the shock is compressed as much as possible, and then extends out as far as it can before it hits the ground. If it doesn't, then the wheel is fully extended.
if(Physics.Raycast (transform.position, down, hit, suspensionRange + wheelRadius) && hit.collider.transform.root != transform.root)
{
// how fast is the wheel moving relative to the ground, without taking the wheel's rotation into account
velocityAtTouch = parent.GetPointVelocity(hit.point);
// calculate the force of the shock as it pushes on the car and the earth due to compression. Since the earth does not move, this is an upward force on the car only
compression = hit.distance / (suspensionRange + wheelRadius);
compression = -compression + 1;
force = -down * compression * suspensionForce;
// Here we set t equal to the speed at which the shock is contracting / expanding
t = transform.InverseTransformDirection(velocityAtTouch);
t.z = t.x = 0;
// this force simulates the force exerted by the friction in the suspension.
shockDrag = transform.TransformDirection(t) * -suspensionDamp;
// the difference between the speed of the ground and the wheel taking rotation + the car's velocity into account. (in local space, that means, relative to the wheel. So if you were standing on the wheel surface, how fast would you percieve the ground moving? Either squashing you every time the wheel goes around (not skidding, absolute value close to zero) or scrapping you to bits (skidding, large absolute value) )
forwardDifference = transform.InverseTransformDirection(velocityAtTouch).z - speed;
//Ok, this next part is not related to real physics an any way whatsoever. Ummm... Basically the friction that the wheel has with the ground changes (using a lerp function over time) depending on the current friction force. I guess this simulates how once a car starts skidding, the friction goes down so it skids more.
// __________________z-friction__________________
// move the current working friction value toward the minimum about porportional to the "skidding-ness" squared
newForwardFriction = Mathf.Lerp(forwardFriction, forwardSlipFriction, forwardSlip * forwardSlip);
// increase the current working friction value depending on how hard the shock is pressing the wheel against the ground
newForwardFriction = Mathf.Lerp(newForwardFriction, newForwardFriction * compression, compressionFrictionFactor);
// smooth the friction value
if(frictionSmoothing > 0)
usedForwardFriction = Mathf.Lerp(usedForwardFriction, newForwardFriction, Time.fixedDeltaTime / frictionSmoothing);
else
usedForwardFriction = newForwardFriction;
// calculate one component of the friction force: the difference between the wheel surface velocity and ground surface velocity times friction (not based on real physics AFAIK)
forwardForce = transform.TransformDirection(Vector3(0, 0, -forwardDifference)) * usedForwardFriction;
// calculate how much we be slippin (if the force is high, the tire will give and slip on the road)
forwardSlip = Mathf.Lerp(forwardForce.magnitude / forwardSlipForce, forwardDifference / forwardSlipVelocity, forwardStiffnessFactor);
// ____________________________________
// this is much the same as the block above, but for the x-axis
// __________________x-friction__________________
sidewaysDifference = transform.InverseTransformDirection(velocityAtTouch).x;
newSideFriction = Mathf.Lerp(sidewaysFriction, sidewaysSlipFriction, sideSlip * sideSlip);
newSideFriction = Mathf.Lerp(newSideFriction, newSideFriction * compression, compressionFrictionFactor);
if(frictionSmoothing > 0)
usedSideFriction = Mathf.Lerp(usedSideFriction, newSideFriction, Time.fixedDeltaTime / frictionSmoothing);
else
usedSideFriction = newSideFriction;
sideForce = transform.TransformDirection(Vector3(-sidewaysDifference, 0, 0)) * usedSideFriction;
sideSlip = Mathf.Lerp(sideForce.magnitude / sidewaysSlipForce, sidewaysDifference / sidewaysSlipVelocity, sidewaysStiffnessFactor);
// ____________________________________
// this thing is totally made up. It's as if god's hand nudges the car back on course whenever it is moving sideways, by a factor of sidewaysDamp
t = transform.InverseTransformDirection(velocityAtTouch);
t.z = t.y = 0;
sideDrag = transform.TransformDirection(t) * -sidewaysDamp;
// By the some of all you combined, I become CAPTAIN FORCE
parent.AddForceAtPosition(force + shockDrag + forwardForce + sideForce + sideDrag, hit.point);
// for every action there is an opposite reaction: the wheel adds a force on the ground, but the ground does not move, so that force is added to the car instead in the opposite direction (this lets the engine propel the thing forward). But!!! we also have to add a force on the engine, because otherwise we'd be adding force from nothing. The opposite reaction is this force on the motor. The motor is not a rigidbody, it is a made up thing in the Car.js script
if(driven)
car.AddForceOnMotor (forwardDifference * usedForwardFriction * Time.fixedDeltaTime);
else
// if this wheel isn't attached to the motor, just add the force to the wheel instead
speed += forwardDifference;
graphic.position = transform.position + (down * (hit.distance - wheelRadius));
}
else
{
graphic.position = transform.position + (down * suspensionRange);
}
graphic.transform.Rotate(360 * (speed / wheelCircumference) * Time.fixedDeltaTime, 0, 0);
}
function OnDrawGizmos () {
Gizmos.color = Color.yellow;
Gizmos.DrawRay (transform.position, transform.TransformDirection (Vector3.up) * -wheelRadius);
}



35 comments:
Very Usable demo ;-). Thanks for sharing
Thanks for the demo, Forrest. I am a college sophomore with a dual major in Physics and Mathematics @ University of California, Santa Barbara. By the way, i came across these excellent physics flash cards. Its also a great initiative by the FunnelBrain team. Amazing!!
Vincent, I can't tell if you are an honest person who made a mistake or a spammer - your link points to funnelbrain.com, it doesn't point to flash cards.
Don't spam!!!! If you do I will track you down and destroy your car and / or home.
hmmm,
I posted this link as an answer to my problem on 29 December.
http://answers.unity3d.com/questions/105/vehicle-programming-source/2042#2042
Sorry for the trouble I caused with people that are start to spam!
Peter.
i am regular visitor of your site and you are writing very nice so keep posting university admission
many fendi products for discount
go to buy fendi handbags on sale
fendi spy bag fendi handbags
get your gucci set at cheap price
gucci gucci replica all so very good
choose gucci handbags high quality
new arrive miu miu handbags miu miu bags
beauful miu miu bags on sale
large numbers of miu miu sale
chanel designed the famous suit
authentic chanel handbags on this
looking for cheap chanel bags chanel
gucci bags luxury bags louis vuitton bag
-----------
as a luxury consumable luxury handbags sale
good quality lv bags online store
louis vuitton bags remains one of the world
carried on the louis vuitton luggage the body
looking for cheap louis vuitton handbags handbags
all kind of louis vuitton bags for sale
you need louis vuitton wallet to put money
luxury louis vuitton bags for sale
luxury louis vuitton louis vuitton bags
have large numbers of louis vuitton shoes discount
louis vuitton bag louis vuitton handbag for discount
discount louis vuitton louis vuitton discount
have louis vuitton shoes is symbol of fashion
this louis vuitton shop sale louis vuitton
have a stylish lv handbags is every women dream
a pair of louis vuitton shoes becoming fashion
in the iwc watches store
Dunno why I had to make a second wordpress account herve leger to leave a comment to your blog but Christian Louboutin anyways xD
Thx for the responses. herve leger bandage dress The “vuvuzela-powered lazer beam” thing Hermes Handbag and your last answer made me laugh Hermes Birkin out loud
I wanted to ask you this Monclerfor quite some time now. Moncler Jackets I would herve leger dresses appreciate it if you replied. What’s actually harder, Christian Louboutin Boots making a record or trying to Herve Leger Strapless name it?
Nice stuff you got, very interesting to read. My stepfather who likes to get Generic Viagra told me about this which it's cool.
Forest is nothing short of a genius...
Call Cab Minneapolis is your best choice for cab and limousine service in the Minneapolis/St.Paul and the Twin Cities area. We have been providing exceptional cab and limousine service for over 15 years. We provide a one-call solution for all your transportation needs.Car service Minneapolis
I wish the Truckers still came in this color. It was the most pleasing Long Haul Trucker color to date.
Velocity Wheels
Almost every girl seeking sexy, fashion, beauty, luxury, noble.As herve leger luxury brands, Herve Leger to meet almost every girl's high requirements.The herve leger dress bandage dress with scoop neck and back And from a mixture of cotton and linen materials, it has spread.
Thousands of Factory Audited China Suppliers, China Manufacturers, China Products are seeking Trusted Importers and Exporters on tradetuber.com .wholesale lingerie | wholesale costume | clubwear wholesale | wholesale swimwear | wholesale corset | wholesale Panties
Media player | Hd player | HDD player | Blu ray player | Android player
Buy $10 Replica Designer Sunglasses with 3-day FREE SHIPPING. At fashion-world4u you find Imitation ,Inspired ,MEN designer Sunglasses and Women Replica Sunglass at cheap discount price. Sunglasses | Replica Sunglasses | Sunglass | Designer Sunglasses
Best Quality Magnetic Jewelry and Magnet Therapy Products - Over 1300 Items that are good for Health as well as Beauty - Free Worldwide Shipping. magnetic jewelry | magnetic therapy | magnetic products | magnetic bracelets | beaded bracelet | bead bracelet
LED Dimming System Supplier -Thousands of Factory Audited China Suppliers, China Manufacturers, China Products are seeking Trusted Importers and Exporters on tradetuber.com
Blu ray player -Thousands of Factory Audited China Suppliers, China Manufacturers, China Products are seeking Trusted Importers and Exporters on tradetuber.com
Blu ray player -Thousands of Factory Audited China Suppliers, China Manufacturers, China Products are seeking Trusted Importers and Exporters on tradetuber.com
As such, abounding dealers are now alms appropriate car deals, abnormally if it comes to lending, that they would not acquire contrarily offered.
Second Hand Cars in Bangalore
Coach Outlet Online is a good store!supsit
Coach Factory Outlet
Coach outlet online Leading American designer and maker of luxury lifestyle handbags and accessories.Large market in Europe and Canada,UK,USA etc.Welcome to Order!2011 New Style arrive,Free Shipping!
As a Coach Factory Outlet Online, you get paid several ways. The primary way is resale - here is where you sell a product thru your company's Coach Factory Outlet Online. The 2nd way is membership sale - you can sell a club membership and you'll get paid unearned income each time the customer renews their Coach Factory Outlet Online.
You will never buy fashion and cheap Coach Handbags. These benefits, Coach Factory Outlet are the best shopping place for most women. They are the first choice for women.
Your success is my inspiration.Indeed, it is wonderful !!
the article is interesting Coach Factory Outlet although there is a small mistake in the chart.
the article is very interesting..Good!!thank you!!
Coach Outlet Online Build a network monitoring long-term mechanism of action to rectify the vigorous national network finally come to an end.
Making sure your legs are covered is just the first step on Coach Outlet the exciting road to choosing the right morning dress trousers for you.
Phony custom made designer bags outsell the original elements in China and tiawan with a staggering percentage of more than 20:
Phony custom made designer bags outsell the original elements in China and tiawan with a staggering percentage of more than 20:
Coach Outlet Online maybe a good handbag store, that I see women carrying in every city that I visit.
Yawn! Zzzzz... Really its awesome.... I really like your blog. Thanks very much for share
Post a Comment