iT邦幫忙

2024 iThome 鐵人賽

DAY 20
0
Mobile Development

用Flutter Flame做遊戲!Live!系列 第 22

不規則的牆(Wall:BodyComponent)

  • 分享至 

  • xImage
  •  

Forge2D的基本範例中設置了一個類別稱為「Wall」,可以用來限制因為物理引擎而往下掉的「Ball」。
第一眼看到這個範例時,我的錯誤詮釋是「在Forge2D中要使用EdgeShape來限制World邊界」,但這是錯的,「在Firge2D中,地板這類的元素也是BodyComponent,必須自行設置。」

所以,該來認真研究一下「地板」「牆」「天花版」等「邊界」的設計方式了!(「教練,我想玩Canvas。」)


這裡有幾個細節要特別注意和滿足:使用滑鼠游標或手指拖曳時,碰撞偵測效果會失靈或運作異常,所以「必要時必須自己設置偵測條件(避免物件被滑移動到Wall外面)」
(很遺憾,寫這篇文章時,這項功能並未完。)

先試著製作一個「圓型的牆」吧!
但是要注意!直接製作一個圓形的Component然後把其餘物件包在裡面,看似簡單直覺,但有個根本上不可行的問題,但先不討論,先研究怎麼製作「不規則形狀」吧!


final shape = PolygonShape();

// Define the arc
double radius = 5;
double startAngle = 0;
double endAngle = pi / 2;
int numSegments = 10;

// Generate the vertices of the polygon
List<Vector2> vertices = List.generate(numSegments, (i) {
  double angle = startAngle + i * (endAngle - startAngle) / (numSegments - 1);
  return Vector2(radius * cos(angle), radius * sin(angle));
});

shape.set(vertices);

這是個「不規則形狀」的「Shape」。(如果有注意到會發現「這不是Path嗎?」)
有這個就可以設計不規則形狀的邊界了。

(但...這不是「圓弧形」啊!看來結論是「此路不通」。)


上一篇
CameraComponent
下一篇
將多個Component連結起來的Joint
系列文
用Flutter Flame做遊戲!Live!26
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言