猫史档案馆


【代码】送大家一个方盒和球的算法框架

用户:星辰Lolita星辰Lolita查看:2 回复:6 评论:2 创建时间:2022-05-04T17:54:36


//这是一个基于集合的算法框架
Box3Bounds3.prototype.scale = function(n){
    const x = this.hi.x - this.lo.x;
    const y = this.hi.y - this.lo.y;
    const z = this.hi.z - this.lo.z;
    const halfX = x * 0.5;
    const halfY = y * 0.5;
    const halfZ = z * 0.5;
    const center = this.lo.add({x: halfX,y: halfY,z: halfZ});
    const offset = {x: halfX * n,y: halfY * n,z: halfZ * n};
    return new Box3Bounds3(center.sub(offset),center.add(offset));
}
Box3Bounds3.prototype.random = function(){
    const x = this.hi.x - this.lo.x;
    const y = this.hi.y - this.lo.y;
    const z = this.hi.z - this.lo.z;
    return this.lo.add({x: x * Math.random(),y: y * Math.random(),z: z * Math.random()});
}
class Angle{
    constructor(angle,up){
        this.angle = angle;
        this.up = up;
    }
    update(angle,up){
        this.angle = angle;
        this.up = up;
    }
    random(){
        return new Angle(Math.random() * Math.PI * 2,(Math.random() - 0.5) * Math.PI);
    }
    towards(){
        return new Box3Vector3(Math.cos(this.angle) * Math.cos(this.up),Math.sin(this.up),Math.sin(this.angle) * Math.cos(this.up));
    }
}
class Round{
    constructor(position/*Box3Vector3*/,radius){
        this.position = position;
        this.radius = radius;
    }
    point(angle){
        const radius = this.radius * Math.cos(angle.up);
        return new Box3Vector3(Math.cos(angle.angle) * radius,Math.sin(angle.up) * this.radius,Math.sin(angle.angle) * radius);
    }
    update(position,radius){
        this.position = position;
        this.radius = radius;
    }
    distance(b){
        if(b instanceof Round){
            return this.position.distance(b.position) - this.radius - b.radius;
        }
        if(b instanceof Cube){
            return this.position.distance(b.position) - b.pointOfPoint(this.position).distance(b.position) - this.radius;
        }
    }
    intersects(b){
        return this.distance(b) > 0;
    }
}
class Cube{
    constructor(position/*Box3Vector3*/,bounds/*Box3Vector3*/){
        this.position = position;
        this.bounds = bounds;
    }
    pointOfAngle(angle){
        const bounds = this.getBounds();
        const position = this.position.add(this.bounds.mag() * angle.towards());
        if(position.x < bounds.lo.x)position.x = bounds.lo.x;
        if(position.x > bounds.hi.x)position.x = bounds.hi.x;
        if(position.y < bounds.lo.y)position.y = bounds.lo.y;
        if(position.y > bounds.hi.y)position.y = bounds.hi.y;
        if(position.z < bounds.lo.z)position.z = bounds.lo.z;
        if(position.z > bounds.hi.z)position.z = bounds.hi.z;
        return position;
    }
    pointOfPoint(position){
        const bounds = this.getBounds();
        if(position.x < bounds.lo.x)position.x = bounds.lo.x;
        if(position.x > bounds.hi.x)position.x = bounds.hi.x;
        if(position.y < bounds.lo.y)position.y = bounds.lo.y;
        if(position.y > bounds.hi.y)position.y = bounds.hi.y;
        if(position.z < bounds.lo.z)position.z = bounds.lo.z;
        if(position.z > bounds.hi.z)position.z = bounds.hi.z;
        return position;
    }
    update(position,bounds){
        this.position = position;
        this.bounds = bounds;
    }
    getBounds(){
        return new Box3Bounds3(this.position.sub(this.bounds),this.position.add(this.bounds));
    }
    distance(b){
        if(b instanceof Round){
            return this.position.distance(b.position) - this.pointOfPoint(b.position).distance(this.position) - b.radius;
        }
        if(b instanceof Cube){
            return this.position.distance(b.position) - this.pointOfPoint(b.position).distance(this.position) - b.pointOfPoint(this.position).distance(b.position);
        }
    }
    intersects(b){
        return this.distance(b) > 0;
    }
}


回复

上一页1 页 / 共 1下一页
云游box3der尖头叉子云游box3der尖头叉子

沙发()()()

点赞0


评论


MC_TTMC_TT

sofa

点赞0


评论


灵清帝王吃爆一切厕所灵清帝王吃爆一切厕所

qp()、

点赞0


评论


星辰Lolita星辰Lolita

没试过有没有bug我不知道()

点赞0


评论


yee089yee089

大佬诈尸()

点赞0


评论


卑微OIer卑微OIer

()()()()()我还以为L退了()()()()()()

结果今天起床诈尸了()()()()()()()()()()

点赞0


评论