From cb1348851a9f5e97df8396e6529b24e65b874e45 Mon Sep 17 00:00:00 2001 From: ronny abraham Date: Wed, 11 Sep 2024 18:22:40 +0300 Subject: [PATCH] adding notes on body-body collision --- ch01/lesson-12.org | 60 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/ch01/lesson-12.org b/ch01/lesson-12.org index 6617ff8..8973926 100644 --- a/ch01/lesson-12.org +++ b/ch01/lesson-12.org @@ -3,7 +3,10 @@ #+title: Lesson 12: Player-Enemy Collisions -[[../../arpg/toc.org][< Back to Main TOC]] +* Links +- [[../../arpg/toc.org][< Back to Main TOC]] +- [[https://youtu.be/WVQkOWY3zxQ?si=UILX6fBQ_Zw1cZoK][Player-enemy collision]] + * Notes @@ -16,14 +19,61 @@ - select 'CapsuleShape2D' from dropdown *** modify shape -- Go to Inspector->CollisionShape2D->Node2D -- Open 'Transform' +- Inspector->CollisionShape2D->Node2D->Transform - Go to Rotation and set to 90 degrees - Modify shape via handles to adjust it over sprite -*** z-index -- set :w +*** set the z-index +- in CollisionShape2D to 0 +- in sprite root node to 1 +*** make sure collision shape fits all the animation frames +- go to the AnimateSprit2D node +- Inspector->AnimatedSprite2D->Animation->Frame +- scroll through the frames to see if they fit the collision shape properly + +** modify the player script to handle the collision + +*** default collision code +- default collision handling occurs between two CharacterBody2D nodes +- limits each others movements +- occurs after the =move_and_slide= function is called + +#+begin_src godot +# Using move_and_collide. +var collision = move_and_collide(velocity * delta) +if collision: + print("I collided with ", collision.get_collider().name) +#+end_src + +*** handle collisions (scaffolding) +- first get the number of collisions that occurred + - this is in =get_slide_collision_count()= function +- loop through the number with an index + - in each loop get the /collision info object/ for that index with =get_slide_collisions()= + - =get_slide_collision= takes an index + - to get the /collider/ call =get_collider()= on the /collision info object/ + +- the function scaffolding: +#+begin_src godot +# this is the basic function +func handleCollision(): + for i in get_slide_collision_count(): + var collision = get_slide_collision(i) + var collider = collision.get_collider() + priint_debug(collider.name) +#+end_src + +- place this function in =func _physics_process(delta)= + - put it after =move_and_slide= but before =updateAnimation= + - example: +#+begin_src godot +func _physics_process(delta): + handleInput() + move_and_slide() + handleCollision() + updateAnimation() +#+end_src * Video Contents