Search in sources :

Example 1 with ClosestRay

use of spacegraph.space3d.phys.collision.ClosestRay in project narchy by automenta.

the class OrbMouse method mouseMotionFunc.

// public ClosestRay mousePick(int sx, int sy) {
// return mousePick(space.rayTo(sx, sy));
// }
// 
// public ClosestRay mousePick(v3 rayTo) {
// ClosestRay r = this.rayCallback;
// 
// //v3 camPos = v(rayTo.x, rayTo.y, space.camPos.z); //project directly upward
// 
// v3 camPos = space.camPos;
// 
// return r;
// }
@Deprecated
private /* TODO probably rewrite */
boolean mouseMotionFunc(int px, int py, short[] buttons) {
    ClosestRay cray = mousePick(px, py);
    /*System.out.println(mouseTouch.collisionObject + " touched with " +
            Arrays.toString(buttons) + " at " + mouseTouch.hitPointWorld
        );*/
    Spatial prevPick = pickedSpatial;
    Spatial pickedSpatial = null;
    picked = cray != null ? cray.collidable : null;
    if (picked != null) {
        Object t = picked.data();
        if (t instanceof Spatial) {
            pickedSpatial = ((Spatial) t);
            if (pickedSpatial.onTouch(finger, picked, cray, buttons, space) != null) {
                // absorbed surface
                clearDrag();
            } else {
            // maybe find next closest?
            }
        }
    }
    if ((pickConstraint != null)) /*|| (directDrag != null)*/
    {
    // // keep it at the same picking distance
    // v3 eyePos = v(space.camPos);
    // v3 dir = v();
    // dir.sub(cray.rayFromWorld, eyePos);
    // dir.normalize();
    // dir.scale(gOldPickingDist);
    // 
    // v3 newPos = v();
    // newPos.add(eyePos, dir);
    // if (directDrag != null) {
    // //directly move the 'static' object
    // 
    // Object u = directDrag.data();
    // 
    // //System.out.println("DRAG: " + directDrag + " " + u + " -> " + newPos);
    // 
    // if (u instanceof SimpleSpatial) {
    // ((SimpleSpatial) u).motionLock(true);
    // }
    // 
    // //                MotionState mm = directDrag.getMotionState();
    // //                if (mm instanceof Motion) {
    // //                    ((Motion) mm).center(newPos);
    // //                }
    // directDrag.worldTransform.set(newPos);
    // 
    // return true;
    // } else
    // v3 newRayTo = new v3(mousePick(px, py).rayToWorld); //HACK dont involve a complete ClosestRay
    // v3 eyePos = new v3(space.camPos);
    // v3 dir = new v3();
    // dir.sub(newRayTo, eyePos);
    // dir.normalize();
    // dir.scale(gOldPickingDist);
    // 
    // v3 newPos = new v3();
    // newPos.add(eyePos, dir);
    // 
    // // move the constraint pivot
    // Point2PointConstraint p2p = (Point2PointConstraint) pickConstraint;
    // p2p.setPivotB(newPos);
    // return true;
    } else {
    // if (mouseDragPrevX >= 0) {
    // 
    // 
    // ///only if ALT key is pressed (Maya style)
    // //            if (m_modifierKeys & BT_ACTIVE_ALT)
    // //            {
    // //                if (m_mouseButtons & 4) {
    // //                    btVector3 hor = getRayTo(0, 0) - getRayTo(1, 0);
    // //                    btVector3 vert = getRayTo(0, 0) - getRayTo(0, 1);
    // //                    btScalar multiplierX = btScalar(0.001);
    // //                    btScalar multiplierY = btScalar(0.001);
    // //                    if (m_ortho) {
    // //                        multiplierX = 1;
    // //                        multiplierY = 1;
    // //                    }
    // //
    // //
    // //                    m_cameraTargetPosition += hor * dx * multiplierX;
    // //                    m_cameraTargetPosition += vert * dy * multiplierY;
    // //                }
    // //            }
    // 
    // for (short b : buttons) {
    // switch (b) {
    // case 1:
    // //                                ClosestRay m = mousePick(x, y, null);
    // //                                if (!m.hasHit()) {
    // //                                    //drag on background space
    // //                                    float px = mouseDragDX * 0.1f;
    // //                                    float py = mouseDragDY * 0.1f;
    // //
    // //                                    //TODO finish:
    // //
    // //                                    //v3 vx = v().cross(camDir, camUp);
    // //                                    //vx.normalize();
    // //                                    //System.out.println(px + " " + py + " " + camDir + " x " + camUp + " " + vx);
    // //
    // //                                    //camPosTarget.scaleAdd(px, vx);
    // //                                    //camPosTarget.scaleAdd(py, camUp);
    // //                                }
    // return true;
    // case 3:
    // //right mouse drag = rotate
    // //                        nextAzi += dx * btScalar(0.2);
    // //                        nextAzi = fmodf(nextAzi, btScalar(360.f));
    // //                        nextEle += dy * btScalar(0.2);
    // //                        nextEle = fmodf(nextEle, btScalar(180.f));
    // //space.azi.setValue(space.azi.floatValue() + mouseDragDX * 0.2f );
    // //nextAzi = fmodf(nextAzi, btScalar(360.f));
    // //space.ele.setValue(space.ele.floatValue() + mouseDragDY * (0.2f));
    // //nextEle = fmodf(nextEle, btScalar(180.f));
    // return true;
    // case 2:
    // //middle mouse drag = zoom
    // 
    // //space.setCameraDistance( space.cameraDistance.floatValue() - mouseDragDY * 0.5f );
    // return true;
    // }
    // }
    // }
    // 
    }
    if (prevPick != pickedSpatial) {
        if (prevPick != null) {
            prevPick.onUntouch(space);
        }
        this.pickedSpatial = pickedSpatial;
    }
    return false;
}
Also used : Spatial(spacegraph.space3d.Spatial) ClosestRay(spacegraph.space3d.phys.collision.ClosestRay)

