Android Tutorial Introduction to the libGDX library

rkwan

Member
Licensed User
Thanks, Informatix!

I can finally see it after I put the point along one of the axes and use a bigger tablet to show!

Therefore, my next questions would be:-

1. Any easy way to make the dot bigger to view please?!

2. In fact, I am trying to do a 3D scatter plot like this one with matplotlib:-
https://matplotlib.org/examples/mplot3d/scatter3d_demo.html
I can get the elevation and azimuth view change with sliders.
Therefore, I am thinking if I can achieve the same with B4A
but it seems quite difficult now...?!

Any suggestion please?

Or do you think that the 3D Bodies library may be better for this kind of plot please?
https://www.b4x.com/android/forum/threads/3d-bodies-four-libraries.48373/

Thanks & Best Regards,
Robert
 

Informatix

Expert
Licensed User
Thanks, Informatix!

I can finally see it after I put the point along one of the axes and use a bigger tablet to show!

Therefore, my next questions would be:-

1. Any easy way to make the dot bigger to view please?!

2. In fact, I am trying to do a 3D scatter plot like this one with matplotlib:-
https://matplotlib.org/examples/mplot3d/scatter3d_demo.html
I can get the elevation and azimuth view change with sliders.
Therefore, I am thinking if I can achieve the same with B4A
but it seems quite difficult now...?!

Any suggestion please?

Or do you think that the 3D Bodies library may be better for this kind of plot please?
https://www.b4x.com/android/forum/threads/3d-bodies-four-libraries.48373/

Thanks & Best Regards,
Robert
Draw boxes instead of points.
 

rkwan

Member
Licensed User
Thanks, Informatix!

I can finally create my Scatter Plot like attached now.

However, I have another challenge which I would like to seek for your advice please.

In my mat3dplot, I could use sliders to control the azimuth and elevation
but now I can only control the azimuth through the Y-axis rotation in this example code.

Therefore, I would like to ask if any way to distinguish horizontal touch dragging vs vertical touch dragging in Sub IP_TouchDragged
so that I can get rotation along X-axis for vertical touch dragging to mimick the elevation view in mat3dplot please.


Thanks & Best Regards,
 

Attachments

Informatix

Expert
Licensed User
Thanks, Informatix!

I can finally create my Scatter Plot like attached now.

However, I have another challenge which I would like to seek for your advice please.

In my mat3dplot, I could use sliders to control the azimuth and elevation
but now I can only control the azimuth through the Y-axis rotation in this example code.

Therefore, I would like to ask if any way to distinguish horizontal touch dragging vs vertical touch dragging in Sub IP_TouchDragged
so that I can get rotation along X-axis for vertical touch dragging to mimick the elevation view in mat3dplot please.


Thanks & Best Regards,
In the ShapeRender demo, LastX - ScreenX computes the amount of move on the horizontal axis. Do the same for the vertical axis (LastY - ScreenY in IP_TouchDragged, with LastY initialized in IP_Touchdown). By comparing the absolute value of the two deltas, you can decide whether the move is more horizontal than vertical, or the contrary.
 

rkwan

Member
Licensed User
Thanks a lot for the hint, Informatix!

I can manage to have the X-axis rotation now but it would not be isolated from the Y-axis rotation even when I slide vertically only.
It would still mix up with Y-axis rotation at the same time (even though it is supposed to be triggered by horizontal slide?!)
and so my display would become messy rotation.

If I comment out either the if or the else if section before,
I can get the Y-axis and X-axis rotation independently Ok with either horizontal or vertical sliding though.

B4X:
Sub Globals

    Dim LastX As Float
    Dim Point, Y_Axis As lgMathVector3
    Dim RotMatrix As lgMathMatrix4
    Dim LastY As Float
    Dim X_Axis As lgMathVector3
    Dim RotMatrix2 As lgMathMatrix4
   
    Dim Y_Rot_Only As Int
    Dim X_Rot_Only As Int
End Sub

Sub LG_Create
   ...
    'Initializes the vector of the rotation axis
    Y_Axis.Set(0, 1, 0)

    X_Axis.Set(1, 0, 0)

End Sub

Sub IP_TouchDown(ScreenX As Int, ScreenY As Int, Pointer As Int) As Boolean
    If Pointer <> 0 Then Return False

    LastX = ScreenX
    LastY = ScreenY
   
    Y_Rot_Only = 0
    X_Rot_Only = 0
   
    Return True
End Sub


