{"author_link":"\/users\/tricky-fat-cat","author_name":"Tricky_Fat_Cat","author_uid":"tricky-fat-cat","comments":[],"epoch":1570867345,"event":"LD45","format":"md","ldjam_node_id":175511,"likes":3,"metadata":{"p_key":"137070","p_author":"Tricky_Fat_Cat","p_authorkey":"1145628","p_urlkey":"353346","p_title":"Movement is a key","p_cat":"LDJam ","p_event":"LD45","p_time":"1570867345","p_likes":"3","p_comments":"0","p_status":"WAYBACK","us_key":"1145628","us_name":"Artyom Volkov","us_username":"artyom-volkov","event_start":"1570147200","event_key":"78","event_name":"Ludum Dare 45"},"node":{"_collation":{"body_sanitizer":"TextUtils::SanitizeHTML via existing importer","event":"LD45","removed_author":false},"_superparent":159347,"_trust":8,"author":145628,"body":"Here is some information about movement system in [**HONK**](https:\/\/ldjam.com\/events\/ludum-dare\/45\/honk) which I'll use in my another project [**One Spear, One Kill**](https:\/\/tricky-fat-cat.itch.io\/one-spear-one-kill).\n\nFirst of all, it based on this tutorial - [**A Comprehensive Guide To Time Dilation**](http:\/\/fauxoperativegames.com\/a-comprehensive-guide-to-time-dilation\/). Even though there is an example project, I've written my own system, which I use everywhere in the game.\n\n# Scripts\n\nIf I need to move something with acceleration or deceleration I will add in Step Event these scripts:\n1. MoveObject;\n2. CalculateAcceleratedVelocity;\n3. CalculateDeceleratedVelocity;\n4. CalculateConstantMovement;\n\n## MoveObject\nIt's a straightforward script, but because of using this system everywhere wherever I want, I keep it so simple. As different things require different calculations. For instance, I use both CalculateAcceleratedVelocity and CalculateDeceleratedVelocity in the player object; on the other hand, in projectiles, I use only one of these things at the same time.\n\n**It's very important to call this script before velocity calculations.**\n\n```gml\n\/\/------------\/\/\n\/\/ MoveObject \/\/\n\/\/------------\/\/\n \n\/\/ @param isConstant\n \nvar _isConstant = argument0;\n \nif (_isConstant)\n{\n    x += velocityX * global.TimeFactor;\n    y += velocityY * global.TimeFactor;\n}\nelse\n{\n    x += velocityX;\n    y += velocityY;\n}\n```\n## CalculateAceleratedVelocity\nThis script calculates velocity while an object is accelerating.\n\n```\n\/\/------------------------------\/\/\n\/\/ CalculateAcceleratedVelocity \/\/\n\/\/------------------------------\/\/\n \n\/\/\/ @param direction\n\/\/\/ @param velocityCurrent\n\/\/\/ @param velocityMax\n\/\/\/ @param acceleration\n \nvar _targetDirection = argument0;\nvar _velocityCurrent = argument1;\nvar _velocityMax = argument2;\nvar _acceleration = argument3;\n \n\/\/Delta X and Y calculations\nvar _positionDelta = global.TimeFactor * _velocityCurrent + (0.5 * sqr(global.TimeFactor) * _acceleration);\n_velocityCurrent = min(_velocityCurrent + _acceleration * global.TimeFactor, _velocityMax);\nvelocityX = lengthdir_x(_positionDelta, _targetDirection);\nvelocityY = lengthdir_y(_positionDelta, _targetDirection);\n \nreturn _velocityCurrent;\n```\n\n## CalculateDeceleratedVelocity\nThis script calculates velocity while an object is decelerating.\n\n```\n\/\/------------------------------\/\/\n\/\/ CalculateDeceleratedVelocity \/\/\n\/\/------------------------------\/\/\n \n\/\/\/ @param direction\n\/\/\/ @param velocityCurrent\n\/\/\/ @param friction\n \nvar _targetDirection = argument0;\nvar _velocityCurrent = argument1;\nvar _friction = argument2;\n \nvar _addX = _friction * sign(_velocityCurrent);\nvar _positionDelta = global.TimeFactor * _velocityCurrent + (0.5 * sqr(global.TimeFactor) * _addX);\n_velocityCurrent = max(_velocityCurrent - _friction * global.TimeFactor, 0);\nvelocityX = lengthdir_x(_positionDelta, _targetDirection);\nvelocityY = lengthdir_y(_positionDelta, _targetDirection);\n \nreturn _velocityCurrent;\n```\n\n## CalculateConstantVelocity\nIn case when I need to move something with a constant velocity I use this script.\n\n```\n\/\/---------------------------\/\/\n\/\/ CalculateConstantVelocity \/\/\n\/\/---------------------------\/\/\n \n\/\/\/ @param Velocity\n\/\/\/ @param direction\n \nvar _velocity = argument0;\nvar _direction = argument1;\n \nvelocityX = lengthdir_x(_velocity, _direction);\nvelocityY = lengthdir_y(_velocity, _direction);\n```\n\n# Collisions\nAnd what about collisions? I use a classic script  for checking collisions, and I place it after all movement calculations.\n\nBut be aware, I don't use this script to execute other code, e.g. destroying projectiles, dealing damage etc.\n\n```\n\/\/----------------\/\/\n\/\/ CheckCollision \/\/\n\/\/----------------\/\/\n \n\/\/ @param object\n \nvar _object = argument0;\n \n\/\/ Collision X\nif (place_meeting(x + velocityX, y, _object))\n{\n    while (!place_meeting(x + sign(velocityX), y, _object))\n        {\n            x += sign(velocityX);\n        }\n    velocityX = 0;\n}\n \n\/\/ Collision Y\nif (place_meeting(x, y + velocityY, _object))\n{\n    while (!place_meeting(x, y + sign(velocityY), _object))\n        {\n            y += sign(velocityY);\n        }\n    velocityY = 0;\n}\n```\n\nThank you for reading, feel free to ask questions and give feedback.\n\n[**Original devlog**](https:\/\/tricky-fat-cat.itch.io\/one-spear-one-kill\/devlog\/104152\/new-arena-border-and-movement)","comments":0,"created":"2019-10-12T07:46:19Z","files":[],"files-timestamp":0,"id":175511,"love":3,"love-timestamp":"2020-04-12T03:54:37Z","meta":[],"modified":"2020-04-12T03:54:37Z","name":"Movement is a key","node-timestamp":"2019-10-12T09:24:57Z","parent":160945,"parents":[1,5,9,159347,160945],"path":"\/events\/ludum-dare\/45\/honk\/movement-is-a-key","published":"2019-10-12T08:02:25Z","scope":"public","slug":"movement-is-a-key","subsubtype":"","subtype":"","type":"post","version":528767},"node_metadata":{"n_key":"175511","n_urlkey":"353346","n_parent":"160945","n_path":"\/events\/ludum-dare\/45\/honk\/movement-is-a-key","n_slug":"movement-is-a-key","n_type":"post","n_subtype":"","n_subsubtype":"","n_author":"145628","n_created":"1570866379","n_modified":"1586663677","n_version":"528767","n_status":"WAYBACK"},"source_url":"https:\/\/ldjam.com\/events\/ludum-dare\/45\/honk\/movement-is-a-key","text":"Here is some information about movement system in [**HONK**](https:\/\/ldjam.com\/events\/ludum-dare\/45\/honk) which I'll use in my another project [**One Spear, One Kill**](https:\/\/tricky-fat-cat.itch.io\/one-spear-one-kill).\n\nFirst of all, it based on this tutorial - [**A Comprehensive Guide To Time Dilation**](http:\/\/fauxoperativegames.com\/a-comprehensive-guide-to-time-dilation\/). Even though there is an example project, I've written my own system, which I use everywhere in the game.\n\n# Scripts\n\nIf I need to move something with acceleration or deceleration I will add in Step Event these scripts:\n1. MoveObject;\n2. CalculateAcceleratedVelocity;\n3. CalculateDeceleratedVelocity;\n4. CalculateConstantMovement;\n\n## MoveObject\nIt's a straightforward script, but because of using this system everywhere wherever I want, I keep it so simple. As different things require different calculations. For instance, I use both CalculateAcceleratedVelocity and CalculateDeceleratedVelocity in the player object; on the other hand, in projectiles, I use only one of these things at the same time.\n\n**It's very important to call this script before velocity calculations.**\n\n```gml\n\/\/------------\/\/\n\/\/ MoveObject \/\/\n\/\/------------\/\/\n \n\/\/ @param isConstant\n \nvar _isConstant = argument0;\n \nif (_isConstant)\n{\n    x += velocityX * global.TimeFactor;\n    y += velocityY * global.TimeFactor;\n}\nelse\n{\n    x += velocityX;\n    y += velocityY;\n}\n```\n## CalculateAceleratedVelocity\nThis script calculates velocity while an object is accelerating.\n\n```\n\/\/------------------------------\/\/\n\/\/ CalculateAcceleratedVelocity \/\/\n\/\/------------------------------\/\/\n \n\/\/\/ @param direction\n\/\/\/ @param velocityCurrent\n\/\/\/ @param velocityMax\n\/\/\/ @param acceleration\n \nvar _targetDirection = argument0;\nvar _velocityCurrent = argument1;\nvar _velocityMax = argument2;\nvar _acceleration = argument3;\n \n\/\/Delta X and Y calculations\nvar _positionDelta = global.TimeFactor * _velocityCurrent + (0.5 * sqr(global.TimeFactor) * _acceleration);\n_velocityCurrent = min(_velocityCurrent + _acceleration * global.TimeFactor, _velocityMax);\nvelocityX = lengthdir_x(_positionDelta, _targetDirection);\nvelocityY = lengthdir_y(_positionDelta, _targetDirection);\n \nreturn _velocityCurrent;\n```\n\n## CalculateDeceleratedVelocity\nThis script calculates velocity while an object is decelerating.\n\n```\n\/\/------------------------------\/\/\n\/\/ CalculateDeceleratedVelocity \/\/\n\/\/------------------------------\/\/\n \n\/\/\/ @param direction\n\/\/\/ @param velocityCurrent\n\/\/\/ @param friction\n \nvar _targetDirection = argument0;\nvar _velocityCurrent = argument1;\nvar _friction = argument2;\n \nvar _addX = _friction * sign(_velocityCurrent);\nvar _positionDelta = global.TimeFactor * _velocityCurrent + (0.5 * sqr(global.TimeFactor) * _addX);\n_velocityCurrent = max(_velocityCurrent - _friction * global.TimeFactor, 0);\nvelocityX = lengthdir_x(_positionDelta, _targetDirection);\nvelocityY = lengthdir_y(_positionDelta, _targetDirection);\n \nreturn _velocityCurrent;\n```\n\n## CalculateConstantVelocity\nIn case when I need to move something with a constant velocity I use this script.\n\n```\n\/\/---------------------------\/\/\n\/\/ CalculateConstantVelocity \/\/\n\/\/---------------------------\/\/\n \n\/\/\/ @param Velocity\n\/\/\/ @param direction\n \nvar _velocity = argument0;\nvar _direction = argument1;\n \nvelocityX = lengthdir_x(_velocity, _direction);\nvelocityY = lengthdir_y(_velocity, _direction);\n```\n\n# Collisions\nAnd what about collisions? I use a classic script  for checking collisions, and I place it after all movement calculations.\n\nBut be aware, I don't use this script to execute other code, e.g. destroying projectiles, dealing damage etc.\n\n```\n\/\/----------------\/\/\n\/\/ CheckCollision \/\/\n\/\/----------------\/\/\n \n\/\/ @param object\n \nvar _object = argument0;\n \n\/\/ Collision X\nif (place_meeting(x + velocityX, y, _object))\n{\n    while (!place_meeting(x + sign(velocityX), y, _object))\n        {\n            x += sign(velocityX);\n        }\n    velocityX = 0;\n}\n \n\/\/ Collision Y\nif (place_meeting(x, y + velocityY, _object))\n{\n    while (!place_meeting(x, y + sign(velocityY), _object))\n        {\n            y += sign(velocityY);\n        }\n    velocityY = 0;\n}\n```\n\nThank you for reading, feel free to ask questions and give feedback.\n\n[**Original devlog**](https:\/\/tricky-fat-cat.itch.io\/one-spear-one-kill\/devlog\/104152\/new-arena-border-and-movement)","title":"Movement is a key","wayback_source":[]}