Example 2 with ClosestRay

use of spacegraph.space3d.phys.collision.ClosestRay in project narchy by automenta.

the class OrbMouse method mousePick.

@Nullable
ClosestRay mousePick(int x, int y) {
    // float top = 1f;
    // float bottom = -1f;
    // float nearPlane = 1f;
    float tanFov = (space.top - space.bottom) * 0.5f / space.zNear;
    float fov = 2f * (float) Math.atan(tanFov);
    v3 rayFrom = new v3(space.camPos);
    v3 rayForward = new v3(space.camFwd);
    rayForward.scale(space.zFar);
    // v3 rightOffset = new v3();
    v3 vertical = new v3(space.camUp);
    v3 hor = new v3();
    // TODO: check: hor = rayForward.cross(vertical);
    hor.cross(rayForward, vertical);
    hor.normalize();
    // TODO: check: vertical = hor.cross(rayForward);
    vertical.cross(hor, rayForward);
    vertical.normalize();
    float tanfov = (float) Math.tan(0.5f * fov);
    float ww = space.getWidth();
    float hh = space.getHeight();
    float aspect = hh / ww;
    hor.scale(2f * space.zFar * tanfov);
    vertical.scale(2f * space.zFar * tanfov);
    if (aspect < 1f) {
        hor.scale(1f / aspect);
    } else {
        vertical.scale(aspect);
    }
    v3 rayToCenter = new v3();
    rayToCenter.add(rayFrom, rayForward);
    v3 dHor = new v3(hor);
    dHor.scale(1f / ww);
    v3 dVert = new v3(vertical);
    dVert.scale(1f / hh);
    v3 tmp1 = new v3();
    v3 tmp2 = new v3();
    tmp1.scale(0.5f, hor);
    tmp2.scale(0.5f, vertical);
    v3 rayTo = new v3();
    rayTo.sub(rayToCenter, tmp1);
    rayTo.add(tmp2);
    tmp1.scale(x, dHor);
    tmp2.scale(y, dVert);
    rayTo.add(tmp1);
    rayTo.sub(tmp2);
    ClosestRay r = new ClosestRay(space.camPos, rayTo);
    space.dyn.rayTest(space.camPos, rayTo, r, simplexSolver);
    if (rayCallback.hasHit()) {
        Body3D body = Body3D.ifDynamic(rayCallback.collidable);
        if (body != null && (!(body.isStaticObject() || body.isKinematicObject()))) {
            pickedBody = body;
            hitPoint = r.hitPointWorld;
        }
    }
    return r;
}
Also used : Body3D(spacegraph.space3d.phys.Body3D) ClosestRay(spacegraph.space3d.phys.collision.ClosestRay) spacegraph.util.math.v3(spacegraph.util.math.v3) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with ClosestRay

use of spacegraph.space3d.phys.collision.ClosestRay in project narchy by automenta.

the class OrbMouse method mouseClick.

private boolean mouseClick(int button, int x, int y) {
    switch(button) {
        case MouseEvent.BUTTON3:
            ClosestRay c = mousePick(x, y);
            if (c.hasHit()) {
                Collidable co = c.collidable;
                // System.out.println("zooming to " + co);
                // TODO compute new azi and ele that match the current viewing angle values by backcomputing the vector delta
                space.camera(co.transform, co.shape().getBoundingRadius() * 2.5f);
                return true;
            }
            break;
    }
    return false;
}
Also used : Collidable(spacegraph.space3d.phys.Collidable) ClosestRay(spacegraph.space3d.phys.collision.ClosestRay)

Aggregations

ClosestRay (spacegraph.space3d.phys.collision.ClosestRay)3 Nullable (org.jetbrains.annotations.Nullable)1 Spatial (spacegraph.space3d.Spatial)1 Body3D (spacegraph.space3d.phys.Body3D)1 Collidable (spacegraph.space3d.phys.Collidable)1 spacegraph.util.math.v3 (spacegraph.util.math.v3)1