10-06-2024, 03:06 AM
(This post was last modified: 10-06-2024, 03:07 AM by TerryRitchie.)
Has anyone worked with Bezier curves?
I'm attempting to write a path generator for items such as enemy ships, like when the ships form up in Galaga at the start of a level, or during a challenging stage.
The code below is a quick and dirty Bezier path maker. It works, but no matter what I set the variable Detail to the path comes up jagged, with a sort of stair step effect to it. Does anyone know how I can smooth this out if possible?
I'm attempting to write a path generator for items such as enemy ships, like when the ships form up in Galaga at the start of a level, or during a challenging stage.
The code below is a quick and dirty Bezier path maker. It works, but no matter what I set the variable Detail to the path comes up jagged, with a sort of stair step effect to it. Does anyone know how I can smooth this out if possible?
Code: (Select All)
TYPE POINT
x AS INTEGER
y AS INTEGER
END TYPE
DIM Curve(3) AS POINT
REDIM Path(0) AS POINT
DIM p AS POINT
Curve(0).x = 0
Curve(0).y = 479
Curve(1).x = 639
Curve(1).y = 239
Curve(2).x = 0
Curve(2).y = 239
Curve(3).x = 639
Curve(3).y = 0
SCREEN _NEWIMAGE(640, 480, 32)
Detail = .00001
FOR t = 0 TO 1 STEP Detail
CurvePoint Curve(), p, t
New = -1
FOR i = 0 TO UBOUND(Path) - 1
IF p.x = Path(i).x AND p.y = Path(i).y THEN
New = 0
EXIT FOR
END IF
NEXT i
IF New THEN
Path(UBOUND(Path)) = p
REDIM _PRESERVE Path(UBOUND(Path) + 1) AS POINT
END IF
'PSET (p.x, p.y)
NEXT t
PSET (Path(0).x, Path(0).y)
FOR i = 1 TO UBOUND(Path) - 1
LINE -(Path(i).x, Path(i).y)
NEXT i
PRINT UBOUND(Path)
SUB CurvePoint (c() AS POINT, p AS POINT, t AS SINGLE)
'Quick and dirty Bezier point calculator
'Note: only works for 4 points
p.x = (1 - t) * (1 - t) * (1 - t) * c(0).x + 3 * (1 - t) * (1 - t) * t * c(1).x + 3 * (1 - t) * t * t * c(2).x + t * t * t * c(3).x
p.y = (1 - t) * (1 - t) * (1 - t) * c(0).y + 3 * (1 - t) * (1 - t) * t * c(1).y + 3 * (1 - t) * t * t * c(2).y + t * t * t * c(3).y
END SUB