来源

Mosiv的语雀笔记:《全方位抛物线》

原文

x-y平面绕 x轴, 旋转30°得到旋转后平面p1, a点投影在p1平面,
其坐标Vector(x,y,z) (x,y为抛物线在2D平面坐标, z为抛物线高度), x不变, y变化
H = (z - (ytan30°)) * sin30°+ (y / cos30°)
得到变换后 a点 坐标 Vector( x , H )
全方位抛物线 - 图1
代码:

  1. extends Path2D
  2. var trans_points :PoolVector2Array = []
  3. var points :PoolVector2Array = []
  4. # 抛物线平面方向
  5. var dirction = Vector2.RIGHT
  6. # 平面旋转角度
  7. var plane_rotate_rad :float= -PI/3
  8. var start_speed_vertical = 100
  9. var speed_hotizontal = 20
  10. var speed_vertical = start_speed_vertical
  11. var gravity := -10.0
  12. var timer := 0.0
  13. var is_over := false
  14. func _process(delta):
  15. if is_over :return
  16. for i in range(8):
  17. caculate(dirction.rotated(PI/4 * i),delta)
  18. # 计算位置
  19. func caculate(direction,delta):
  20. timer += delta
  21. var vertical_heigt = start_speed_vertical*timer + (gravity * timer * timer )/2
  22. var horizontal_position = direction * speed_hotizontal * timer
  23. var trans_pos = trans(Vector3(horizontal_position.x,horizontal_position.y,vertical_heigt))
  24. trans_points.append(trans_pos)
  25. points.append(horizontal_position)
  26. update()
  27. if vertical_heigt <= 0:
  28. is_over = true
  29. print("over")
  30. # 绘制
  31. func _draw():
  32. if points.size() < 2:return
  33. draw_polyline(points,Color.red,5.0)
  34. draw_polyline(trans_points,Color.chartreuse,5.0)
  35. # 变换坐标
  36. func trans(pos:Vector3)->Vector2:
  37. var x = pos.x
  38. var y = (pos.z - (pos.y*tan(plane_rotate_rad)))*sin(plane_rotate_rad) + (pos.y / cos(plane_rotate_rad))
  39. return Vector2(x,y)