Tutorial (Godot Engine v3 - GDScript) - Verlet Chain (v0.01)!

in #utopian-io7 years ago (edited)

Godot Engine Logo v3 (Beginner).png Tutorial

<p dir="auto"><img src="https://images.hive.blog/768x0/https://steemit-production-imageproxy-thumbnail.s3.amazonaws.com/DQmeRXYK5hTURHD9TqUuGi4vPsockATFzcKFZdawUYpq24B_1680x8400" srcset="https://images.hive.blog/768x0/https://steemit-production-imageproxy-thumbnail.s3.amazonaws.com/DQmeRXYK5hTURHD9TqUuGi4vPsockATFzcKFZdawUYpq24B_1680x8400 1x, https://images.hive.blog/1536x0/https://steemit-production-imageproxy-thumbnail.s3.amazonaws.com/DQmeRXYK5hTURHD9TqUuGi4vPsockATFzcKFZdawUYpq24B_1680x8400 2x" /> ...learn about Verlet Chains! <h1>What Will I Learn? <p dir="auto">Hi, <p dir="auto">I posted a <a href="https://steemit.com/utopian-io/@sp33dy/tutorial-godot-engine-v3-gdscript-physics-verlet-vs-rigidbody" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">tutorial at the weekend that demonstrated both Verlet Integration and Godot's RigidBody physics. At the end of the tutorial, I mentioned the fact that I had developed a chain and rope movements, which can also be applied to bridges etc. <p dir="auto">This Tutorial will explain how to create Chains, dangling from a ceiling, using Verlet Integration; I plan to do the same with Godot Physics in a future post. <p dir="auto">Why have I bothered? I have several game ideas that require chain and rope type effects, so I thought it would be wise to prototype it, so that I can implement it in a game without the fear of not knowing how to achieve it! <blockquote> <p dir="auto">Writing games can often be a journey down the research and experimentation path! Which, in itself, can lead to new ideas and discovery; so I find it fun! <p dir="auto">This is what we are going to build: <p dir="auto"><center><br /> <img src="https://images.hive.blog/0x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521543099/jsgitb7huanx2d4it7z9.gif" alt="chains.gif" /><br /> <br /> <center>Note: the recording method does so at 30fps<br /> <center>, therefore, it ruins the quality throughout this article! <blockquote> <p dir="auto"><em>NOTE: This is the first iteration of the code, therefore it is NOT perfect! I.E. as you can see in the video above, the movement of the chain appears to grow with momentum, when actually, it shouldn't become so extreme! This will be ironed out in a future post. <hr /> <h3>Assumptions <blockquote> <ul> <li>You have installed <a href="https://steemit.com/gamedev/@sp33dy/installing-godot-engine-v3-0-windows" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Godot Engine v3.0 <li>You are familiar with GDScipt <h3>You will <ul> <li>Overview <li>Add a Chain Loop instance <li>Add a Chain Link instance <li>Add the Chain instance <li>Add control to the Loop! <h1>Requirements <p dir="auto">You must have installed <a href="https://steemit.com/gamedev/@sp33dy/installing-godot-engine-v3-0-windows" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Godot Engine v3.0. <p dir="auto">All the code from this tutorial is provided in a <a href="https://github.com/sp33dy/Godot-v3-Tutorials-Competent" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">GitHub repository. I'll explain more about this, towards the end of the tutorial. You may want to download this <hr /> <h1>Overview <p dir="auto">A simulated chain is very easy to implement, in fact, so is a rope because it is implemented in the same way! <p dir="auto">Effectively, we need to build a list of objects, that when attached to each other, force them to interact. We are going to use Verlet Integration, which ensures a list of objects have physics applied to them, given they have a vector of movement. <p dir="auto">The objects we are going to create have been termed for my purposes, NOT, by some scientific formula way. If you go looking at Wikipedia, you can soon get lost in lots of algorithms, symbols and mathematical meaning. I'm not here to fully explain the maths! IN fact, refer to <a href="https://www.youtube.com/watch?v=3HjO_RGIjCU" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Coding Math youtube channel tutorial which does a FAR better job than I! <p dir="auto">The terms I'm going to use are: <ul> <li>Chain Loop: This is the side-on chain ring <img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521543601/onkcrk12ysz7h8ysmzbg.png" alt="Loop.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521543601/onkcrk12ysz7h8ysmzbg.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521543601/onkcrk12ysz7h8ysmzbg.png 2x" /> <li>Chain Loop Anchor: The fixed, non-movable Chain Loop <img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521543630/dluksats4id3w2cvpadk.png" alt="Anchor.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521543630/dluksats4id3w2cvpadk.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521543630/dluksats4id3w2cvpadk.png 2x" /> <li>Chain Link: The side of chain link <img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521543793/ey3lxih0vxqakzqcls8j.png" alt="Link.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521543793/ey3lxih0vxqakzqcls8j.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521543793/ey3lxih0vxqakzqcls8j.png 2x" /> <p dir="auto">I've seen many GREAT art assets, that provide really convincing chains. However, to simulate the moving, they need to be broken into their constituent parts; allowing them to be overlayed to build a chain. <p dir="auto">Building the chain is very simple: <ul> <li>Plot each Loop up to its Size + Gap distance from the next <li>Overlay a Link, exactly halfway between the two Loops and ensure it rotated at the angle between them <p dir="auto">_Note: It is important to note the starting angles of objects, i.e. 0 degrees faces the right, therefore the link object must be horizontal to being with. <p dir="auto">All physics shall be applied to every Loop object and the Link object shall restrict the loops, as per the Point / Sticks metaphor in the Coding Maths tutorial. <p dir="auto">Let's get on with it! <h1>Add a Chain Loop instance <p dir="auto">A Chain Loop is the first object to create, therefore, please create a new Instance and add a Sprite as the Root node. <p dir="auto">I then assign the Loop image into the texture, which is a 24 x 24 pixel circle. <p dir="auto">Please attach a Script and enter the following: <pre><code>extends Sprite <blockquote> <p dir="auto">Extend the Sprite Class <pre><code>const CHAIN_LOOP_GAP = 2 <blockquote> <p dir="auto">Define a gap between the Loops (play with this when the code is running, what is important to remember? Hint, think about the links) <pre><code>export var anchor = false <blockquote> <p dir="auto">Define an exposed flag (for the editor), to be able to define this loop as an Anchor point. I will discuss this further down, in the Chain section. <pre><code>var pos = Vector2() var oldPos = Vector2() <blockquote> <p dir="auto">Allocate local pos and oldPos variables. The Sprite already has a position and global_position properties, but we need these two variables to calculate vectors each frame. In the previous tutorial, these were held in the main code (i.e. our Chain below); however, I felt moving them into the Loop object makes both more sense and keeps the other cleaner! <pre><code>func setPosition(index): pos = Vector2(0, index + 1) pos *= (texture.get_size().y + CHAIN_LOOP_GAP) oldPos = pos position = pos <blockquote> <p dir="auto">This defines a new function that will position the Loop on screen, based on a position index number. I.E. Each loop is placed below the last. This again could be kept in the Chain object, but for me, logically it makes sense to keep it inside the object that is going to use it. <blockquote> <p dir="auto">The function sets the pos class variable to the index position (plus one to be lower than the Anchor) and then multiplies it by the size and gap required per Loop<br /> The pos value is also placed into the oldPos and into the position property; forcing the Loop to a position on the screen <p dir="auto">Make sure you save the Scene and script, I placed them into a folder structure of /Chains/Loop/* <p dir="auto">You can add the instance to the Game Scene, and you should be able to see a Loop; nothing exciting so far! <ul> <li>Add a Chain Link instance<br /> The Chain Link is even less exciting! Create a new instance with a Sprite root Node named Chain Link. Add the texture, ensuring it is lying horizontally: <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521544909/xgalurnxvzuj3cx4ol8i.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521544909/xgalurnxvzuj3cx4ol8i.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521544909/xgalurnxvzuj3cx4ol8i.png 2x" /> <p dir="auto">As stated previously, angle 0 faces Right, therefore the link must face right because it will be rotated to match the angle between two Loops. <p dir="auto">Finally, add a script: <pre><code>extends Sprite var parentLoop = null var childLoop = null <blockquote> <p dir="auto">This again, extends a Sprite Class <blockquote> <p dir="auto">The two points that would normally be kept in a dictionary of the main code has been moved into the object instead, this, in my opinion, maintains clarity as to the purposes of the variables. These two simply point to the parent and child loop; the parent is the Loop above and the child is the Loop below. <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521545272/x8kbrdbou1zz7dlhiqzq.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521545272/x8kbrdbou1zz7dlhiqzq.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521545272/x8kbrdbou1zz7dlhiqzq.png 2x" /> <p dir="auto">I saved mine in the folder /Chain/Link/* <p dir="auto">All done! Two down, one object to go... <ul> <li>Add the Chain instance<br /> The Chain instance is where ALL the magic occurs. It brings the Loops and Links together to form a beautiful chain. <p dir="auto">Please add a new instance with a Node2D as the root Node, named Chain. We use Node2D, because the 'Chain' can be placed anywhere on the Screen, but is not a Sprite itself. <p dir="auto">Please add an instance of the Loop as a child, which we are going to call AnchorLoop. We are reusing the Loop object, to form the Anchor: <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521545670/aayxm5ahshemoeap3rji.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521545670/aayxm5ahshemoeap3rji.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521545670/aayxm5ahshemoeap3rji.png 2x" /> <p dir="auto">In the Inspector, override the Loop image texture with the Anchor Loop image (it has the fixed connector on top). <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521545718/xpqpzf11yy4zoccshllw.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521545718/xpqpzf11yy4zoccshllw.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521545718/xpqpzf11yy4zoccshllw.png 2x" /> <p dir="auto">We also enable the 'Anchor' property, that was exposed! The Chain code will use this flag to ensure it keeps it stationary; i.e. it won't apply movement to it. This is another example of why I feel placing this type of property in the object is better practice than keeping in a list of dictionary items that get lost! Also, any other script can look up this node uses its condition. <p dir="auto">Please attach a Script to the Chain Node2D. This is the most involved script of all: <pre><code>extends Node2D const CHAIN_LOOP = preload("res://Chain/Loop/ChainLoop.tscn") const CHAIN_LINK = preload("res://Chain/Link/ChainLink.tscn") const CHAIN_LINK_LENGTH = 26 const GRAVITY = 0.6 const RESISTANCE = 0.98 const FRICTION = 0.90 var loops = [] var links = [] export (int) var linkCount = 10 onready var SCREEN_SIZE = get_viewport().size <blockquote> <p dir="auto">Extend the Node2D class <blockquote> <p dir="auto">Declare preloaded constants for the Loop and Link instances; allowing them to be instanced <blockquote> <p dir="auto">Declare the chain length (24 pixels) + 2 for the Gap <blockquote> <p dir="auto">Declare the physics properties, including Gravity, air Resistance and Friction <blockquote> <p dir="auto">Declare two arrays for the Loops and Links (to accelerate the code, rather than finding objects in the tree)) <blockquote> <p dir="auto">Declare an exposed variable for the count of links to create; this can be set in the editor, allowing for differing lengths to be 'grown' <blockquote> <p dir="auto">Finally, obtain the screen size from the viewport (although this needs to be a fetch of the screen size in the ProjectSettings if 2D stretching is enabled). <p dir="auto">Next, we build the chain when the Node is <em>ready: <pre><code>func _ready(): addAnchorLoop() for i in range (linkCount): addLoop(i) addLink(i) <blockquote> <p dir="auto">Add the Anchor object (already declared in the tree) to the Loop array <blockquote> <p dir="auto">Then loop for the number of links configured, adding a Loop and then a Link instance <pre><code>func addAnchorLoop(): loops.append($AnchorLoop) <blockquote> <p dir="auto">The function to add the Anchor to the Loops array. This function isn't necessary per say, as it could have been placed inline in the <em>ready function; however, I like to create a specific meaning to my code. This is my preference. Given this is initialisation code, performance is NOT a factor, readability is. <pre><code>func addLoop(index): var loop = CHAIN_LOOP.instance() loop.setPosition(index) loops.append(loop) add_child(loop) <blockquote> <p dir="auto">For the next 'loop' index, create the Chain Loop, set its position (via the instance's internal function, see above) and then both append it the array and place it on the screen <pre><code>func addLink(index): var link = CHAIN_LINK.instance() link.parentLoop = loops[index] link.childLoop = loops[index + 1] links.append(link) add_child(link) <blockquote> <p dir="auto">Do the same as we did with the add loop function, creating the Link and setting the parent and child Loop points; remember, the Anchor will have been added as index 0, hence, the childhood is + 1. <p dir="auto">Next, we declare the next frame process function: <pre><code>func _process(delta): updateLoops() constrainLinks() renderFrame() <blockquote> <p dir="auto">For each frame, update the Loops (i.e. apply physics), then constrain their positions by the links and finally render them <blockquote> <p dir="auto">FYI, I experimented with the idea of implementing the physics inside each Loop node itself; DONT! After doing so, I recognised the bad decisions I made: <ul> <li>There is no guarantee on the order of processing by Godot Engine; for the chains, we need the order to be processed top down! <li>It is slow! Using a fixed array is far faster than Godot to processing the <em>process function of each node; because that has an overhead. <li>We perform a constrain after applying the physics. We can do both of those before changing the final position of the Sprite! However, if the logic is placed inside the Node Script, they have to position and reposition themselves, which looks both ugly and jittery! <blockquote> <p dir="auto">It is far better to process the main loop as we have it. This will be important for the "Space Invaders" clone! <pre><code>func updateLoops(): for loop in loops: if !loop.anchor: applyMovement(loop) func applyMovement(loop): var velocity = calcVelocity(loop) loop.oldPos = loop.pos loop.pos += velocity loop.pos.y += GRAVITY func calcVelocity(loop): return (loop.pos - loop.oldPos) * RESISTANCE * FRICTION <blockquote> <p dir="auto">The update of the Loops is constructed through three functions: <ol> <li>The first loops through each Loop and calls the movement function, if it isn't the Anchor Loop (as stated previously, we have to prevent it from moving; unless it is an engine! I.E. an Engine might move it side to side! <li>The second function applies the physics to the Node. The velocity is first calculated (via the the third function). The current position is moved into the old position, and then both the velocity and gravity forces are added to the current position. <li>Calculates the velocity, using the old and current positions. <p dir="auto">func constrainLinks():<br /> for link in links:<br /> var vector = calcLinkVector(link)<br /> var distance = link.childLoop.pos.distance_to(link.parentLoop.pos)<br /> var difference = CHAIN_LINK_LENGTH - distance<br /> var percentage = difference / distance<br /> vector *= percentage<br /> link.childLoop.pos += vector <blockquote> <p dir="auto">The update loops function constrains the Loops, by calling this function. <blockquote> <p dir="auto">It loops through all the links and calculates the vector between the Parent and the Child (see next function). <blockquote> <p dir="auto">It then calculates the distance between the Parent and Child positions. <blockquote> <p dir="auto">It then calculates the difference between the distance and the Link length. <blockquote> <p dir="auto">A percentage can then be calculated, which is then applied to the original vector between then Parent and Child. The adjustment is applied to the Child position, forcing it towards the Parent, maintaining the correct distance. Please refer to the <a href="https://www.youtube.com/watch?v=tAd7ttKbugA" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Coding Maths for an explanation of this. <pre><code>func calcLinkVector(link): return link.childLoop.pos - link.parentLoop.pos <blockquote> <p dir="auto">This function is used by the previous to calculate the vector between the child and parent positions. <p dir="auto">The last of the update Loops function calls the render frame function: <pre><code>func renderFrame(): for link in links: link.childLoop.position = link.childLoop.pos positionLinkBetweenLoops(link) rotateLinkBetweenLoops(link) <blockquote> <p dir="auto">Each link is worked through, with its child Loop positioned on screen (the parent is ignored because the first link will refer to the Anchor; which won't move. Then, the child of this link becomes the parent of the next, which has already been moved!) <blockquote> <p dir="auto">The next step is to position the Link, inbetween the Parent and Child Loops, see the next function <blockquote> <p dir="auto">Finally, the Link is rotated by the second function below <pre><code>func positionLinkBetweenLoops(link): link.position = link.parentLoop.pos + (calcLinkVector(link) / 2) <blockquote> <p dir="auto">Positioning the Link between the Loops is relatively straightforward. Calculate the vector between the two Loops, divide it in half and add it to the Parent Loop position. <pre><code>func rotateLinkBetweenLoops(link): link.global_rotation = link.parentLoop.pos.angle_to_point(link.childLoop.pos) <blockquote> <p dir="auto">Rotating the Link is also trivial because Godot provides a function on Vector2 that calculates the 'Angle to point' between the two. <p dir="auto">Add a chain to the Game Scene and run it! <p dir="auto"><center><br /> <img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521547940/eaaj2yj4yquzcobbblot.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521547940/eaaj2yj4yquzcobbblot.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521547940/eaaj2yj4yquzcobbblot.png 2x" /><br /> <p dir="auto">... oh!! They show, but there is no movment, nor a way to interact! <h1>Add control to the Loop! <p dir="auto">We can easily remedy this issue, but providing mouse/touch control to the Chain Loop instance! There is a <em>Control node, which detects when the mouse/touch occurs on it; i.e. enter or exits. Let's use this. <p dir="auto">Open the Chain Loop instance. Add a Control as a parent: <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521548056/fg5gdyjovqq4bc5dqxfc.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521548056/fg5gdyjovqq4bc5dqxfc.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521548056/fg5gdyjovqq4bc5dqxfc.png 2x" /> <p dir="auto">Ensure you set the Inspector/Rect/Position values so that the control overlaps the image in the 2D View: <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521548112/fvjgirqgy5qov9q4baob.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521548112/fvjgirqgy5qov9q4baob.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521548112/fvjgirqgy5qov9q4baob.png 2x" /> <p dir="auto">I had to set position to (-12, -12): <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521548235/sbm9dvvpqdggbfdv06xo.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521548235/sbm9dvvpqdggbfdv06xo.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521548235/sbm9dvvpqdggbfdv06xo.png 2x" /> <p dir="auto">Next, the on entered action of the control needs to be wired to the Script of the ChainLoop instance. Open the Node tab of the Inspector: <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521548295/fdeugjl77ekuncp6e46v.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521548295/fdeugjl77ekuncp6e46v.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521548295/fdeugjl77ekuncp6e46v.png 2x" /> <p dir="auto">Double click the <em>mouse_entered() function and then select the ChainLoop instance: <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521548346/dxf5sqhuudqqhg0mzlup.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521548346/dxf5sqhuudqqhg0mzlup.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1521548346/dxf5sqhuudqqhg0mzlup.png 2x" /> <p dir="auto">Click the Connect button at the bottom of the page and the Script will open with a new function. Please change it to: <pre><code>func _on_Control_mouse_entered(): if !anchor: over = true modulate = Color(0x0000ffff) <blockquote> <p dir="auto">If the Loop Node is not the anchor, set the Class variable <em>over to true (this needs to be added, see next line of code) and then we set the Sprite's colour to Blue <pre><code>var over = false <blockquote> <p dir="auto">Please add this variable to the top of the script. It is used to flag when the user is interacting with the Loop Node. <p dir="auto">Do the same for the <em>mouse_exited() function and change the code to this: <pre><code>func _on_Control_mouse_exited(): if !anchor: over = false modulate = Color(0xffffffff) <blockquote> <p dir="auto">This resets the over flag and ensures the Loop sprite is returned to its original colour (i.e. the mouse has moved away from the Sprite Control!) <pre><code>func _process(delta): if Input.is_mouse_button_pressed(BUTTON_LEFT) and over: var mousePos = get_viewport().get_mouse_position() var vector = mousePos - global_position oldPos = pos pos += vector <blockquote> <p dir="auto">The above <em>process function is required to process the Loop every frame to determine whether the mouse is over it and the Left Button has been pressed. If it has, calculate the vector from the Loop Node to the Mouse and then move it towards it! It is important the local <em>pos is used and NOT the Sprites position because the Chain code will ensure the Link restrain is applied (clever, yes?) <p dir="auto">If you run the game now, interaction is enabled, BUT, when you move and let go of a selected Node, it seems to "jerk" awkwardly; that's because Godot Engine maybe in the middle of the Chain movement logic. We need to alter the Chain function: <pre><code>func updateLoops(): for loop in loops: if !loop.anchor _and !loop.over_: applyMovement(loop) <blockquote> <p dir="auto">The additional 'and condition' is applied, only allowing movement to occur to a Node if it does not have a mouse over it! This will smooth the movement after the user releases the button. <p dir="auto">Try running! It should mirror my example, as seen in the animated Gif at the top of this tutorial! <h1>Finally <p dir="auto">This tutorial has shown you how to implement a Verlet Chain! I hope it is useful and that someone might use it in a game; i have a few ideas of my own. <p dir="auto">There are some improvements to make: <ul> <li><p dir="auto">Prevent the Chains to whip around too quickly. One option is to use the angles of the previous loop and the next to ensure a threshold is not crossed; i.e. the link would restrict movement, because the chain would hit itself <li><p dir="auto">It would be good for the chain loops and links above a user moved loop to also move, i.e. so the entire chain would subtly move <p dir="auto">I will look into this in the future, and will provide a v1.00; unless someone here figures it, then let me know! <p dir="auto">My next task is to provide this as a RigidBody solution using Godot's Joints! <p dir="auto">I'm also going to use this approach for the 'Space Invader' clone; i.e. for the ship formations and to direct them. Have a google on Spring Joints!!! <p dir="auto">Please do comment and ask questions! I'm more than happy to interact with you. <h1>Sample Project <p dir="auto">I hope you've read through this Tutorial, as it will provide you with the hands-on skills that you simply can not learn from downloading the sample set of code. <p dir="auto">However, for those wanting the code, please download from <a href="https://github.com/sp33dy/Godot-v3-Tutorials-Competent" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">GitHub. <p dir="auto">You should then Import the "Verlet Chain Swing" folder into Godot Engine. <h1>Other Tutorials <h4>Beginners <blockquote> <ul> <li><a href="https://steemit.com/gamedev/@sp33dy/installing-godot-engine-v3-0-windows" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Install Godot Engine 3.0 <li><a href="https://steemit.com/gamedev/@sp33dy/first-demo-godot-engine-v3-0" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Installing your First Demo <li><a href="https://steemit.com/utopian-io/@sp33dy/tutorial-godot-engine-v3-gdscript-your-first-moving-sprite" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Your first Sprite! <li><a href="https://steemit.com/utopian-io/@sp33dy/tutorial-godot-engine-v3-gdscript-move-your-sprite" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Move your first Sprite! <li><a href="https://steemit.com/utopian-io/@sp33dy/tutorial-godot-engine-v3-gdscript-create-lots-of-sprites" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Create lots of Sprites! <li><a href="https://steemit.com/utopian-io/@sp33dy/tutorial-godot-engine-v3-gdscript-sprite-formations" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Sprite formations! <li><a href="https://steemit.com/utopian-io/@sp33dy/tutorial-godot-engine-v3-gdscript-smooth-movement#comments" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Smooth Movement! <li><a href="https://steemit.com/utopian-io/@sp33dy/tutorial-godot-engine-v3-gdscript-invader-graphics" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Invader Graphics! <li><a href="https://steemit.com/utopian-io/@sp33dy/tutorial-godot-engine-v3-gdscript-player-ship" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Player Ship! <li><a href="https://steemit.com/utopian-io/@sp33dy/tutorial-godot-engine-v3-gdscript-bullets" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Bullets <li><a href="https://steemit.com/utopian-io/@sp33dy/tutorial-godot-engine-v3-gdscript-collision-dectection" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Collision Detection! <li><a href="https://steemit.com/utopian-io/@sp33dy/tutorial-godot-engine-v3-gdscript-colour-use" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Colour use! <h4>Competent <blockquote> <ul> <li><a href="https://steemit.com/utopian-io/@sp33dy/tutorial-godot-engine-v3-gdscript-custom-tilemap" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Custom TileMaps <li><a href="https://steemit.com/utopian-io/@sp33dy/tutorial-godot-engine-v3-gdscript-custom-tilemap-scrolling" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Custom TileMap Scrolling <li><a href="https://steemit.com/utopian-io/@sp33dy/tutorial-godot-engine-v3-gdscript-generic-screen-wrapping-sprite" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Screen Wrapping Sprite <li><a href="https://steemit.com/utopian-io/@sp33dy/tutorial-godot-engine-v3-gdscript-generic-screen-wrapping-node" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Screen Wrap Node <li><a href="https://steemit.com/utopian-io/@sp33dy/tutorial-godot-engine-v3-gdscript-physics-verlet-vs-rigidbody" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Verlet vs RigidBody Physics <p dir="auto"><br /><hr /><em>Posted on <a href="https://utopian.io/utopian-io/@sp33dy/tutorial-godot-engine-v3-gdscript-verlet-chain-v0-01" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Utopian.io - Rewarding Open Source Contributors<hr /><p>
Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @roj, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

Thank you Roj; especially for the effort it must take to read so many of these!

click here.Congratulations @sp33dy, this post is the third most rewarded post (based on pending payouts) in the last 12 hours written by a User account holder (accounts that hold between 0.1 and 1.0 Mega Vests). The total number of posts by User account holders during this period was 2973 and the total pending payments to posts in this category was $5526.98. To see the full list of highest paid posts across all accounts categories,

If you do not wish to receive these messages in future, please reply stop to this comment.

Wow, that is prett amazing! I'm stoked by that revelation! ..and I've only just begun posting :)

Hey @sp33dy I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x