ChatGPT - "3D sphere"

Star-Dust

Expert
Licensed User
Longtime User
ChatSD also handled touch

1727275944891.png
27.gif

 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User

B4J code generated by ChatGPT (it only failed to handle arrow key press events).
AI it's interesting and I use it a lot, but I can say that it is useful for creating small parts of a code. A complex query or solve some problems in known languages.
But it is not efficient in all languages, it often applies the syntax of another language or non-existent methods, it always needs to be fixed. It also can't develop an app unless it's very small. Often uses instructions incorrectly.
 

Star-Dust

Expert
Licensed User
Longtime User
šŸ˜…

I know your skills in this area, @Star-Dust, but mine are much poorer. Note that I was not interested in getting something usable, I just wanted to test out ChatGPT's capabilities.
Yes, in fact it was an answer to ChatGPT. It's still not at chatSD level
 

Johan Schoeman

Expert
Licensed User
Longtime User
This is what Copilot gives me when asked to draw a 3D sphere in B4J and to rotate it. Have not tried it in a B4J project. Don't know what it has done with the Timer.....

write b4j code to draw a 3d sphere:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private Timer1 As Timer
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show

    ' Create a 3D sphere
    Dim sphere As JavaObject
    sphere.InitializeNewInstance("javafx.scene.shape.Sphere", Array(100)) ' 100 is the radius of the sphere

    ' Create a 3D scene
    Dim scene As JavaObject
    scene.InitializeNewInstance("javafx.scene.Scene", Array(MainForm.RootPane, 800, 600, True))
    scene.RunMethod("setFill", Array(fx.Colors.White))

    ' Add the sphere to the scene
    Dim root As JavaObject = scene.RunMethod("getRoot", Null)
    root.RunMethod("getChildren", Null).RunMethod("add", Array(sphere))

    ' Create a PointLight
    Dim pointLight As JavaObject
    pointLight.InitializeNewInstance("javafx.scene.PointLight", Null)
    pointLight.RunMethod("setColor", Array(fx.Colors.White))
    pointLight.RunMethod("setTranslateX", Array(200))
    pointLight.RunMethod("setTranslateY", Array(-100))
    pointLight.RunMethod("setTranslateZ", Array(-300))

    ' Add the light to the scene
    root.RunMethod("getChildren", Null).RunMethod("add", Array(pointLight))

    ' Set the scene to the form
    MainForm.RootPane.RunMethod("setScene", Array(scene))

    ' Create a RotateTransition for the sphere
    Dim rotateTransition As JavaObject
    rotateTransition.InitializeNewInstance("javafx.animation.RotateTransition", Array(2000, sphere)) ' 2000 ms duration
    rotateTransition.RunMethod("setByAngle", Array(360)) ' Rotate by 360 degrees
    rotateTransition.RunMethod("setCycleCount", Array(-1)) ' Infinite loop
    rotateTransition.RunMethod("setAutoReverse", Array(False))

    ' Start the rotation
    rotateTransition.RunMethod("play", Null)
End Sub
 

Daestrum

Expert
Licensed User
Longtime User
most of the errors were caused by passing integer values where it needed doubles.

also a white sphere on a white background with a white light - not a good combination if you actually want to see anything.

modified code (no rotation - as a plain sphere wont look like its rotating anyways)
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show

    ' Create a 3D sphere
    Dim sphere As JavaObject
    sphere.InitializeNewInstance("javafx.scene.shape.Sphere", Array(100.00)) ' 100 is the radius of the sphere


    ' Add the sphere to the scene
    MainForm.RootPane.AddNode(sphere,300,300,-1,-1)

    ' Create a PointLight
    Dim pointLight As JavaObject
    pointLight.InitializeNewInstance("javafx.scene.PointLight", Null)
    pointLight.RunMethod("setColor", Array(fx.Colors.blue))
    pointLight.RunMethod("setTranslateX", Array(200.0))
    pointLight.RunMethod("setTranslateY", Array(-100.0))
    pointLight.RunMethod("setTranslateZ", Array(-600.0))

    ' Add the light to the scene
    MainForm.RootPane.AddNode(pointLight,300,300,-1,-1)


    ' Set the scene to the form
    'MainForm.RootPane.RunMethod("setScene", Array(scene))

    ' Create a RotateTransition for the sphere
'    Dim rotateTransition As JavaObject
'    rotateTransition.InitializeNewInstance("javafx.animation.RotateTransition", Array(2000, sphere)) ' 2000 ms duration
'    rotateTransition.RunMethod("setByAngle", Array(360)) ' Rotate by 360 degrees
'    rotateTransition.RunMethod("setCycleCount", Array(-1)) ' Infinite loop
'    rotateTransition.RunMethod("setAutoReverse", Array(False))

    ' Start the rotation
