Author Topic: Side detection for cube  (Read 2894 times)

Offline xDonny

  • byte
  • *
  • Posts: 16
    • View Profile
Side detection for cube
« on: November 20, 2013, 06:11:56 pm »
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:

Code: [Select]
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:

Code: [Select]

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
« Last Edit: November 20, 2013, 06:15:04 pm by xDonny »

Offline aZen

  • int
  • **
  • Posts: 94
    • View Profile
Re: Side detection for cube
« Reply #1 on: November 21, 2013, 12:38:30 am »
Here you go:

Code: [Select]
        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?
« Last Edit: November 21, 2013, 12:43:44 am by aZen »

Offline xDonny

  • byte
  • *
  • Posts: 16
    • View Profile
Re: Side detection for cube
« Reply #2 on: November 21, 2013, 03:15:12 am »
Here you go:

Code: [Select]
        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

Offline aZen

  • int
  • **
  • Posts: 94
    • View Profile
Re: Side detection for cube
« Reply #3 on: November 21, 2013, 03:43:35 am »
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 =)

Offline xDonny

  • byte
  • *
  • Posts: 16
    • View Profile
Re: Side detection for cube
« Reply #4 on: November 21, 2013, 02:17:49 pm »
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

Offline aZen

  • int
  • **
  • Posts: 94
    • View Profile
Re: Side detection for cube
« Reply #5 on: November 22, 2013, 12:25:59 am »
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12297
    • View Profile
    • http://www.jpct.net
Re: Side detection for cube
« Reply #6 on: November 22, 2013, 07:55:19 am »
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.