Checking for collisions before adding an object

I’ve been through the tutorials and other info and searched the forums but am still stuck. In my game I’ve managed to get objects to be placed in the world when the user right clicks, but the object can then push the player away if it is placed so it overlaps the player. I want to check if adding the object would cause a collision (eventually with the player or other mobile creatures) and, if so, prevent it being added.

I’ve played around with collisions but I think I’m going down the wrong route. Can someone give me a quick overview of the approach I should take here (links to examples would be great) - I’m happy to play around to get it working but I want to make sure I’m not spending ages trying to do it the wrong way. I’m using JBullet to handle the physics which is working fine.


use physicsSpace.sweepTest with the objects collision shape.

Great - thanks for the pointer, I’ll go play. :slight_smile:

Sorry, had a look and I can’t work out why it takes two transforms…

sweepTest(CollisionShape shape, Transform start, Transform end)

Performs a sweep collision test and returns the results as a list of PhysicsSweepTestResults

You have to use different Transforms for start and end (at least distance > 0.4f).

Should I set that to opposite corners of the shape (it’s a box)?

no, just sweep a bit or do two tests from either side if you want to be 100% exact, with an offset of min. 0.04f (I don’t know where you got that 0.4f from and the 0.04f limit is a bug where idk if it really exists in jbullet too).

I copied from the JavaDoc for Class PhysicsSpace, - looks like it needs to be updated.

Sorry to be a pain but I’m not really understanding what the sweep test actually does, which is why I can’t see what values to put in. I’ve been searching JME and JBullet but can’t find any examples using it. Unless…

Ah…I’m only providing a collisionshape so the sweeptest doesn’t actually know where the object is does it? So I need to tell it the points to sweep between (the two transforms)?

Does this mean that I don’t actually need to have added the shape to RootNode (via a child node) to do the test?

Or am I barking up the wrong tree?

it sweeps the collision shape through the physics space and reports any collisions w/o actually doing anything in the space, like physicsSpace.rayTest uses a ray. note it doesn’t work when going from inside an object to outside of it.

So…if I need to test the space a cube will occupy I can create a collisionshape that’s a square of the same shape, but with very small depth, then move that from one side of where cube will be to the other?

Trying to move the whole cube, as I have been, would mean it would collide with items outside of the cubes space.

Doing some more reading I think that using Ghost might be a better solution - I could create the box, without adding it to the bullet physics engine, then use the ghost shape to tell if it overlaps with the player. That way I can tell if anything overlaps any of the intended location. I’ll try that out and post back here if it works out or not.


Thanks again for all the help and the quick response. I spent some time thinking this through (OK, and drinking a couple of beers) and decided it was easier to take the player and convert it’s location back into the underlying data model for the game then do direct compares there, which is working well.

That said, I’m sure I’ll be needing some of the above for other areas in the game. I’ve found some JBulllet information covering sweeps so will try to get a better understanding.