'    rotateTransition.RunMethod("play", Null)
End Sub
 

Star-Dust

Expert
Licensed User
Longtime User
I am sure that with a bit of effort one could turn this into a 3D rotating sphere.
With bit effort with B4X and without AI he made a 3D in multiplatform

I have tested Copilot a lot, it also has a fairly effective image generation. For the rest they don't change much. I use both, when I'm lucky one gives me an acceptable result


B4J code generated by ChatGPT (it only failed to handle arrow key press events).
Post the code that generated
 
Last edited:

Daestrum

Expert
Licensed User
Longtime User
Modified code - added material to sphere and rotation about specified axis.
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    
    ' get image for sphere    
    Dim img As Image
    img.Initialize("C:/temp","mypicture.png")

    'create  material
    Dim material As JavaObject
    material.InitializeNewInstance("javafx.scene.paint.PhongMaterial",Null)
    material.RunMethod("setDiffuseMap",Array(img))    
    
    ' Create a 3D sphere
    Dim sphere As JavaObject
    sphere.InitializeNewInstance("javafx.scene.shape.Sphere", Array(100.00)) ' 100 is the radius of the sphere
    'set sphere material
    sphere.RunMethod("setMaterial",Array(material))

    ' Add the sphere to the scene
    MainForm.RootPane.AddNode(sphere,300,300,-1,-1)

    ' Create a PointLight
    Dim pointLight As JavaObject
    pointLight.InitializeNewInstance("javafx.scene.PointLight", Null)
    pointLight.RunMethod("setColor", Array(fx.Colors.white))
    pointLight.RunMethod("setTranslateX", Array(200.0))
    pointLight.RunMethod("setTranslateY", Array(-100.0))
    pointLight.RunMethod("setTranslateZ", Array(-600.0))

    ' Add the light to the scene
    MainForm.RootPane.AddNode(pointLight,300,300,-1,-1)

    ' duration of animation
    Dim duration As JavaObject
    duration.InitializeNewInstance("javafx.util.Duration",Array(4000.00))

    'set axis for rotation
    Dim axis As JavaObject
    axis.InitializeNewInstance("javafx.geometry.Point3D",Array(0.0,1.0,0.0)) ' x,y,z  one with 1.0 is axis to use

    ' Create a RotateTransition for the sphere
    Dim rotateTransition As JavaObject
    rotateTransition.InitializeNewInstance("javafx.animation.RotateTransition", Array(duration, sphere)) ' 2000 ms duration
    rotateTransition.RunMethod("setByAngle", Array(359.0)) ' Rotate by 360 degrees
    rotateTransition.RunMethod("setCycleCount", Array(-1)) ' Infinite loop
    rotateTransition.RunMethod("setAutoReverse", Array(False))
    rotateTransition.RunMethod("setAxis",Array(axis))
    
    ' Start the rotation
    rotateTransition.RunMethod("play", Null)
End Sub
 

Johan Schoeman

Expert
Licensed User
Longtime User
99.9% from Copilot - only had to do 2 small changes. This one also spins...
1727432609736.png



Copilot:
#Region Project Attributes
    #MainFormWidth: 800
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private Timer1 As Timer
    Private Canvas1 As Canvas
    Private Sphere As List
    Private Angle As Double
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1") ' Load the layout file.
    MainForm.Show
    
    Timer1.Initialize("Timer1", 30)
    Timer1.Enabled = True
    Sphere.Initialize
    CreateSphere(150, 20, 20)
End Sub

Sub Timer1_Tick
    Angle = Angle + 0.05
    DrawSphere
End Sub

Sub CreateSphere(Radius As Double, Latitudes As Int, Longitudes As Int)
    For i = 0 To Latitudes
        Dim theta As Double = i * cPI / Latitudes
        Dim sinTheta As Double = Sin(theta)
        Dim cosTheta As Double = Cos(theta)
        
        For j = 0 To Longitudes
            Dim phi As Double = j * 2 * cPI / Longitudes
            Dim sinPhi As Double = Sin(phi)
            Dim cosPhi As Double = Cos(phi)
            
            Dim x As Double = Radius * cosPhi * sinTheta
            Dim y As Double = Radius * cosTheta
            Dim z As Double = Radius * sinPhi * sinTheta
            
            Dim point As Map
            point.Initialize
            point.Put("x", x)
            point.Put("y", y)
            point.Put("z", z)
            Sphere.Add(point)
        Next
    Next
