mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-06 20:07:09 +00:00
Math: Added RayTraceResult, removed dependence on MovingObjectPosition
MOP doesn't make any sense anyway. RayTraceResult is a container which represents the point at which a line hits a bounding box. No dependence on blocks or entities is wanted or needed. MovingObjectPosition has API changes to allow it to wrap RayTraceResult, but nothing uses MOP anymore anyway. This would allow modularisation of the pocketmine\\math namespace.
This commit is contained in:
parent
fe4b5498e6
commit
45b02d92d4
@ -30,9 +30,9 @@ use pocketmine\entity\Entity;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\item\ItemFactory;
|
||||
use pocketmine\level\Level;
|
||||
use pocketmine\level\MovingObjectPosition;
|
||||
use pocketmine\level\Position;
|
||||
use pocketmine\math\AxisAlignedBB;
|
||||
use pocketmine\math\RayTraceResult;
|
||||
use pocketmine\math\Vector3;
|
||||
use pocketmine\metadata\Metadatable;
|
||||
use pocketmine\metadata\MetadataValue;
|
||||
@ -615,15 +615,15 @@ class Block extends Position implements BlockIds, Metadatable{
|
||||
* @param Vector3 $pos1
|
||||
* @param Vector3 $pos2
|
||||
*
|
||||
* @return MovingObjectPosition|null
|
||||
* @return RayTraceResult|null
|
||||
*/
|
||||
public function calculateIntercept(Vector3 $pos1, Vector3 $pos2) : ?MovingObjectPosition{
|
||||
public function calculateIntercept(Vector3 $pos1, Vector3 $pos2) : ?RayTraceResult{
|
||||
$bbs = $this->getCollisionBoxes();
|
||||
if(empty($bbs)){
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var MovingObjectPosition|null $currentHit */
|
||||
/** @var RayTraceResult|null $currentHit */
|
||||
$currentHit = null;
|
||||
/** @var int|float $currentDistance */
|
||||
$currentDistance = PHP_INT_MAX;
|
||||
@ -641,12 +641,6 @@ class Block extends Position implements BlockIds, Metadatable{
|
||||
}
|
||||
}
|
||||
|
||||
if($currentHit !== null){
|
||||
$currentHit->blockX = $this->x;
|
||||
$currentHit->blockY = $this->y;
|
||||
$currentHit->blockZ = $this->z;
|
||||
}
|
||||
|
||||
return $currentHit;
|
||||
}
|
||||
|
||||
|
@ -31,7 +31,6 @@ use pocketmine\event\entity\EntityDamageByEntityEvent;
|
||||
use pocketmine\event\entity\EntityDamageEvent;
|
||||
use pocketmine\event\entity\ProjectileHitEvent;
|
||||
use pocketmine\level\Level;
|
||||
use pocketmine\level\MovingObjectPosition;
|
||||
use pocketmine\math\Vector3;
|
||||
use pocketmine\nbt\tag\CompoundTag;
|
||||
|
||||
@ -136,13 +135,13 @@ abstract class Projectile extends Entity{
|
||||
}
|
||||
|
||||
$axisalignedbb = $entity->boundingBox->grow(0.3, 0.3, 0.3);
|
||||
$ob = $axisalignedbb->calculateIntercept($this, $moveVector);
|
||||
$rayTraceResult = $axisalignedbb->calculateIntercept($this, $moveVector);
|
||||
|
||||
if($ob === null){
|
||||
if($rayTraceResult === null){
|
||||
continue;
|
||||
}
|
||||
|
||||
$distance = $this->distanceSquared($ob->hitVector);
|
||||
$distance = $this->distanceSquared($rayTraceResult->hitVector);
|
||||
|
||||
if($distance < $nearDistance){
|
||||
$nearDistance = $distance;
|
||||
@ -151,14 +150,8 @@ abstract class Projectile extends Entity{
|
||||
}
|
||||
|
||||
if($nearEntity !== null){
|
||||
$movingObjectPosition = MovingObjectPosition::fromEntity($nearEntity);
|
||||
}
|
||||
|
||||
if($movingObjectPosition !== null){
|
||||
if($movingObjectPosition->entityHit !== null){
|
||||
$this->onCollideWithEntity($movingObjectPosition->entityHit);
|
||||
return false;
|
||||
}
|
||||
$this->onCollideWithEntity($nearEntity);
|
||||
return false;
|
||||
}
|
||||
|
||||
if($this->isCollided and !$this->hadCollision){ //Collided with a block
|
||||
|
@ -23,70 +23,53 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\level;
|
||||
|
||||
use pocketmine\block\Block;
|
||||
use pocketmine\entity\Entity;
|
||||
use pocketmine\math\Vector3;
|
||||
use pocketmine\math\RayTraceResult;
|
||||
|
||||
class MovingObjectPosition{
|
||||
public const TYPE_BLOCK_COLLISION = 0;
|
||||
public const TYPE_ENTITY_COLLISION = 1;
|
||||
|
||||
/** @var RayTraceResult */
|
||||
public $hitResult;
|
||||
|
||||
/** @var int */
|
||||
public $typeOfHit;
|
||||
|
||||
/** @var int|null */
|
||||
public $blockX;
|
||||
/** @var int|null */
|
||||
public $blockY;
|
||||
/** @var int|null */
|
||||
public $blockZ;
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
* Which side was hit. If its -1 then it went the full length of the ray trace.
|
||||
* -1 or one of the Vector3::SIDE_* constants
|
||||
*/
|
||||
public $sideHit;
|
||||
|
||||
/** @var Vector3 */
|
||||
public $hitVector;
|
||||
|
||||
/** @var Entity|null */
|
||||
public $entityHit = null;
|
||||
/** @var Block|null */
|
||||
public $blockHit = null;
|
||||
|
||||
protected function __construct(){
|
||||
|
||||
protected function __construct(int $hitType, RayTraceResult $hitResult){
|
||||
$this->typeOfHit = $hitType;
|
||||
$this->hitResult = $hitResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $x
|
||||
* @param int $y
|
||||
* @param int $z
|
||||
* @param int $side
|
||||
* @param Vector3 $hitVector
|
||||
* @param Block $block
|
||||
* @param RayTraceResult $result
|
||||
*
|
||||
* @return MovingObjectPosition
|
||||
*/
|
||||
public static function fromBlock(int $x, int $y, int $z, int $side, Vector3 $hitVector) : MovingObjectPosition{
|
||||
$ob = new MovingObjectPosition;
|
||||
$ob->typeOfHit = self::TYPE_BLOCK_COLLISION;
|
||||
$ob->blockX = $x;
|
||||
$ob->blockY = $y;
|
||||
$ob->blockZ = $z;
|
||||
$ob->sideHit = $side;
|
||||
$ob->hitVector = $hitVector->asVector3();
|
||||
public static function fromBlock(Block $block, RayTraceResult $result) : MovingObjectPosition{
|
||||
$ob = new MovingObjectPosition(self::TYPE_BLOCK_COLLISION, $result);
|
||||
$ob->blockHit = $block;
|
||||
return $ob;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Entity $entity
|
||||
* @param Entity $entity
|
||||
*
|
||||
* @param RayTraceResult $result
|
||||
*
|
||||
* @return MovingObjectPosition
|
||||
*/
|
||||
public static function fromEntity(Entity $entity) : MovingObjectPosition{
|
||||
$ob = new MovingObjectPosition;
|
||||
$ob->typeOfHit = self::TYPE_ENTITY_COLLISION;
|
||||
public static function fromEntity(Entity $entity, RayTraceResult $result) : MovingObjectPosition{
|
||||
$ob = new MovingObjectPosition(self::TYPE_ENTITY_COLLISION, $result);
|
||||
$ob->entityHit = $entity;
|
||||
$ob->hitVector = $entity->asVector3();
|
||||
|
||||
return $ob;
|
||||
}
|
||||
}
|
||||
|
@ -23,8 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\math;
|
||||
|
||||
use pocketmine\level\MovingObjectPosition;
|
||||
|
||||
class AxisAlignedBB{
|
||||
|
||||
/** @var float */
|
||||
@ -353,15 +351,15 @@ class AxisAlignedBB{
|
||||
|
||||
/**
|
||||
* Performs a ray-trace and calculates the point on the AABB's edge nearest the start position that the ray-trace
|
||||
* collided with. Returns a MovingObjectPosition with colliding vector closest to the start position.
|
||||
* collided with. Returns a RayTraceResult with colliding vector closest to the start position.
|
||||
* Returns null if no colliding point was found.
|
||||
*
|
||||
* @param Vector3 $pos1
|
||||
* @param Vector3 $pos2
|
||||
*
|
||||
* @return MovingObjectPosition|null
|
||||
* @return RayTraceResult|null
|
||||
*/
|
||||
public function calculateIntercept(Vector3 $pos1, Vector3 $pos2){
|
||||
public function calculateIntercept(Vector3 $pos1, Vector3 $pos2) : ?RayTraceResult{
|
||||
$v1 = $pos1->getIntermediateWithXValue($pos2, $this->minX);
|
||||
$v2 = $pos1->getIntermediateWithXValue($pos2, $this->maxX);
|
||||
$v3 = $pos1->getIntermediateWithYValue($pos2, $this->minY);
|
||||
@ -423,7 +421,7 @@ class AxisAlignedBB{
|
||||
$f = Vector3::SIDE_SOUTH;
|
||||
}
|
||||
|
||||
return MovingObjectPosition::fromBlock(0, 0, 0, $f, $vector);
|
||||
return new RayTraceResult($this, $f, $vector);
|
||||
}
|
||||
|
||||
public function __toString(){
|
||||
|
75
src/pocketmine/math/RayTraceResult.php
Normal file
75
src/pocketmine/math/RayTraceResult.php
Normal file
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\math;
|
||||
|
||||
/**
|
||||
* Class representing a ray trace collision with an AxisAlignedBB
|
||||
*/
|
||||
class RayTraceResult{
|
||||
|
||||
/**
|
||||
* @var AxisAlignedBB
|
||||
*/
|
||||
public $bb;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $hitFace;
|
||||
/**
|
||||
* @var Vector3
|
||||
*/
|
||||
public $hitVector;
|
||||
|
||||
/**
|
||||
* @param AxisAlignedBB $bb
|
||||
* @param int $hitFace one of the Vector3::SIDE_* constants
|
||||
* @param Vector3 $hitVector
|
||||
*/
|
||||
public function __construct(AxisAlignedBB $bb, int $hitFace, Vector3 $hitVector){
|
||||
$this->bb = $bb;
|
||||
$this->hitFace = $hitFace;
|
||||
$this->hitVector = $hitVector;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AxisAlignedBB
|
||||
*/
|
||||
public function getBoundingBox() : AxisAlignedBB{
|
||||
return $this->bb;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getHitFace() : int{
|
||||
return $this->hitFace;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Vector3
|
||||
*/
|
||||
public function getHitVector() : Vector3{
|
||||
return $this->hitVector;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user