Sub IP_TouchDragged(ScreenX As Int, ScreenY As Int, Pointer As Int) As Boolean
    If Pointer <> 0 Then Return False

    'Rotates the camera on the Y axis
    Point.Set(Camera.Position.X, Camera.Position.Y, Camera.Position.Z)
   
    If (Abs(LastX - ScreenX) > Abs(LastY - ScreenY)) And (X_Rot_Only <> 1) Then
        Log("Path YYY")
        RotMatrix.setToRotation(Y_Axis, (LastX - ScreenX) / 5)
        Point.Mul(RotMatrix)
        Camera.Position.Set(Point.X, Point.Y, Point.Z)
        Camera.LookAt(0.0, 0.5, 0.0)
        Camera.Update
        Y_Rot_Only = 1
    Else If (Abs(LastX - ScreenX) <= Abs(LastY - ScreenY)) And (Y_Rot_Only <> 1) Then
        Log("Path XXX")
        RotMatrix2.setToRotation(X_Axis, (LastY - ScreenY) / 5)
        Point.Mul(RotMatrix2)
        Camera.Position.Set(Point.X, Point.Y, Point.Z)
        Camera.LookAt(0.5, 0.5, 0.5)
        Camera.Update
        X_Rot_Only = 1
    End If
   
    'LastX = ScreenX
    'LastY = ScreenY
    Return True
End Sub

[\code]

Therefore, any idea on what I am wrong please?


Thanks & Best Regards,
 

Informatix

Expert
Licensed User
Thanks a lot for the hint, Informatix!

I can manage to have the X-axis rotation now but it would not be isolated from the Y-axis rotation even when I slide vertically only.
It would still mix up with Y-axis rotation at the same time (even though it is supposed to be triggered by horizontal slide?!)
and so my display would become messy rotation.

If I comment out either the if or the else if section before,
I can get the Y-axis and X-axis rotation independently Ok with either horizontal or vertical sliding though.

B4X:
Sub Globals

    Dim LastX As Float
    Dim Point, Y_Axis As lgMathVector3
    Dim RotMatrix As lgMathMatrix4
    Dim LastY As Float
    Dim X_Axis As lgMathVector3
    Dim RotMatrix2 As lgMathMatrix4
  
    Dim Y_Rot_Only As Int
    Dim X_Rot_Only As Int
End Sub

Sub LG_Create
   ...
    'Initializes the vector of the rotation axis
    Y_Axis.Set(0, 1, 0)

    X_Axis.Set(1, 0, 0)

End Sub

Sub IP_TouchDown(ScreenX As Int, ScreenY As Int, Pointer As Int) As Boolean
    If Pointer <> 0 Then Return False

    LastX = ScreenX
    LastY = ScreenY
  
    Y_Rot_Only = 0
    X_Rot_Only = 0
  
    Return True
End Sub


Sub IP_TouchDragged(ScreenX As Int, ScreenY As Int, Pointer As Int) As Boolean
    If Pointer <> 0 Then Return False

    'Rotates the camera on the Y axis
    Point.Set(Camera.Position.X, Camera.Position.Y, Camera.Position.Z)
  
    If (Abs(LastX - ScreenX) > Abs(LastY - ScreenY)) And (X_Rot_Only <> 1) Then
        Log("Path YYY")
        RotMatrix.setToRotation(Y_Axis, (LastX - ScreenX) / 5)
        Point.Mul(RotMatrix)
        Camera.Position.Set(Point.X, Point.Y, Point.Z)
        Camera.LookAt(0.0, 0.5, 0.0)
        Camera.Update
        Y_Rot_Only = 1
    Else If (Abs(LastX - ScreenX) <= Abs(LastY - ScreenY)) And (Y_Rot_Only <> 1) Then
        Log("Path XXX")
        RotMatrix2.setToRotation(X_Axis, (LastY - ScreenY) / 5)
        Point.Mul(RotMatrix2)
        Camera.Position.Set(Point.X, Point.Y, Point.Z)
        Camera.LookAt(0.5, 0.5, 0.5)
        Camera.Update
        X_Rot_Only = 1
    End If
  
    'LastX = ScreenX
    'LastY = ScreenY
    Return True
End Sub

[\code]

Therefore, any idea on what I am wrong please?


Thanks & Best Regards,
When more than one axis is involved, that becomes more complicated. Look at this solution:
https://stackoverflow.com/questions/30690027/realistic-rotation-of-a-3d-model-in-libgdx-by-dragging
 

rkwan

Member
Licensed User
Thanks for the information, Informatix!

Yes, it looks complicated and I will need some more time to try out then...
 

rkwan

Member
Licensed User
Hello Informatix,

After spending some more time to try out the solution you pointed,
I am still stuck at two points and would like to see if you would have more pointers please.

