Side detection for cube

Started by xDonny, November 20, 2013, 06:11:56 PM

Previous topic - Next topic

xDonny

I've been working for 2 days to figure out which side of a box i'm hitting when using picking. I get a SimpleVector from the picking and I have tried numerous things to figure it out but all to no avail.

I have tried:

a) adding the SimpleVector and the center of the box together, and finding which side it's closest too, unfortunately it's always closest to the character, even if the top is clicked it will put it on the side that's facing the camera.
b) pulling out my hair
c) crying

I need a fresh mind to help me with this

Thanks


EDIT:

This is my picking code:

SimpleVector dir=Interact2D.reproject2D3DWS(g.camera, g.fb, (int) me.getX(), (int) me.getY()).normalize();
Object[] res=g.world.calcMinDistanceAndObject3D(g.camera.getPosition(), dir, 18 /*or whatever*/);

Object3D result = (Object3D) res[res.length-1];


some pseudo code of what I have tried:



dir.scalarMul(-1f);
dir.add(result.getTransformedCenter());

Object3D temp = result.cloneObject();

temp.translate(0,-2,0); // up
float dist1 = dir.distance(temp.getTransformedCenter());





I use 6 distances (one for each side) and it still doesn't work!


Again, thanks

aZen

#1
Here you go:


        SimpleVector dir = this.getDirection(point.x, point.y);
        Object[] res = world.calcMinDistanceAndObject3D(camera.getPosition(), dir, 10000);
        if (res[1] != null) { // something hit
            Object3D obj3D = ((Object3D)res[1]);
            Voxel hitVoxel = data.getVoxel(world.getVoxelId(obj3D.getID()));
            if (hitVoxel != null) {
                voxelPos = hitVoxel.getPosAsInt();
                // find collision point
                SimpleVector colPoint = camera.getPosition();
                dir.scalarMul((Float)res[0]);
                colPoint.add(dir);
                colPoint.sub(obj3D.getOrigin());
                // find side that it hits
                int lastActiveSide = 0; // assume it's zero (no need to check)
                float dist = colPoint.distance(directionVectors[0]);
                for (int i = 1; i < directionVectors.length; i++) {
                    float tempDist = colPoint.distance(directionVectors[i]);
                    if (dist > tempDist) {
                        dist = tempDist;
                        lastActiveSide = i;
                    }
                }
            }
        }


For the direction I use Interact2D.reproject2D3DWS

Please let me know if you have any questions.

Just curious, what are you up to?

xDonny

Quote from: aZen on November 21, 2013, 12:38:30 AM
Here you go:


        SimpleVector dir = this.getDirection(point.x, point.y);
        Object[] res = world.calcMinDistanceAndObject3D(camera.getPosition(), dir, 10000);
        if (res[1] != null) { // something hit
            Object3D obj3D = ((Object3D)res[1]);
            Voxel hitVoxel = data.getVoxel(world.getVoxelId(obj3D.getID()));
            if (hitVoxel != null) {
                voxelPos = hitVoxel.getPosAsInt();
                // find collision point
                SimpleVector colPoint = camera.getPosition();
                dir.scalarMul((Float)res[0]);
                colPoint.add(dir);
                colPoint.sub(obj3D.getOrigin());
                // find side that it hits
                int lastActiveSide = 0; // assume it's zero (no need to check)
                float dist = colPoint.distance(directionVectors[0]);
                for (int i = 1; i < directionVectors.length; i++) {
                    float tempDist = colPoint.distance(directionVectors[i]);
                    if (dist > tempDist) {
                        dist = tempDist;
                        lastActiveSide = i;
                    }
                }
            }
        }


For the direction I use Interact2D.reproject2D3DWS

Please let me know if you have any questions.

Just curious, what are you up to?


Thank you - I figured this out by generating 6 cubes around it and detecting if those are collided.

I'm making a game called CubeCraft, yes, it's exactly what you think it is ;)


Thanks,
Donny

aZen

Cool! Do you have anything to show yet?

The code I posted is much more efficient than generating more objects and testing collision. I'm actually not sure how your approach works...?

I've written a voxel editor with jpct, so if you have any questions feel free to ask! I should know most quirks =)

xDonny

I do! I'll upload an .apk when I'm at home.

I'm just having issues with collisions, it works for ground and standing on cubes. But side collision with cubes is non existent (you can walk through them and be elevated up.)

Perhaps you have a solution for that as well? I'm currently using the camera as my player, So i'm using

World.checkCameraCollisionEllipsoid();

Thanks,
Donny

aZen

Considering that you're using voxel and you probably want to be efficient, I'd just generate a walking "grid"  for each hight layer of voxel. You can generate it on the fly and it should be extremely fast and reliable. Let me know if you need further clarification! Btw You should use a ray test to prevent clipping through corners.

EgonOlsen

If ellipsoid collisions don't work for you, this might be caused by your polygons being to large. Try to adjust http://www.jpct.net/jpct-ae/doc/com/threed/jpct/Config.html#collideOffset and see if that helps.