Voxelers / 3d

3D in Voxelers
Apache License 2.0
9 stars 1 forks source link

Explore Godot 3D and implement voxels collisions in physics testbed #1

Open acs opened 4 years ago

acs commented 4 years ago

Godot 3D tutorial: https://docs.godotengine.org/en/stable/tutorials/3d/introduction_to_3d.html#spatial-node

Starting to learn with it!

This tutorial does not cover the basics pretty well. Let's find others:

https://www.reddit.com/r/godot/comments/an0iq5/godot_tutorials_list_of_video_and_written/

Godot and MagicaVoxel: https://www.reddit.com/r/godot/comments/9497gz/how_to_3d_voxels_to_pixel_art_with_viewports/

The 3D tutorials are not as complete as the 2D ones (https://godotforums.org/discussion/15767/request-beginner-3d-step-by-step-tutorial) and most of them are inside videos.

For example:

https://www.youtube.com/watch?v=VeCrE-ge8xM

Let's take a look to this eBook: https://godottutorials.pro/free-ebook-godot-game-development/ link

This tutorial seems to be a good reference: https://godottutorials.pro/fps-godot-tutorial/ (the same than the one included in the book).

And this article bout Godot tutorials for Beginners is also pretty interesting, pointing to 3D resources: https://conceptartempire.com/godot-tutorials/

I would focus in this youtube tutorial:

https://www.youtube.com/watch?v=VeCrE-ge8xM Godot 3.1: Creating a Simple 3D Game: Part 1 (Intro, Nodes & 3D Physics) #GodotEngine

All of those were started in https://github.com/Voxelers/mcthings/issues/137

acs commented 4 years ago

Also, a good learning resource are the Godot demos about 3D. Once we have learnt the basic concepts around 3D learning by examples could be the best option.

The final of the trip is implementing https://github.com/Voxelers/mcthings/issues/137

acs commented 4 years ago

Ok, let's focus on: https://godottutorials.pro/fps-godot-tutorial/ I will try to follow it as I did for the 2D game tutorial.

I have tested the finished project at it is promising. More motivation to complete the tutorial:

Screenshot from 2020-08-24 23-00-20

acs commented 4 years ago

One of the most important things in 3D is to feel comfortable moving in the 3D world. Godot tries to mimic Blender but I can not reproduce the experience. I can zoom in and zoom out with "scrolling mouse". And I can rotate using the 3D axis GUI at top right like in Blender, or using right click and the mouse moves at the same time.

According to the Settings:

Screenshot from 2020-08-25 11-54-28

It should be like Blender. Maybe the issue is that I am using a touch pad.

Looking to the video: https://www.youtube.com/watch?v=0Kr5-NXzmfs

SHIFT-F enters you in Freelook which is the best mode to move around your 3D world.

With Right Click pressed y AWSD you can move similar to freelook. And also you can move the mouse to rotate.

Ok, enough to continue now learning with the tutorial.

acs commented 4 years ago

Ok, the tutorial reached a point when it says: "Go ahead and create an environment." At this momento we must do a lof of things alone. Resize/scale the ground so it holds all the objects. Start adding all the objects, rotating them as needed to recreate the environment. I will try to do it but probably, the tutorial must give some steps on howto create the "environment".

For example, for scaling the ground so we can place over it the full scene:

Screenshot from 2020-08-26 10-09-39

With it a 10x1x10 (x, y, z, 100 squares) ground is created.

acs commented 4 years ago

I have found that it is critical to understand how to position the camera in order to see the scene. You must understand how to do it in order to be comfortable. Use the Preview button to see how everything looks from the camera vision.

Screenshot from 2020-08-26 10-24-50

And of course playing with lights is critical. In this case example I am turning of/off a directorial light.

Screenshot from 2020-08-26 10-30-54

lights3d

acs commented 4 years ago

The tutorial is going pretty well. After creating the environment from the models, partially, I have created the player, with her camera, so it is a first person shooter: the camera are the eyes of the player!

And right now, I am learning howto implement the movement of the player. It is pretty similar to what we did in 2D but in 3D, so it is a bit more complex. In the process I have seen that in GDScript you can use types!

var moveSpeed : float = 5.0

The basic idea is the same:

func _input (event): 
    # did the mouse move?
    if event is InputEventMouseMotion:
        mouseDelta = event.relative
func _process (delta):

    # rotate camera along X axis
    camera.rotation_degrees -= Vector3(rad2deg(mouseDelta.y), 0, 0) * lookSensitivity * delta

    # clamp the vertical camera rotation
    camera.rotation_degrees.x = clamp(camera.rotation_degrees.x, minLookAngle, maxLookAngle)

    # rotate player along Y axis
    rotation_degrees -= Vector3(0, rad2deg(mouseDelta.x), 0) * lookSensitivity * delta

    # reset the mouse delta vector
    mouseDelta = Vector2()

In delta we have the delta time between frames. It is used to measure the time the mouse has been pressed in order to adjust the amount of rotation to be done. With "lookSensitive" we can adjust the rotation speed. mouseDelta comes from the _input method. Not yet sure why to rotate camera in X we use the mouseDelta.y.

It is curious that for "godot y" rotation we use the camera, and for x, we use the player scene complete. Not sure why yet.

# called every physics step
func _physics_process (delta):
        # reset the x and z velocity
    vel.x = 0
    vel.z = 0

    var input = Vector2()

    # movement inputs
    if Input.is_action_pressed("move_forward"):
        input.y -= 1
    if Input.is_action_pressed("move_backward"):
        input.y += 1
    if Input.is_action_pressed("move_left"):
        input.x -= 1
    if Input.is_action_pressed("move_right"):
        input.x += 1

    # normalize the input so we can't move faster diagonally
    input = input.normalized()

    # get our forward and right directions
    var forward = global_transform.basis.z
    var right = global_transform.basis.x
    # set the velocity
    vel.z = (forward * input.y + right * input.x).z * moveSpeed
    vel.x = (forward * input.y + right * input.x).x * moveSpeed

    # apply gravity
    vel.y -= gravity * delta

    # move the player
    vel = move_and_slide(vel, Vector3.UP)

    # jump if we press the jump button and are standing on the floor
    if Input.is_action_pressed("jump") and is_on_floor():
        vel.y = jumpForce

This is the more complex code. Here we control the velocity of the player in 3D. x and z and computed using the constant moveSpeed (5) and the keys pressed. y is computed using the gravity force (12) and the time passed under this force.

And finally, if we press jump key (space) and we are on the floor, they y velocity is adjusted to jump!

And with the above code, all the logic for 3D moving and rotating the player is implemented. Cool!

It is pretty similar to what we did for 2D but we need to include the third dimension.

acs commented 4 years ago

Ok, shooting also implemented. It is pretty easy to understand it.

shooting

It is mostly physics all.

acs commented 4 years ago

Don't forget to implement https://github.com/Voxelers/mcthings/issues/122 in Godot!

The goal is to add two models created in MagicaVoxel and collide them inside a Godot scene!