Help with spheres

BlitzMax Forums/BlitzMax Beginners Area/Help with spheres

Hi there,

I'm trying to create a sphere that i can rotate around. I think i'm heading in the right direction but i've hit a wall of sorts. The program is not drawing a sphere but an oval.

If anyone knows whats going wrong then let me know.

Local width=640,height=480

Graphics width,height,0,32

Type point
	Field x#,y#,z#,r#,a1#,a2#
End Type

Local x#,y#

Local pointList:TList=New TList
Local p2:point=New point
For n=0 To 3
	
	For i=0 To 35
		
		Local p:point=New point
		
		pointList.AddLast p
		
		p.a1=i*10
		p.a2=n*45
		p.r=1.0
		
		p.x=p.r*(Cos(p.a1))*(Sin(p.a2))
		p.y=p.r*(Sin(p.a1))*(Cos(p.a2))
		p.z=p.r*Cos(p.a2)
	
	Next
	
Next

While Not KeyHit(key_escape)
	
	Cls()
	
	For p:point=EachIn pointList
	
		If KeyDown(key_up) p.a1:+0.5
		If KeyDown(key_down) p.a1:-0.5
		If KeyDown(key_left) p.a2:-0.5
		If KeyDown(key_right) p.a2:+0.5
		
		If p.a1>=359.5 p.a1=0.0
		If p.a1<0.0 p.a1=359.5
		If p.a2>=359.5 p.a2=0.0
		If p.a2<0.0 p.a2=359.5
		
		p.x=p.r*(Cos(p.a1))*(Sin(p.a2))
		p.y=p.r*(Sin(p.a1))*(Cos(p.a2))
		p.z=p.r*(Cos(p.a2))
		
		x=(width/2.0)+(p.x/p.z)*150
		y=(height/2.0)+(p.y/p.z)*150
		
		SetScale 1.5/p.z,1.5/p.z
		SetColor 255,255,255
		DrawOval x,y,1,1
		
	Next
	Flip(0)
	
Wend



Cheers
Charlie

try setting the drawoval using only the height variable, by using the width as well youre stretching it.

sorry, i don't understand what you mean. Could you explain.

Cheers
Charlie

@Smiler,

Thats what I thought until I actualy read the whole code

@jk,

Smiler made a mistake.

No one has a clue on this then?

Cheers
Charlie

I don't understand the 3d math so I can't really help you that way but I hope this program can help you:

'         name; 3D ROTATING OBJECT                   Date; Unknown Date           
'  Author; Joshua Dickerson                          
'  Origin; FidoNet QUIK_BAS Echo 
'  modified for bmax by Jesse Perez           
'===========================================================================


AppTitle = "ROTATING OBJ"
Type Tnode
	Field x#
	Field y#
	Field z#
End Type

Global  node:tnode
Global  X, Y, Z, sX#, sY#,sXs#, sYs#, D#
Global  AngleAxisz#, AngleAxisx#, AngleAxisy#, mZ#
Global  sinAngleAxisz#,cosAngleAxisz#, cosAngleAxisx#, sinAngleAxisx#, sinAngleAxisy#, cosAngleAxisy#
Global nodeList:Tlist
RestoreData FACE 
Local nodes
ReadData nodes

For I = 1 To nodes 
	node= New tnode
	If Not nodeList nodelist = CreateList()
	nodelist.addlast(node)
	ReadData node.x,node.y,node.z
Next
InitProgram()
MainLoop()
End 

Function Calc3D()

	X = -1 * X; 
	Local Xa# = cosAngleAxisz * X - sinAngleAxisz * Z; 
	Local Za# = sinAngleAxisz * X + cosAngleAxisz * Z
	Local Ya# = cosAngleAxisx * Y - sinAngleAxisx * Xa;
	X = cosAngleAxisx * Xa + sinAngleAxisx * Y; 
	Z = cosAngleAxisy * Za - sinAngleAxisy * Ya
	Y = sinAngleAxisy * Za + cosAngleAxisy * Ya;
	Z = Z + mZ; 
	sX = D * X / Z
	sY = D * Y / Z

End Function

Function DrawObject()

	Rotateobject
	Local I = 1
	For node  = EachIn nodeList
		X = node.x
		Y = node.y
		Z = node.z
		Calc3D
		If  Not (i Mod 2) DrawLine sXs, sYs , sX, sY 'get start and end points before drawing.
		sXs = sX; sYs = sY
		I = I+1
	Next
End Function

Function  InitProgram()

	Graphics 640,480
	SetOrigin 320, 240
	SetColor 255,0,0
	D# = 500      'View point And rotation values
	mZ# = -200	  'shift from z axis away from viewpoint
	AngleAxisz# = 0 'spin along y axis; angle of z
	AngleAxisx# = 0 'spin along z axiz; angle of x
	AngleAxisy# = 0 'spin along x axis; angle of y

End Function 

Function  MainLoop()

While Not KeyDown(KEY_ESCAPE)
  Cls
  'AngleAxisz = AngleAxisz + 1; If AngleAxisz > 360 Then AngleAxisz# = 0
  AngleAxisx = AngleAxisx + 1; If AngleAxisx > 360 Then AngleAxisx# = 0
  AngleAxisy = AngleAxisy + 3; If AngleAxisy > 360 Then AngleAxisy = 0
  DrawObject
  Flip
Wend

End Function