First, I mimick that solution to implement in B4A like this:-

B4X:
    Dim Ray1, Ray2 As lgMathRay
    Dim Screen1, Screen2 As lgMathVector3
    Dim Cross1, Cross2 As lgMathVector3
    Dim CenterPosition As lgMathVector3
    Dim QRotation As lgMathMatrix4

Sub IP_TouchDragged(ScreenX As Int, ScreenY As Int, Pointer As Int) As Boolean
    If Pointer <> 0 Then Return False

    'Rotates the camera on the Y axis
    Point.Set(Camera.Position.X, Camera.Position.Y, Camera.Position.Z)

    Ray1 = Camera.GetPickRay(LastX, LastY)
    Ray2 = Camera.GetPickRay(ScreenX, ScreenY)
   
    Screen1 = Ray1.direction.cpy().scl(0.1).add(Ray1.origin)
    Screen2 = Ray2.direction.cpy().scl(0.1).add(Ray2.origin)

    CenterPosition.set(0,0,0)   

    Cross1 = Screen1.cpy().sub(CenterPosition).nor()
    Cross2 = Screen2.cpy().sub(CenterPosition).nor()
   
    '    RotMatrix.setToRotation(Cross1, Cross2)
    QRotation.set

    Point.Mul(RotMatrix)
    Camera.Position.Set(Point.X, Point.Y, Point.Z)
    Camera.LookAt(0.0, 0.5, 0.0)
    Camera.Update

...

[\code]

1. 
for the settings of Ray1, Ray2, Screen1 & Screen2 seem straight-forward,
but I am not sure if the myObjectsCenterPosition in the solution would mean,
 what I am trying to set CenterPosition.set(0,0,0) as in your example with the 3 axes?!

2. I know that there is no Quaternion class in the libGDX and so I tried to use lgMathMatrix4 instead
but then I realized that there is no member setfFromCross(Cross1, Cross2) for the lgMathMatrix4 as in Quaternion,
and so I am stuck here too.


Thanks & Best Regards,
 

Informatix

Expert
Licensed User
Hello Informatix,

After spending some more time to try out the solution you pointed,
I am still stuck at two points and would like to see if you would have more pointers please.

First, I mimick that solution to implement in B4A like this:-

B4X:
    Dim Ray1, Ray2 As lgMathRay
    Dim Screen1, Screen2 As lgMathVector3
    Dim Cross1, Cross2 As lgMathVector3
    Dim CenterPosition As lgMathVector3
    Dim QRotation As lgMathMatrix4

Sub IP_TouchDragged(ScreenX As Int, ScreenY As Int, Pointer As Int) As Boolean
    If Pointer <> 0 Then Return False

    'Rotates the camera on the Y axis
    Point.Set(Camera.Position.X, Camera.Position.Y, Camera.Position.Z)

    Ray1 = Camera.GetPickRay(LastX, LastY)
    Ray2 = Camera.GetPickRay(ScreenX, ScreenY)
  
    Screen1 = Ray1.direction.cpy().scl(0.1).add(Ray1.origin)
    Screen2 = Ray2.direction.cpy().scl(0.1).add(Ray2.origin)

    CenterPosition.set(0,0,0)  

    Cross1 = Screen1.cpy().sub(CenterPosition).nor()
    Cross2 = Screen2.cpy().sub(CenterPosition).nor()
  
    '    RotMatrix.setToRotation(Cross1, Cross2)
    QRotation.set

    Point.Mul(RotMatrix)
    Camera.Position.Set(Point.X, Point.Y, Point.Z)
    Camera.LookAt(0.0, 0.5, 0.0)
    Camera.Update

...

[\code]

1.
for the settings of Ray1, Ray2, Screen1 & Screen2 seem straight-forward,
but I am not sure if the myObjectsCenterPosition in the solution would mean,
 what I am trying to set CenterPosition.set(0,0,0) as in your example with the 3 axes?!

2. I know that there is no Quaternion class in the libGDX and so I tried to use lgMathMatrix4 instead
but then I realized that there is no member setfFromCross(Cross1, Cross2) for the lgMathMatrix4 as in Quaternion,
and so I am stuck here too.


Thanks & Best Regards,
This is a bit out of my league. I'm not an expert in 3D maths.
Sorry for the lack of the Quaternion class. I completely forgot that it was not exposed publicly in the B4A version.
 

rkwan

Member
Licensed User
Thanks, Informatix.

I will just have to wait for the Quaternion class later then
as I am also not expert in the 3D maths!


Thanks & Best Regards,
 
Top