Create Your Custom QMeshNode with Code - erayzesen/godot-quarkphysics GitHub Wiki
In this tutorial, we will create our own custom rectangle QMeshNode
using GDScript to better understand and master the functionality of QMeshNode in QuarkPhysics. We will start by creating particles, then establish spring connections, define our polygon, and subsequently define our UV polygons. Next, we will enable the vector render feature and apply the Godot logo texture to it. Finally, we will attach this to a QSoftBodyNode that we create. All of this will be accomplished using GDScript.
Step 1: Add a QWorldNode to Our Scene
To make QuarkPhysics work, we need to add a QWorldNode under the root node of our scene.
Step 2: Add a Floor to the Scene
Add a QRigidBodyNode to the scene and name it Floor. Set its mode to 'Static', then add a QMeshRectNode as a child to represent the ground with the appropriate dimensions. Finally, position it at the bottom of the scene.
Step 3: Attach a Script to the Scene's Root Node
We will attach a script to the root node of our scene and begin writing our code.
Step 4: Coding
extends Node2D
var rect_width:float=64
var rect_height:float=128
var godot_texture:Texture2D=preload("res://icon.svg")
func _ready() -> void:
var rect_half_width=rect_width*0.5
var rect_half_height=rect_height*0.5
#Create a QSoftBodyNode
var my_body:QSoftBodyNode=QSoftBodyNode.new()
#Set body position
my_body.global_position=Vector2(100,200)
#Activate Shape Matching Feature
my_body.shape_matching=true
#Set rigidity
my_body.rigidity=0.1
#Add body to the scene
add_child(my_body)
##CUSTOM RECT MESH
#Create a QMeshNode
var my_mesh=QMeshNode.new()
# p0.....p1
# ....x....
# p3.....p2
#Create particles
var particle_0=QParticleObject.new()
var particle_1=QParticleObject.new()
var particle_2=QParticleObject.new()
var particle_3=QParticleObject.new()
#Set default particle positions
particle_0.set_position(Vector2(-rect_half_width,-rect_half_height))
particle_1.set_position(Vector2(rect_half_width,-rect_half_height))
particle_2.set_position(Vector2(rect_half_width,rect_half_height))
particle_3.set_position(Vector2(-rect_half_width,rect_half_height))
#Add particles to the mesh
my_mesh.add_particle(particle_0)
my_mesh.add_particle(particle_1)
my_mesh.add_particle(particle_2)
my_mesh.add_particle(particle_3)
#Create springs and add to the mesh
#0 to 1
var spring_0:QSpringObject=QSpringObject.new()
spring_0.configure_with_current_distance(particle_0,particle_1,false)
my_mesh.add_spring(spring_0)
#1 to 2
var spring_1:QSpringObject=QSpringObject.new()
spring_1.configure_with_current_distance(particle_1,particle_2,false)
my_mesh.add_spring(spring_1)
#2 to 3
var spring_2:QSpringObject=QSpringObject.new()
spring_2.configure_with_current_distance(particle_2,particle_3,false)
my_mesh.add_spring(spring_2)
#3 to 0
var spring_3:QSpringObject=QSpringObject.new()
spring_3.configure_with_current_distance(particle_3,particle_0,false)
my_mesh.add_spring(spring_3)
#0 to 2 (cross spring)
var spring_4:QSpringObject=QSpringObject.new()
spring_4.configure_with_current_distance(particle_0,particle_2,false)
my_mesh.add_spring(spring_4)
#Define a Collider Polygon With Particles.
var polygon=[particle_0,particle_1,particle_2,particle_3]
#Set polygon to the mesh
my_mesh.set_polygon(polygon)
#Create UV polygons with particle indexes. We need to two triangle to do that.
# 0----1
# |../
# 3
var uv_polygon_1=[0,1,3]
# 1
# /-|
# 3---2
var uv_polygon_2=[1,2,3]
#Add UV polygons to the mesh
my_mesh.add_uv_map(uv_polygon_1)
my_mesh.add_uv_map(uv_polygon_2)
##ADD THE MESH TO THE BODY
#Add mesh as a child to the body
my_body.add_child(my_mesh)
##ENABLE VECTOR RENDERING FEATURES
my_mesh.enable_vector_rendering=true
my_mesh.enable_fill=true
my_mesh.fill_texture=godot_texture
Result
Congratulations! You have created a custom QMeshNode entirely with GDScript, attached it to a QRigidBodyNode you created via code, enabled rendering features, and started your simulation. What can I say? By the end of this series, you have mastered all the fundamentals of QuarkPhysics. Well done! Now itโs time to use this knowledge to create amazing games and bring your brilliant ideas to life...