I don’t understand the question to have a rectangle instead of a square.
Hi R0J0hound, thanks for your reply and contribution to the community of course!
I managed to break all ties with the layout size, but I can't realize how to do the same spatial grid but not for circles or squares, but for rectangle sprites instead, which can change their angle during the gameplay? Now my rectangle sprites have like an "invisible field" on its longer sides that pushing out other similar sprites even before collision, but I'd like to keep it within the sprite rectangle. Is my explanation clear enough?
const SIZE = 144, radius = 144;
let grid = [];
let objs = runtime.objects.sprite.getAllInstances();
function calculateDistance(obj1, obj2) {
return Math.sqrt((obj2.x - obj1.x) ** 2 + (obj2.y - obj1.y) ** 2);
}
function applyForce(obj1, obj2, dist) {
let separation = Math.max(0, radius - dist);
if (separation > 0) {
let ang = Math.atan2(obj2.y - obj1.y, obj2.x - obj1.x);
let force = separation / 12.5;
obj1.offsetPosition(-force * Math.cos(ang), -force * Math.sin(ang));
obj2.offsetPosition(force * Math.cos(ang), force * Math.sin(ang));
}
}
globalThis.spatialGrid = function() {
objs = runtime.objects.sprite.getAllInstances();
for (let i = 0; i < objs.length; i++) {
for (let j = i + 1; j < objs.length; j++) {
let obj1 = objs[i];
let obj2 = objs[j];
let dist = calculateDistance(obj1, obj2);
applyForce(obj1, obj2, dist);
}
}
};