Package org.terasology.physics

Source Code of org.terasology.physics.HitResult

/*
* Copyright 2013 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.terasology.physics;

import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.math.Vector3i;

import javax.vecmath.Vector3f;

/**
* A HitResult holds the result of a ray-trace.
*
* @author Immortius
*/
public class HitResult {
    private boolean hit;
    private EntityRef entity;
    private Vector3f hitPoint;
    private Vector3f hitNormal;
    private Vector3i blockPosition;
    private final boolean worldHit;

    public HitResult() {
        hit = false;
        entity = EntityRef.NULL;
        worldHit = false;
    }

    /**
     * Creates a HitResult for hitting an entity.
     *
     * @param entity
     * @param hitPoint
     * @param hitNormal
     */
    public HitResult(EntityRef entity, Vector3f hitPoint, Vector3f hitNormal) {
        this.hit = true;
        this.entity = entity;
        this.hitPoint = hitPoint;
        this.hitNormal = hitNormal;
        //This is the block were the hitPoint is inside:
        this.blockPosition = new Vector3i(hitPoint, 0.5f);
        this.worldHit = false;
    }

    /**
     * Creates a HitResult for hitting a block from the world.
     *
     * @param entity
     * @param hitPoint
     * @param hitNormal
     * @param blockPos
     */
    public HitResult(EntityRef entity, Vector3f hitPoint, Vector3f hitNormal, Vector3i blockPos) {
        this.hit = true;
        this.entity = entity;
        this.hitPoint = hitPoint;
        this.hitNormal = hitNormal;
        this.blockPosition = blockPos;
        this.worldHit = true;
    }

    /**
     * @return true if something was hit, false otherwise.
     */
    public boolean isHit() {
        return hit;
    }

    /**
     * @return The entity hit, or EntityRef.NULL if no entity was hit.
     */
    public EntityRef getEntity() {
        return entity;
    }

    /**
     * Returns the point where the hit took place.
     *
     * @return null if isHit() == false, otherwise the point where the hit took
     *         place.
     */
    public Vector3f getHitPoint() {
        return hitPoint;
    }

    /**
     * Returns the normal of surface on which the hit took place.
     *
     * @return null if isHit() == false, otherwise the normal of surface on
     *         which the hit took place.
     */
    public Vector3f getHitNormal() {
        return hitNormal;
    }

    /**
     * @return The block where the hit took place. If the world was hit, it will
     *         return the location of the block that was hit. Otherwise it returns the
     *         block location inside which the hit took place. This is different from
     *         the block position of the entity that got hit!
     */
    public Vector3i getBlockPosition() {
        return blockPosition;
    }

    /**
     * Returns true if the hit has hit the world, rather than an entity.
     *
     * @return true if the world has been hit, false otherwise.
     */
    public boolean isWorldHit() {
        return worldHit;
    }
}
TOP

Related Classes of org.terasology.physics.HitResult

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.