Hello everyone,
I have a test level, with 3 objects in a group. I want to know if one of them passes between the other 2 when I apply an impulse to it. Since this is in 3D and I don't master linear algebra, I thought that a simple way to achieve this would be to create an Area between the 2 objects, give it a Collision shape and test if the moving objects collides with that area.
I create the Areas' CollisionShape (which is a ConvexPolygonShape made out of 4 points) through code, by getting the global_transform.origin of the objects I want it to be spawned between.
I am able to created the collision shape, but I'm not able to place the Area where it should be (from obj1 origin to obj2 origin with the proper size and rotation)
Bellow you can see the code that I came up with
extends Area
onready var test_objects= get_tree().get_nodes_in_group("TestObjects")
onready var collision_polygon = get_child(0).get_shape()
func generate_area_between_objects(object_to_be_moved):
var index = test_objects.find(object_to_be_moved)
if index == 0:
collision_polygon.set_points([Vector3(test_objects[1].global_transform.origin.x, test_objects[1].global_transform.origin.y + 5, test_objects[1].global_transform.origin.z),
Vector3(test_objects[2].global_transform.origin.x, test_objects[2].global_transform.origin.y + 5, test_objects[2].global_transform.origin.z),
Vector3(test_objects[2].global_transform.origin.x, test_objects[2].global_transform.origin.y - 5, test_objects[2].global_transform.origin.z),
Vector3(test_objects[1].global_transform.origin.x, test_objects[1].global_transform.origin.y - 5, test_objects[1].global_transform.origin.z)])
var mid = (test_objects[2].global_transform.origin + test_objects[1].global_transform.origin) / 2
var direction = test_objects[2].global_transform.origin.direction_to(test_objects[1].global_transform.origin)
global_transform.origin = direction
if index == 1:
collision_polygon.set_points([Vector3(test_objects[0].global_transform.origin.x, test_objects[0].global_transform.origin.y + 5, test_objects[0].global_transform.origin.z),
Vector3(test_objects[2].global_transform.origin.x, test_objects[2].global_transform.origin.y + 5, test_objects[2].global_transform.origin.z),
Vector3(test_objects[2].global_transform.origin.x, test_objects[2].global_transform.origin.y - 5, test_objects[2].global_transform.origin.z),
Vector3(test_objects[0].global_transform.origin.x, test_objects[0].global_transform.origin.y - 5, test_objects[0].global_transform.origin.z)])
var mid = (test_objects[2].global_transform.origin + test_objects[0].global_transform.origin) / 2
var direction = test_objects[2].global_transform.origin.direction_to(test_objects[0].global_transform.origin)
global_transform.origin = direction
if index == 2:
collision_polygon.set_points([Vector3(test_objects[0].global_transform.origin.x, test_objects[0].global_transform.origin.y + 5, test_objects[0].global_transform.origin.z),
Vector3(test_objects[1].global_transform.origin.x, test_objects[1].global_transform.origin.y + 5, test_objects[1].global_transform.origin.z),
Vector3(test_objects[1].global_transform.origin.x, test_objects[1].global_transform.origin.y - 5, test_objects[1].global_transform.origin.z),
Vector3(test_objects[0].global_transform.origin.x, test_objects[0].global_transform.origin.y - 5, test_objects[0].global_transform.origin.z)])
var mid = (test_objects[1].global_transform.origin + test_objects[0].global_transform.origin) /2
var direction = test_objects[1].global_transform.origin.direction_to(test_objects[0].global_transform.origin)
global_transform.origin = direction
As you can see, I'm setting the Areas' global_transform.origin to the direction, because strangely enough that gives me the result that is the closest to what I'm trying to achieve, however it's not actually on point.
Setting the origin to the mid point between the 2 objects places the Area very far from where it should be.
I appreciate any help related to how I can properly place this Area or any other advice on how you can detect an object passing between 2 others in 3D - I though about eliminating the Y component and doing it in X and Z only, but can't figure that out either 🙁