adding notes on body-body collision

This commit is contained in:
ronny abraham 2024-09-11 18:22:40 +03:00
parent e462e2df4b
commit cb1348851a

View file

@ -3,7 +3,10 @@
#+title: Lesson 12: Player-Enemy Collisions #+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 * Notes
@ -16,14 +19,61 @@
- select 'CapsuleShape2D' from dropdown - select 'CapsuleShape2D' from dropdown
*** modify shape *** modify shape
- Go to Inspector->CollisionShape2D->Node2D - Inspector->CollisionShape2D->Node2D->Transform
- Open 'Transform'
- Go to Rotation and set to 90 degrees - Go to Rotation and set to 90 degrees
- Modify shape via handles to adjust it over sprite - Modify shape via handles to adjust it over sprite
*** z-index *** set the z-index
- set :w - 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 * Video Contents