MX0C

  • 18 Jul
  • Joined 7 Jul
  • 0 best answers

    Tomcat Well yes, I want to implement it in a 3D project but I wanted to do the first tests in a 2D project, but if you think it is necessary to start from now on in 3D I would change it now

      Sorry for the delay, I had things to do, but I was doing a little research on the move_and_collide and test_move methods, but not enough so I decided to postpone these methods and make sure it works first with areas to have the principles of the behavior ready.
      However, I have encountered several inconveniences, such as, for example, handling vector 2 in a mathematical operation that requires multiplication/division, if any of its values ​​turns out to be a perfect 0, you will imagine that it does a strange behavior,
      The second one that I fixed for the moment but I was a little dissatisfied is that instead of gaining more strength as I get closer to the object, it is the other way around that I leave you the modified code based on the basic_movement project from the documentation tutorial.
      https://docs.godotengine.org/en/stable/tutorials/physics/using_character_body_2d.html

      extends CharacterBody2D

      var speed = 300
      var last_vel : Vector2
      
      func get_input():
      	var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
      	velocity = input_dir * speed
      	return input_dir * speed
      
      func _physics_process(_delta):
      	#move_and_collide(velocity * delta)
      	move_and_slide()
      	get_input()
      func reject_body(BODY_POS:Vector2):
      	var vector_cero = Vector2(0,0)
      	if velocity != vector_cero:
      		last_vel = velocity
      	var dir : Vector2 = (BODY_POS - global_position)
      	var dis = dir * dir.normalized()
      	
      	velocity =  get_input() - dir.normalized() * (Vector2(100,100)/dis) 
      	print((Vector2(100,100)/dis) )

      Remember to also add an area with this code

      extends Area2D
      
      
      # Called when the node enters the scene tree for the first time.
      func _ready():
      	pass # Replace with function body.
      
      
      # Called every frame. 'delta' is the elapsed time since the previous frame.
      func _process(_delta):
      	for bodies in get_overlapping_bodies():
      		if bodies.has_method("reject_body"):
      			bodies.reject_body(global_position)
        • Edited

        DaveTheCoder I could try it, but I don't know if that method would be practical for performance

        We know how collisions behave (in a basic way) in godot, when the collision figures approach more than the defined margin they will brake suddenly and, in case of transfer due to a calculation error, the bodies will be abruptly displaced outside the body However, I thought that for my project maybe it would be better if (speaking of the npc entities and players), instead of blocking each other so rigidly there would be a way for the entities to move smoothly as if a force field It would be treated, with a varying amount of force depending on the proximity to the center of mass of the objects to give the sense of an obstacle but at the same time offer the opportunity to have greater contact between said elements.
        I repeat, if it were possible, I would prefer this to happen exclusively between NPC entities and players because it seems to me that to calculate the scenario, the basic behavior of collisions would be essential.
        Does anyone have any suggestions or comments to better understand what this dilemma would represent?