Chapter 19: Beginning Game Physics

前言

這個新的app,主要是建立一個複雜的AR模型車,並利用SceneKit內建提供的物理引擎,讓整個車子的移動更加真實。

大綱

Getting started

簡單介紹這個app大概會做到哪些功能。

  • 基本ARKit的session管理

  • 平面偵測

  • 遊戲邏輯方面分成三個steps

    • .detectSurface: 偵測平面

    • .hitStartToPlay: 置放車子

    • .playGame: 可以輕觸螢幕讓車子加速,然後透過手機物理感測的左右控制車子輪子方向

The vehicle parts

在模型的部分,提供了車子主體,輪子等,需要自己組合成車子

Building the monster truck

接下來就花一節的篇幅介紹要如何組起來,我自己是對這塊沒太大興趣,所以就跳過這塊了。

Loading the monster truck

將組合好的模型讀取到程式中。

Adding vehicle physics

這裡就是重頭戲,如何添加物理引擎,在Scenekit已經有內建的車子的行為模式,所以接下來就是要懂如何用。

在車子這塊主要有三項物理行為

  • SCNPhysicsVehicle: This is a physics behavior that will modify a standard physics body to behave like a vehicle.

  • SCNPhysicsBody: This is a normal stock standard physics body that will be used as the vehicle’s chassis body. This body will be provided to the SCNPhysicsVehicle during its creation.

  • SCNPhysicsVehicleWheel: This is a special physics body type that simulates the behavior of a wheel and also its appearance and other physical characteristics. This body will also be provided to the SCNPhysicsVehicle during its creation.

Creating the wheel physics

先從輪子下手

Creating the vehicle physics

下一步,就是vehicle physics

Updating the truck’s position

根據添加的物理行為,來更新車子移動位置

Last updated