Skip to content

Navigation Menu

Sign in
Sign up

How do I get bodies that are not intersected? #186

Answered by ChrisAcrobat
ChrisAcrobat asked this question in Q&A
Discussion options

I spawn a random amount of Bodys at a single point with collisionResponse=false. This is what I want, but what I also want is to enable collisionResponse when each object has left the group after .fixedStep(n).
Is there an in-engine or native way of solving this? I have tried using collisionMatrix and event handler, but have not got it to work yet,

You must be logged in to vote

Solved it by: (semi-pseudo, minimized for proof-of-concept)

const spheres = [];
let nextID = 1;
const target = Math.random()*100;
for(let i= 0; i < target; i++) {
	const collisionFilterGroup = nextID;
	const collisionFilterMask = 0;
	spheres.push(new Sphere({collisionFilterGroup, collisionFilterMask}));
	nextID *= 2;
}
while(true){
	world.fixedStep(1);
	spheres.forEach(s1 => {
		spheres.forEach(s2 => {
			const touchPoint = s1.shapes[0].radius + s2.shapes[0].radius;
			if(touchPoint < s1.position.distanceTo(s2.position)){
				s1.collisionFilterMask |= s2.collisionFilterGroup;
			}
		});
	});
}

If someone have a better solution for s1.shapes[0].radius + s2.shapes[0].radius, then I'm all ears.

Replies: 1 comment

Comment options

Solved it by: (semi-pseudo, minimized for proof-of-concept)

const spheres = [];
let nextID = 1;
const target = Math.random()*100;
for(let i= 0; i < target; i++) {
	const collisionFilterGroup = nextID;
	const collisionFilterMask = 0;
	spheres.push(new Sphere({collisionFilterGroup, collisionFilterMask}));
	nextID *= 2;
}
while(true){
	world.fixedStep(1);
	spheres.forEach(s1 => {
		spheres.forEach(s2 => {
			const touchPoint = s1.shapes[0].radius + s2.shapes[0].radius;
			if(touchPoint < s1.position.distanceTo(s2.position)){
				s1.collisionFilterMask |= s2.collisionFilterGroup;
			}
		});
	});
}

If someone have a better solution for s1.shapes[0].radius + s2.shapes[0].radius, then I'm all ears.

You must be logged in to vote
0 replies
Answer selected by ChrisAcrobat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /