查看: 2103|回复: 7
打印 上一主题 下一主题

如何使用unity3d制作出真实的物理效果(三)

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2011-11-12 11:32:00 |只看该作者 |倒序浏览


           Now the fun part...
         

            
         

           Q: How to simulate stabilizer bars in Unity?
         

           A: Easily
         

           Anti-roll bars work by transferring some compression force from one spring to the opposite in the same axle. The amount of the transfered force depends on the difference in the suspension travel among the wheels.
         

           So first task is to calculate the suspension travel on each wheel, as well as determine whether is grounded or not. We want the travel value to be between 0.0 (fully compressed) and 1.0 (fully extended):
         

           code:
         

           groundedL = WheelL.GetGroundHit(hit))
         

           if (groundedL)
         

               travelL = (-WheelL.transform.InverseTransformPoint(hit.point).y - WheelL.radius) / WheelL.suspensionDistance;
         

           else
         

               travelL = 1.0;
         

           We multiply the travel diference by the AntiRoll value, which is the maximum
         

           force that the anti-roll bar can transfer among the springs (we could call this
         

           the Anti-roll bar's stiffness). This yields the amount of force that will be
         

           transfered:
         

           code:
         

           var antiRollForce = (travelL - travelR) * AntiRoll;
         

           Finally, we must simply substract the force from one spring and add it to the
         

           other. We achieve this by adding opposite forces to the rigidbody at the
         

           positions of the WheelColliders:
         

           code:
         

           if (groundedL)
         

               rigidbody.AddForceAtPosition(WheelL.transform.up * -antiRollForce, WheelL.transform.position);
         

           if (groundedR)
         

               rigidbody.AddForceAtPosition(WheelR.transform.up * antiRollForce, WheelR.transform.position);
         

           Q: What's a good AntiRoll value? Which units is it expressed in?
         

           A: A good  value is roughly the value of the wheel's spring. Both are expressed in Newtons.
         

           The spring value means the force the spring can give when fully
         

           compressed, and the AntiRoll value means the amount of force that can be
         

           transfered from one spring to another. Having the same values for Springs and
         

           AntiRoll in the same axle means that the anti-roll bar can transfer up the
         

           entire force of one spring to the other.
         

           Q: Can I have the script  for an anti-roll bar?
         

           A: Sure, here is it.
         

           As suggestion, I
         

           believe that an Anti-Roll script should be included in Unity's Standard
         

           Assets.
         

           Add two anti-roll scripts to your vehicle, one per axle (front -
         

           rear). Set the left-right wheels on each one and adjust the AntiRoll value.
         

           Remember to reset center of mass to the real position, or don't touch it at
         

           all!
         

           The full script: AntiRollBar.js
         

           code:
         

           var WheelL : WheelCollider;
         

           var WheelR : WheelCollider;
         

           var AntiRoll = 5000.0;
         

           function FixedUpdate ()
         

               {
         

               var hit : WheelHit;
         

               var travelL = 1.0;
         

               var travelR = 1.0;
         

               var groundedL = WheelL.GetGroundHit(hit);
         

               if (groundedL)
         

                   travelL = (-WheelL.transform.InverseTransformPoint(hit.point).y - WheelL.radius) / WheelL.suspensionDistance;
         

               var groundedR = WheelR.GetGroundHit(hit);
         

               if (groundedR)
         

                   travelR = (-WheelR.transform.InverseTransformPoint(hit.point).y - WheelR.radius) / WheelR.suspensionDistance;
         

               var antiRollForce = (travelL - travelR) * AntiRoll;
         

               if (groundedL)
         

                   rigidbody.AddForceAtPosition(WheelL.transform.up * -antiRollForce,
         

                          WheelL.transform.position);
         

               if (groundedR)
         

                   rigidbody.AddForceAtPosition(WheelR.transform.up * antiRollForce,
         

                          WheelR.transform.position);
         

               }
         

           Q: How can I set a real center of mass for my vehicle?
         

           A: Use  non-overlapping box colliders to roughly resemble your vehicle's shape, then  move the center towards the position of the engine.
         




            
         

           Unity/PhysX calculates the center of mass based on the position and
         

           volume of the GameObject's colliders. Note that overlapping colliders add more
         

           mass at the overlap position.
         

           Typically, you would only need to move the
         

           center of mass a bit towards the position of the engine (front - rear). For
         

           instance, if your vehicle has front engine and its front is oriented in the Z+
         

           direction, you should only need to move the (automatically calculated) center of
         

           mass 1 meter to the front:
         

           code:
         

           function Start ()
         

               {
         

               rigidbody.centerOfMass += Vector3(0, 0, 1.0);
         

               }
         

           Q: Does it really works so good? Could it really be so simple??
         

           A: See it  by yourself:
           
            http://www.edy.es/unity/offroader.html
           


           You can have
         

           a good view of how it works by pressing C (change secondary camera), then
         

           pressing B while driving (alternate Sport / Offroad mode). Press N for
         

           completely disabling the stabilizer bars (B to reenable, or Enter to restart
         

           after flipping over).
         

           Ending words....
         

           As this is my first post here, please let me introduce myself. I'm a developer now working on dj software. Years ago, while I was a student two friends and me spent about two years trying to develop games in C++ from scratch (no engines at all), because games was what we wanted to do since kids. As you can imagine, very little to no result could be seen in the screen. I discovered Unity about four weeks ago (had heard of it before, but hadn't look at it) and for me it was like seeing The Light. I could just put things on the screen, click play, and worked! I could then fine-tune the scene by looking at code examples and writing scripts in whatever language used by Unity (two weeks ago I realized I was coding in javascript). Magic! Now I'm about to achieve a personal goal of modeling physically real vehicles. This is what I really wanted to do since I was kid and self-learned to code in Basic with by Commodore 64. Overlander, Powerdrift, Hard Driving, and later 4D Sports Driving, Fatal Racing... now I can easily do it!! Even as hobbyist, I'm purchasing the Unity Pro license just because I love it!
         

           This article is my first contribution to the GREAT Unity community. It's great to have fun by modeling and playing with Unity's vehicle physics.
         

           Enjoy!
         

           Edy
         
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

沙发
发表于 2012-2-3 23:25:06 |只看该作者
我就看看,我不说话
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

板凳
发表于 2012-3-30 23:19:32 |只看该作者
不错啊 经典
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2012-6-23 23:18:08 |只看该作者
俺是新人,这厢有礼了!
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2012-7-13 23:20:40 |只看该作者
水……生命之源……灌……
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

6#
发表于 2012-10-16 23:19:52 |只看该作者
顶!学习了!阅!
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

7#
发表于 2012-10-30 23:18:58 |只看该作者
心中有爱,爱咋咋地
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

8#
发表于 2013-1-27 23:27:09 |只看该作者
发了那么多,我都不知道该用哪个给你回帖了,呵呵
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-24 04:42 , Processed in 0.115724 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部