Function Rotateobject()

	sinAngleAxisz = Sin(AngleAxisz)
	cosAngleAxisz = Cos(AngleAxisz)
	sinAngleAxisx = Sin(AngleAxisx)
	cosAngleAxisx = Cos(AngleAxisx)
	sinAngleAxisy = Sin(AngleAxisy)
	cosAngleAxisy = Cos(AngleAxisy)

End Function
#CUBE
'LOOKING THINGY
DefData 24
DefData -90,-90,-90, -90,-90,90,     -90,-90,90,    90,-90,90
DefData  90,-90,90,   90,-90,-90,     90,-90,-90,  -90,-90,-90
DefData -90,90,-90,  -90,90,90,      -90,90,90,    90,90,90
DefData  90,90,90,    90,90,-90,      90,90,-90,  -90,90,-90
DefData -90,90,-90,  -90,-90,-90,    -90,-90,90,  -90,90,90
DefData  90,90,90,    90,-90,90,      90,-90,-90,  90,90,-90
DefData 26
DefData -90,0,-90,   -90,0,90,       -90,0,90,    90,0,90
DefData  90,0,90,     90,0,-90,       90,0,-90,  -90,0,-90
DefData -90,0,-90,    0,90,0,        -90,0,90,    0,90,0
DefData  90,0,90,     0,90,0,         90,0,-90,   0,90,0
DefData -90,0,-90,    0,-25,0,       -90,0,90,    0,-25,0
DefData  90,0,90,     0,-25,0,        90,0,-90,   0,-25,0
DefData  0,-25,0, 0,90,0
'
'EMPEROR ANDROSS (STAR FOX)
#FACE 


DefData 142
'
'~~~~~~~~~~~~~~~~~~~~~~~~~NOSE~~~~~~~~~~~~~~~~~~~~~~~~~~
DefData  5,-4,10,    -5,-4,10,       -5,-4,10,   -5,-25,10
DefData -5,-25,10,    0,-52,0,        0,-52,0,    5,-25,10
DefData  5,-25,10,    5,-4,10,        5,-25,10,   10,-18,2
DefData  10,-18,2,    10,2,2,         10,2,2,     5,-4,10
DefData  10,2,2,     -10,2,2,        -10,2,2,    -5,-4,10
DefData -10,2,2,     -10,-18,2,      -10,-18,2,  -5,-25,10
'
'~~~~~~~~~~~~~~~~~~~~~~~~~MOUTH~~~~~~~~~~~~~~~~~~~~~~~~~
DefData -10,2,2,     -28,26,0,       -10,2,2,     0,14,5
DefData  0,14,5,      10,2,2,         28,26,0,    10,2,2
DefData -28,26,0,     0,14,5,         0,14,5,     28,26,0
DefData -28,26,0,     0,18,6,         0,18,6,     28,26,0
DefData -28,26,0,     0,31,6,         0,31,6,     28,26,0
DefData  0,14,5,      0,18,6,        -28,26,0,    0,36,5
DefData  0,36,5,      28,26,0,        0,31,6,     0,36,5
DefData -8,47,0,      0,36,5,         0,36,5,     8,47,0
'
'~~~~~~~~~~~~~~~~~~~~~OUTLINE OF FACE~~~~~~~~~~~~~~~~~~~
DefData  0,-52,0,    -26,-47,0,      -26,-47,0,  -37,-32,0
DefData -37,-32,0,   -37,-6,0,       -37,-6,0,   -28,26,0
DefData -28,26,0,    -8,47,0,        -8,47,0,     8,47, 0
DefData  8,47,0,      28,26,0,        28,26,0,    37,-6,0
DefData  37,-6,0,     37,-32,0,       37,-32,0,   26,-47,0
DefData  26,-47,0,    0,-52,0
'
'~~~~~~~~~~~~~~~~~~~~~~~~FOREHEAD~~~~~~~~~~~~~~~~~~~~~~~
DefData  0,-52,0,    -23,-33,10,     -23,-33,10, -5,-25,10
DefData -26,-47,0,   -23,-33,10,     -37,-32,0,  -23,-33,10
DefData -37,-18,0,   -23,-33,10,     -10,-18,2,  -23,-33,10
DefData  0,-52,0,     23,-33,10,      23,-33,10,  5,-25,10
DefData  26,-47,0,    23,-33,10,      37,-32,0,   23,-33,10
DefData  37,-18,0,    23,-33,10,      23,-33,10,  10,-18,2
'
'~~~~~~~~~~~~~~~~~~~~~~EYES + CHEEKS~~~~~~~~~~~~~~~~~~~~
DefData -37,-18,0,   -23,-26,12,     -23,-26,12, -10,-18,2
DefData -37,-18,0,   -23,-13,10,     -23,-13,10, -10,-18,2
DefData -37,-18,0,   -23,-17,12,     -23,-17,12, -10,-18,2
DefData -23,-33,10,  -23,-26,12,     -23,-17,12, -23,-13,10
DefData -10,2,2,     -23,-13,10,     -23,-13,10, -37,-6,0
DefData  37,-18,0,    23,-26,12,      23,-26,12,  10,-18,2
DefData  37,-18,0,    23,-13,10,      23,-13,10,  10,-18,2
DefData  37,-18,0,    23,-17,12,      23,-17,12,  10,-18,2
DefData  23,-33,10,   23,-26,12,      23,-17,12,  23,-13,10
DefData  10,2,2,      23,-13,10,      23,-13,10,  37,-6,0