theone
November 21, 2024, 1:33pm
1
Godot Version
4.3
Question
I have a truck in 2d environment. When the truck is achieved the end of the map i need to flip it. I have not symmetric sprite and additional things so i cant flip sprite. I need to flip all truck.
Hierarchy of the truck:
Truck(RigidBody2D)
Sprite2D
CollisionPolygon2D
FirstWheelHolder(PinJoint2D)
Wheel(RigidBody2D)
SecondWheelHolder(PinJoint2D)
Wheel(RigidBody2D)
FirstLoadHolder(PinJoint2D)
RigidBody2D
SecondLoadHolder(PinJoint2D)
RigidBody2D
ThirdLoadHolder(PinJoint2D)
RigidBody2D
Regards
theone
November 21, 2024, 4:36pm
2
This solution does not answer the question but partially solves the problem
I do not flip the truck but I move children
If i do something wrong please correct me
extends RigidBody2D
class_name Truck
@onready var sprite: Sprite2D = $Sprite2D
@onready var cp: CollisionPolygon2D = $CollisionPolygon2D
@onready var fwh: PinJoint2D = $FirstWheelHolder
@onready var swh: PinJoint2D = $SecondWheelHolder
@onready var flh: PinJoint2D = $FirstLoadHolder
@onready var slh: PinJoint2D = $SecondLoadHolder
@onready var tlh: PinJoint2D = $ThirdLoadHolder
func _set_bodies_mode(mode) -> void:
PhysicsServer2D.body_set_mode(fwh.get_node("Wheel").get_rid(), mode)
PhysicsServer2D.body_set_mode(swh.get_node("Wheel").get_rid(), mode)
PhysicsServer2D.body_set_mode(flh.get_node("Wheel").get_rid(), mode)
PhysicsServer2D.body_set_mode(slh.get_node("Wheel").get_rid(), mode)
PhysicsServer2D.body_set_mode(tlh.get_node("Wheel").get_rid(), mode)
PhysicsServer2D.body_set_mode(get_rid(), mode)
func _set_body_state(rid: RID, pos: Vector2) -> void:
PhysicsServer2D.body_set_state(
rid,
PhysicsServer2D.BODY_STATE_TRANSFORM,
Transform2D.IDENTITY.translated(pos)
)
func move_pin_joint_with_rigid_body(pj: PinJoint2D, pos: Vector2) -> void:
pj.node_b = ""
pj.position = pos
for child in pj.get_children():
if child is RigidBody2D:
_set_body_state(child.get_rid(), pj.global_position)
child.position = Vector2.ZERO
pj.node_b = child.get_path()
func flip() -> void:
# stop physics
_set_bodies_mode(PhysicsServer2D.BODY_MODE_KINEMATIC)
# move pinjoints with child
move_pin_joint_with_rigid_body(fwh, Vector2)
move_pin_joint_with_rigid_body(swh, Vector2)
move_pin_joint_with_rigid_body(flh, Vector2)
move_pin_joint_with_rigid_body(slh, Vector2)
move_pin_joint_with_rigid_body(tlh, Vector2)
# flip sprite and collision polygon
sprite.flip_h = last_direction == DIRECTION.RIGHT
cp.scale.x *= -1
# start physics again
_set_bodies_mode(PhysicsServer2D.BODY_MODE_RIGID)