End Sub

Sub DrawSphere
    Canvas1.ClearRect(0, 0, Canvas1.Width, Canvas1.Height)
    For Each point As Map In Sphere
        Dim x As Double = point.Get("x")
        Dim y As Double = point.Get("y")
        Dim z As Double = point.Get("z")
        
        Dim screenX As Double = x * Cos(Angle) - z * Sin(Angle)
        Dim screenY As Double = y
        Dim screenZ As Double = x * Sin(Angle) + z * Cos(Angle)
        
        Dim scale As Double = 300 / (300 + screenZ)
        screenX = screenX * scale + Canvas1.Width / 2
        screenY = screenY * scale + Canvas1.Height / 2
        
        Canvas1.DrawCircle(screenX, screenY, 2, fx.Colors.Black, True, 1)
    Next
End Sub
 

Johan Schoeman

Expert
Licensed User
Longtime User
Also from Copilot but had to do some open-heart surgery on the Copilot code to make it work in B4J. It is drawn on a standard B4J canvas.
Drag it around with your mouse....

1727513809617.png


Code of Main:
Main:
#Region Project Attributes
    #MainFormWidth: 800
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private Canvas1 As Canvas
    Private Sphere As Sphere3D
    
    Private xAngle, yAngle As Float
    Private MouseX, MouseY As Float
    Private IsDragging As Boolean
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show

    Sphere.Initialize(Canvas1)
    Canvas1 = Sphere.DrawSphere(105.0f, 0.0f)
    
End Sub

Private Sub Canvas1_MousePressed (EventData As MouseEvent)
    MouseX = EventData.x
    MouseY = EventData.y
    IsDragging = True
End Sub

Private Sub Canvas1_MouseDragged (EventData As MouseEvent)
    Dim x As Double = EventData.x
    Dim y As Double = EventData.y
    If IsDragging Then
        xAngle = xAngle + (x - MouseX) * 0.1
        yAngle = yAngle + (y - MouseY) * 0.1
        MouseX = x
        MouseY = y
        Canvas1 = Sphere.DrawSphere(xAngle, yAngle)
    End If
    
End Sub

Private Sub Canvas1_MouseReleased (EventData As MouseEvent)
    IsDragging = False
End Sub

Code of Class Sphere3D:
Class Sphere3D:
Sub Class_Globals
    Private cvs As Canvas
    Dim fx As JFX
End Sub

Public Sub Initialize(c As Canvas)
    cvs = c
    
End Sub

public Sub DrawSphere(xAngle As Float, yAngle As Float) As Canvas
    cvs.ClearRect(0, 0, cvs.Width, cvs.Height)
    Dim radius As Float = Min(cvs.Width, cvs.Height) / 3
    Dim centerX As Float = cvs.Width / 2
    Dim centerY As Float = cvs.Height / 2

    For lat = -90 To 90 Step 5
        For lon = -180 To 180 Step 5
            Dim x1, y1, z1 As Float
            x1 = radius * CosD(lat) * CosD(lon)
            y1 = radius * CosD(lat) * SinD(lon)
            z1 = radius * SinD(lat)

            Dim x2, y2 As Float
            x2 = centerX + x1 * CosD(yAngle) - z1 * SinD(yAngle)
            y2 = centerY + y1 * CosD(xAngle) - (x1 * SinD(yAngle) + z1 * CosD(yAngle)) * SinD(xAngle)

            cvs.DrawCircle(x2, y2, 1, fx.Colors.Black, True, 1)
        Next
    
    Next
    Return cvs
End Sub
 

Attachments

  • sphereWithLines.zip
    2.6 KB · Views: 22

Johan Schoeman

Expert
Licensed User
Longtime User
A 3d pyramid created by Copilot - had to do some very minor changes (only mouse Events) to make it work in B4J. Drag it around with your mouse...

1727519952432.png
 

Attachments

  • pyramid.zip
    2.2 KB · Views: 25

JackKirk

Well-Known Member
Licensed User
Longtime User
In a couple of weeks I hope to report on a major webapp I have developed that uses ABMaterial as the "server framework" but each ABMPage has a single cell with an ABMComp element. Each ABMComp has a html component and a JavaScript component.

Trouble was before I started this exercise I had zero real competence in either - I have mastered Copilot to the extent I can now produce some pretty slick stuff - at least by my measure. And I can use it for debugging both in a good many cases.

There is a saying floating around along the lines "AI won't take your job - the person who masters it will".
 
Top