2d/3d Stars

Blitz3D Forums/Blitz3D Programming/2d/3d Stars

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Heya. This code is based on Tracer's 3ddotball.bb code that shipped with blitz3d. I'm trying to get a sphere of 2d stars to be rotated as if it was a 3d starfield. If you know what I mean. Here's the source. I just can't get it right.

I want to make it move as if each star were a sprite, and the camera was looking around.

Any help appreciated.

; *****************************
; * DEPTH COLORED 3D DOT BALL *
; *****************************
; * CODING BY TRACER          *
										; Z location.

; Arrays used by 3d code.
Dim star_points#(1500, 5)
Graphics 512,384,32,2
SetBuffer BackBuffer()

setup2dStarField()
HidePointer

; Loop till esc is pressed.
While Not KeyDown(1)

	mx#=(MouseXSpeed()*0.2)
	my#=(MouseYSpeed()*0.2)

	MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
	update2DStarField(mx,my)									; Removes the mouse from fullscreen.
	Flip													; Flip the screen.
	Cls														; Clear the screen.
Wend
End


Function setup2dStarField(dots_in_ball=1500)
	For t= 1 To dots_in_ball
		xd# = Rnd(-90.0,90.0)
		x0# = (Cos(xd) * 10.0) * (Cos(t) * 10.0)
		y0# = (Cos(xd) * 10.0) * (Sin(t) * 10.0)
		z0# = Sin(xd) * 100.0
		star_points(t,1) = x0
		star_points(t,2) = y0
		star_points(t,3) = z0
		star_points(t,4)=Rnd(10,150)
	Next
	star_points(0,0)=100
End Function


Function update2DStarField(x#,y#,dots_in_ball=1500)
	star_points(0,1)=star_points(0,1)+y
	star_points(0,2)=star_points(0,2)
	star_points(0,3)=star_points(0,3)+x

	gw=GraphicsWidth()
	gh=GraphicsHeight()

	distance#=star_points(0,0)

	vx#=star_points(0,1)
	vy#=star_points(0,2)
	vz#=star_points(0,3)

	For n = 1 To dots_in_ball
		x3d# = star_points(n, 1)
		y3d# = star_points(n, 2)
		z3d# = star_points(n, 3)       
		ty# = ((y3d * Cos(vx#)) - (z3d * Sin(vx#)))
		tz# = ((y3d * Sin(vx#)) + (z3d * Cos(vx#)))
		tx# = ((x3d * Cos(vy#)) - (tz# * Sin(vy#)))
		tz# = ((x3d * Sin(vy#)) + (tz# * Cos(vy#)))
		ox# = tx#
		tx# = ((tx# * Cos(vz#)) - (ty# * Sin(vz#)))
		ty# = ((ox# * Sin(vz#)) + (ty# * Cos(vz#)))
		nx#  = Int(gw * (tx#) / (distance - (tz#))) + gh
		ny#  = Int(gh - (gw* ty#) / (distance - (tz#)))
		Color star_points(n, 4),star_points(n, 4),star_points(n, 4)
		Rect nx,ny,1,1
	Next
End Function