Just as with the circle3d command which I posted a few days ago, here is some code to draw a 3d ellipse. Cloggy's plugin is the d3dfunc plugin that appears in the program announcements board. The code below will be replaced by a native command in the next version of the plugin.
There is a more elegant way of drawing a 3d ellipse using two foci vectors rather than the sin/cos parametric equations I've used but I quite like this method. Its got soul.
Rem PJY - the picnic basket obsessed bear's magic 3d ellipse code
Rem PJY - (with thanks to Danny Gregory for his comments on the 3d circle command)
sync on
sync rate 60
autocam off
d3d_init
Rem PJY - make a matrix to act as a visual reference point
make matrix 1, 1000, 1000, 100, 100
position matrix 1, -500, 0, -500
Rem PJY - stick the camera somewhere useful
position camera 12.5, 20, -40
point camera 0, -30, 20, -10
Rem PJY - create a completely random normalised vector
null = make vector3(1)
set vector3 1, 100, 50, 30
normalize vector3 1, 1
nx# = x vector3(1)
ny# = y vector3(1)
nz# = z vector3(1)
null = delete vector3(1)
Rem PJY - define length and width of the 3d ellipse
length = 2
width = 3
angle = 135
repeat
text 0, 0, "FPS : " + str$(screen fps())
text 0, 30, str$(camera position x(0)) + " " + str$(camera position y(0)) + " " + str$(camera position z(0))
Rem PJY - ellipse3d(length, width, x, y, z, nx, ny, nz)
Rem PJY - length and width are the lengths of the two semi-major axes (i.e. the local x and y axes of the ellipse)
Rem PJY - angle is difficult to explain, hence the angle = angle + 1 code below which demonstrates it
Rem PJY - the centre point coordinates of the ellipse are x = -30, y = 10, z = -10
Rem PJY - and feed in the normalised vector of the plane on which the ellipse sits (nx, ny and nz)
ellipse3d(length, width, angle, -30, 20, -10, nx#, ny#, nz#)
angle = angle + 1
angle = wrapvalue(angle)
control camera using arrowkeys 0, 2, 1
sync
until spacekey() > 0
delete matrix 1
end
FUNCTION ellipse3d(length as float, width as float, angle AS float, x as float, y as float, z as float, nx as float, ny as float, nz as float)
Remstart
length is the length of one of the semi-major axis of the ellipse
width is the length of the other semi major axis of the ellipse
angle is, well, difficult to explain. :-)
x, y and z are the centre coords for the ellipse
nx, ny and nz are the dimensions of the vector that define the normal of
the plane on which the ellipse resides
remend
null = make vector3(1)
null = make vector3(2)
null = make vector3(3)
null = make vector3(4)
null = make matrix4(5)
Rem PJY - set vector3 1 to be the normalised vector
set vector3 1, nx, ny, nz
Rem PJY - calculate distance co-efficient of plane equation
distance# = x * nx + y * ny + z * nz
Rem PJY - We need a known point on the plane (this can be our ellipse center)
set vector3 2, x, y, z
Rem PJY - find a vector that coincides with our plane by projecting a random point on to the plane and
Rem PJY - subsequently subtracting the projected point by a known point on the plane
set vector3 3, x vector3(2) + 100, y vector3(2) + 100, z vector3(2) + 100
dis# = dot product vector3(1, 3) - distance#
set vector3 4, x vector3(3) + nx * -dis#, y vector3(3) + ny * -dis#, z vector3(3) + nz * -dis#
Rem PJY - Calculate vector from the two points on the plane to represent the length vector
subtract vector3 3, 4, 2
normalize vector3 3, 3
Rem PJY - now transform
angle# = angle
angle# = (angle# / 180.0) * 3.1415
build rotation axis matrix4 5, 1, angle#
transform coords vector3 3, 3, 5
cross product vector3 2, 1, 3
multiply vector3 3, length
Rem PJY - create new vector to be the width
normalize vector3 2, 2
multiply vector3 2, width
for angle = 1 to 360
Rem PJY - use the parametric equation of a 3d ellipse:
Rem PJY - namely: Px = Cx + a*ux*cos(angle) + b*vx*sin(angle) etc
x_ellipse# = x + (width * x vector3(2) * cos(angle)) + (length * x vector3(3) * sin(angle))
y_ellipse# = y + (width * y vector3(2) * cos(angle)) + (length * y vector3(3) * sin(angle))
z_ellipse# = z + (width * z vector3(2) * cos(angle)) + (length * z vector3(3) * sin(angle))
d3d_dot3d x_ellipse#, y_ellipse#, z_ellipse#, 3, rgb(255, 0, 0), 1
next angle
Rem PJY - create a lovely green line to represent a somewhat longer version of the normalised vector of the ellipse
d3d_line3d x, y, z, x + nx, y + ny, z + nz, rgb(0, 255, 0), 1
null = delete vector3(1)
null = delete vector3(2)
null = delete vector3(3)
null = delete vector3(4)
null = delete matrix4(5)
ENDFUNCTION
Cheer if you like bears! Cheer if you like jam sandwiches!
Quote of the week: "... I started learning DBP while I was a Satellite Network Controller for the US Army Space Command ... "