set_pos overrides collision(Both shape and polygon)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lavaduder
:warning: Old Version Published before Godot 3 was released.

I didn’t like the floatiness of velocity controls, so I made a control system with set_pos(). And it controls much better. However I have ran into a issue. the set_pos() function overrides collision. So I can move my character smoothly, smoothly through walls. How do make the set_pos() not override the collision of my scene.


#Get position
var charx= get_pos().x setget setcharx
var chary= get_pos().y setget setchary

func _ready():
	set_fixed_process(true)
	pass

func _fixed_process(delta):
	#control variables
	var moveu = Input.is_action_pressed("ui_up")
	var moved = Input.is_action_pressed("ui_down")
	var mover = Input.is_action_pressed("ui_right")
	var movel = Input.is_action_pressed("ui_left")
	var controlenabled = true
	
	#input
	if controlenabled == true:
		if (moveu):
			setchary(-5)
			pass
		if (moved):
			setchary(5)
			pass
		if (mover):
			setcharx(5)
			pass
		if (movel):
			setcharx(-5)
			pass
		pass
	
	set_pos(Vector2(charx,chary))
	pass

func setcharx(value):
	charx = value + get_pos().x
	pass

func setchary(value):
	chary = value + get_pos().y
	pass
:bust_in_silhouette: Reply From: avencherus

You can’t.

It’s one or the other. If you’re using KinematicBody you should be able to get the same effect as set_pos() with move_to().

Thank you! However I now have a different problem, It sticks to objects if it’s rubbing up against them. Any way to fix that?

lavaduder | 2017-04-03 04:25

Hard to say. Kinematics aren’t used for their collision responsiveness. Often that’s why they’re preferred, so they will ignore certain impacts. IE - Moving platforms.

If you’re writing your own physics code, you have to figure out what all these situations are, study the outputs, and fine tune solutions to your design. There are all kinds of techniques, such as casting rays, using special collision shapes, etc. There is a lot of study, and various pros and cons, so I’m afraid there is no easy answer that will be the magic bullet there.

I’m fairly sure the standard thing is to cast a ray, and detect if it’s a wall collision. Then ignore whatever movement is being sent, and maybe even perform a vector slide along the collision normal. Such as this: http://docs.godotengine.org/en/stable/tutorials/2d/kinematic_character_2d.html#problem

I could also suggest some kinematic character tutorial videos @ https://www.youtube.com/playlist?list=PLFt_AvWsXl0f0hqURlhyIoAabKPgRsqjz

It’s for Unity, but it may give you some ideas about the techniques and considerations you need to tackle when writing kinematic characters.

I hope that helps.

avencherus | 2017-04-03 11:31