I figured out how to draw a simple bezier curve!
I don’t know why the ’ CubicCurveseg’ class doesn’t seem to work:
CubicCurveseg class for Bezier curves
I still got Bezier to display on screen using ‘LineSegs’
#!/usr/bin/env python3
from direct.showbase.ShowBase import ShowBase
from panda3d.core import LineSegs
from math import comb
class MyApp(ShowBase):
def __init__(self):
ShowBase.__init__(self)
# Set up the camera
self.setup_camera()
# Create a LineSegs object
lines = LineSegs()
# Set the color of the curve (red)
lines.set_color(1, 0, 0, 1)
# Define the control points of the cubic Bezier curve
control_points = [
(0, 0, 0), # Start point
(1, 0, 0), # First control point
(0, 1, 0), # Second control point
(1, 1, 0) # End point
]
# Set the number of subdivisions for the curve
num_subdivisions = 100
# Draw the curve
for i in range(num_subdivisions + 1):
t = i / num_subdivisions
point = self.evaluate_cubic_bezier(control_points, t)
lines.draw_to(point)
# Create a NodePath to attach the curve geometry
curve_np = self.render.attachNewNode(lines.create())
# Set the line thickness
curve_np.setRenderModeThickness(3)
def evaluate_cubic_bezier(self, control_points, t):
n = len(control_points) - 1
result = [0, 0, 0]
for i, point in enumerate(control_points):
coeff = comb(n, i) * (1 - t)**(n - i) * t**i
for j in range(3):
result[j] += coeff * point[j]
return tuple(result)
def setup_camera(self):
self.disableMouse()
camera.setPos(0, 0, 10)
camera.lookAt(0, 0, 0)
app = MyApp()
app.run()