dcsimg
Results 1 to 8 of 8

Thread: [RESOLVED] I have a glitch in my 3D starfield. I can't figure out what's wrong.

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    7

    Resolved [RESOLVED] I have a glitch in my 3D starfield. I can't figure out what's wrong.

    The application works correctly for a short time frame. I have a range where the 3D space is -1 to +1 on all axis. In the code, 0 is the camera limit for the z axis, so if stars reach past 0 they should disappear and be replaced in random locations. The program seems to work, but the screen clears completely and everything is reset, instead of a smooth continuation. I am not even sure if the Rnd function might be the error, I simply can't figure out where I went wrong. The code is a conversion of a Java example, which is very clear, but it does not seem to add up correctly in Visual Basic. If you have the patience to look over this and maybe get it into Visual Studio, I would appreciate it. The only annoyance perhaps is that I am using SharpDevelop 4.4, for this project. Here is the application. Please tell me if you figure out what is wrong with this.

    Code:
    Public Partial Class MainForm
    	Public Sub New()
    		' The Me.InitializeComponent call is required for Windows Forms designer support.
    		Me.InitializeComponent()
    		
    		'
    		' TODO : Add constructor code after InitializeComponents
    		'
    	End Sub
    	
    	Dim bm As New Bitmap(800,600)
    	
    	Dim Stars As New Stars3D(4096, 64.0, 20.0)
    	
    	Dim bQuit As Boolean = False
    	
    	Dim previousTime As long = Date.Now.Millisecond
    	
    	Sub MainFormLoad(sender As Object, e As EventArgs)
    		Me.Show
    		
    		Do While Not bQuit
    			Application.DoEvents
    			
    			Dim currentTime As Long = Date.Now.Millisecond
    			Dim delta As Single = 	CSng((currentTime - previousTime) / 1000)
    			previousTime = currentTime
    			
    			Stars.UpdateAndRender(bm,delta)
    			
    			pbDisplay.Invalidate
    		Loop
    		
    	End Sub
    		
    	Sub PbDisplayPaint(sender As Object, e As PaintEventArgs)
    		e.Graphics.DrawImage(bm,0,0,800,600)
    	End Sub
    	
    	
    	Sub MainFormFormClosed(sender As Object, e As FormClosedEventArgs)
    		bQuit = True
    	End Sub
    End Class
    Code:
    Public Class Stars3D
    	Dim m_spread As Single
    	Dim m_speed As single
    	
    	Dim m_starX() As Single
    	Dim m_starY() As Single
    	Dim m_starZ() As Single
    	
    	Public Sub New(numStars As Integer, spread As Single, speed As Single)
    		m_spread = spread
    		m_speed = speed
    		
    		ReDim m_starX(numStars)
    		ReDim m_starY(numStars)
    		ReDim m_starZ(numStars)
    		
    		For i As Integer = 0 To m_starX.Length - 1
    			InitStar(i)
    		Next
    	End Sub
    	
    	Public Sub UpdateAndRender(target As Bitmap, delta As Single)
    		Dim g_target As Graphics = Graphics.FromImage(target)
    		g_target.Clear(Color.Black)
    		
    		Dim halfWidth As Single = 400
    		Dim halfHeight As Single = 300
    		
    		For i As Integer = 0 To m_starX.Length - 1
    			m_starZ(i) -= delta * m_speed
    			If m_starZ(i) <= 0 Then
    				InitStar(i)
    			End If
    			
    			Dim x As Integer = CInt((m_starX(i) / m_starZ(i)) * halfWidth + halfWidth)
    			Dim y As Integer = CInt((m_starY(i) / m_starZ(i)) * halfHeight + halfHeight)
    			
    			If (x < 0 Or x >= 800 Or (y < 0 Or y >= 600)) Then
    				InitStar(i)
    			Else
    				target.SetPixel(x, y, Color.white)
    			End If
    		Next
    	End Sub
    	
    	Private Sub InitStar(i As Integer)
    		m_starX(i) = 2 * CSng(Rnd - 0.5) * m_spread
    		m_starY(i) = 2 * CSng(Rnd - 0.5) * m_spread
    		m_starZ(i) = CSng(Rnd + 0.00001) * m_spread
    	End Sub
    End Class

  2. #2
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,784

    Re: I have a glitch in my 3D starfield. I can't figure out what's wrong.

    I think it's this line: m_starZ(i) -= delta * m_speed

    I think "delta * m_speed" generates a number that is quite big, and it goes out of range very quick

    Try something like:
    m_starZ(i) -= delta * m_speed / 100

    PS. I did not try it out, this is just by looking at the code

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    7

    Re: I have a glitch in my 3D starfield. I can't figure out what's wrong.

    Thanks, I was trying to solve it at the same time. You are partly correct, but I am still trying to wrap my head around it. My delta time calculation was gimbal locked with Date.Now. So I am trying a stopwatch instead.

    Here is a quick solve, I am studying it now.

    Code:
    Public Partial Class MainForm
    	Public Sub New()
    		' The Me.InitializeComponent call is required for Windows Forms designer support.
    		Me.InitializeComponent()
    		
    		'
    		' TODO : Add constructor code after InitializeComponents
    		'
    	End Sub
    	
    	Dim bm As New Bitmap(800,600)
    	
    	Dim Stars As New Stars3D(4096, 64.0, .006)
    	
    	Dim bQuit As Boolean = False
    	
    	Dim previousTime As long = 0
    	
    	Sub MainFormLoad(sender As Object, e As EventArgs)
    		Me.Show
    		
    		Dim sw As New Stopwatch
    		sw.Start
    		
    		Do While Not bQuit
    			Application.DoEvents
    			
    			Dim currentTime As Long = sw.GetTimestamp
    			Dim delta As Single = 	CSng((currentTime - previousTime) / 1000)
    			
    			Stars.UpdateAndRender(bm,delta)
    			
    			previousTime = currentTime
    			
    			sw.Restart
    			
    			pbDisplay.Invalidate
    		Loop
    		
    	End Sub
    		
    	Sub PbDisplayPaint(sender As Object, e As PaintEventArgs)
    		e.Graphics.DrawImage(bm,0,0,800,600)
    	End Sub
    	
    	
    	Sub MainFormFormClosed(sender As Object, e As FormClosedEventArgs)
    		bQuit = True
    	End Sub
    End Class

  4. #4
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,784

    Re: [RESOLVED] I have a glitch in my 3D starfield. I can't figure out what's wrong.

    Well, your speed is also high: 20
    That's why I put the division there because it affects both time & speed

    [Edit], Ow, never mind, I just saw you changed it to .006

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,026

    Re: [RESOLVED] I have a glitch in my 3D starfield. I can't figure out what's wrong.

    You weren't using Date.Now, but rather Date.Now.Millisecond, which only returns the millisecond component.

    So one iteration the value could be 950, and the next 100, which gives a negative delta.

  6. #6
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,784

    Re: [RESOLVED] I have a glitch in my 3D starfield. I can't figure out what's wrong.

    I didn't notice that, nice catch!

    The fix for that would be something like:
    If delta < 0 Then delta = CSng(((currentTime + 1000) - previousTime) / 1000)

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,541

    Re: [RESOLVED] I have a glitch in my 3D starfield. I can't figure out what's wrong.

    Or use a StopWatch object
    Code:
    'in the declarations
     Private sw As New Stopwatch
    
    Sub MainFormLoad(sender As Object, e As EventArgs)
        Me.Show()
        sw.Start()
        Do While Not bQuit
          Application.DoEvents()
    
          Dim currentTime As Long = sw.ElapsedMilliseconds '.Now.Millisecond
    
    '...

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    7

    Re: [RESOLVED] I have a glitch in my 3D starfield. I can't figure out what's wrong.

    Thank's for the help. Next, I'm going to be trying some simple shapes. I found a tutorial on Software Rendering, but it's java based, so I have to try and translate most of it.

    Anyone here have some 3D tuts that are VB based? I'm looking to do everything from scratch, so no OpenGL and etc. The immediate goal is a wireframe render of a small 3D scene in Visual Basic. Even C# would be ok I think.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width