Search in sources :

Example 31 with CollisionResults

use of com.jme3.collision.CollisionResults in project TeachingInSimulation by ScOrPiOzzy.

the class VS10N051CControl method controlUpdate.

@Override
protected void controlUpdate(float tpf) {
    // 监听碰撞
    CollisionResults results = new CollisionResults();
    rootNode.collideWith(ray, results);
    temp = false;
    for (CollisionResult result : results) {
        if (result.getDistance() > limit) {
            continue;
        }
        Geometry geometry = result.getGeometry();
        if (((Node) spatial).hasChild(geometry)) {
            continue;
        }
        temp = true;
    }
    if (collised != temp) {
        collised = temp;
        if (collised) {
            rotateControl.rotate(JmeConst.NEGATIVE, FastMath.DEG_TO_RAD * 10);
            // collision.rotate(0, FastMath.DEG_TO_RAD * -10, 0);
            button.doSwitch(1);
            // 被碰撞按下之后禁止鼠标监听
            MouseEventState.setToMouseVisible(collision, false);
        } else {
            rotateControl.rotate(JmeConst.POSITIVE, FastMath.DEG_TO_RAD * 10);
            // collision.rotate(0, FastMath.DEG_TO_RAD * 10, 0);
            button.doSwitch(0);
            // 离开碰撞松开之后启用鼠标监听
            MouseEventState.setToMouseVisible(collision, true);
        }
    }
}
Also used : CollisionResult(com.jme3.collision.CollisionResult) Geometry(com.jme3.scene.Geometry) CollisionResults(com.jme3.collision.CollisionResults) Node(com.jme3.scene.Node)

Example 32 with CollisionResults

use of com.jme3.collision.CollisionResults in project TeachingInSimulation by ScOrPiOzzy.

the class SwingControlEx method controlUpdate.

@Override
protected void controlUpdate(float tpf) {
    Vector3f origin = start1.getWorldTranslation();
    Vector3f goal = end1.getWorldTranslation();
    float limit = FastMath.abs(goal.distance(origin));
    Ray ray = new Ray(origin, goal.add(origin.negate()));
    CollisionResults results = new CollisionResults();
    rootNode.collideWith(ray, results);
    for (CollisionResult result : results) {
        if (result.getDistance() > limit) {
            continue;
        }
        Geometry geometry = result.getGeometry();
        if ("material".equals(geometry.getUserData("type"))) {
            Spatial material = geometry.getParent().getParent();
            if (Util.notEmpty(material.getUserData("owner"))) {
                break;
            }
            if (material.getNumControls() != 0) {
                material.setLocalTranslation(Vector3f.ZERO);
                control1 = (ItemControl) material.getControl(0);
                control1.setEnabled(false);
                pieceLoc1.attachChild(material);
                stateManager.getState(MouseEventState.class).setVipModel(material);
            }
        }
    }
    origin = start2.getWorldTranslation();
    goal = end2.getWorldTranslation();
    limit = FastMath.abs(goal.distance(origin));
    Ray ray2 = new Ray(origin, goal.add(origin.negate()));
    CollisionResults results2 = new CollisionResults();
    rootNode.collideWith(ray2, results2);
    for (CollisionResult result : results2) {
        if (result.getDistance() > limit) {
            continue;
        }
        Geometry geometry = result.getGeometry();
        if ("material".equals(geometry.getUserData("type"))) {
            Spatial material = geometry.getParent().getParent();
            if (Util.notEmpty(material.getUserData("owner"))) {
                break;
            }
            if (material.getNumControls() != 0) {
                material.setLocalTranslation(Vector3f.ZERO);
                control2 = (ItemControl) material.getControl(0);
                control2.setEnabled(false);
                pieceLoc2.attachChild(material);
                stateManager.getState(MouseEventState.class).setVipModel(material);
            }
        }
    }
    if (!swing) {
        return;
    }
    float rad = tpf * speed;
    Vector3f per = dir.mult(rad);
    if (b) {
        total += rad;
        for (final Spatial spatial : driveds) {
            spatial.rotate(per.x, per.y, per.z);
        }
        if (total >= degree) {
            for (final Spatial spatial : driveds) {
                spatial.rotate(0, degree - total, 0);
            }
            total = degree;
            swing = false;
            // 此处用于通知检测被当前托盘中工件阻塞的工件开始移动,并不真正设置模型
            if (control1 != null) {
                control1.onMoving();
            }
            if (control2 != null) {
                control2.onMoving();
            }
        }
    } else {
        total -= rad;
        for (final Spatial spatial : driveds) {
            spatial.rotate(-per.x, -per.y, -per.z);
        }
        if (total <= 0) {
            for (final Spatial spatial : driveds) {
                spatial.rotate(0, -total, 0);
            }
            total = 0;
            swing = false;
            // 此处用于通知检测被当前托盘中工件阻塞的工件开始移动,并不真正设置模型
            if (control1 != null) {
                control1.onMoving();
            }
            if (control2 != null) {
                control2.onMoving();
            }
        }
    }
}
Also used : CollisionResult(com.jme3.collision.CollisionResult) Geometry(com.jme3.scene.Geometry) CollisionResults(com.jme3.collision.CollisionResults) Spatial(com.jme3.scene.Spatial) Vector3f(com.jme3.math.Vector3f) Ray(com.jme3.math.Ray) MouseEventState(com.cas.robot.common.MouseEventState)

Aggregations

CollisionResult (com.jme3.collision.CollisionResult)22 CollisionResults (com.jme3.collision.CollisionResults)21 Ray (com.jme3.math.Ray)15 Vector3f (com.jme3.math.Vector3f)15 TempVars (com.jme3.util.TempVars)8 Spatial (com.jme3.scene.Spatial)6 Geometry (com.jme3.scene.Geometry)5 Vector2f (com.jme3.math.Vector2f)4 BoundingBox (com.jme3.bounding.BoundingBox)3 UnsupportedCollisionException (com.jme3.collision.UnsupportedCollisionException)3 BoundingVolume (com.jme3.bounding.BoundingVolume)2 Triangle (com.jme3.math.Triangle)2 Node (com.jme3.scene.Node)2 BufferedImage (java.awt.image.BufferedImage)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Point2d (javax.vecmath.Point2d)2 MouseEventState (com.cas.robot.common.MouseEventState)1 BoundingSphere (com.jme3.bounding.BoundingSphere)1 ColorRGBA (com.jme3.math.ColorRGBA)1