123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- /** @format */
- import {Line} from './Maths'
- import {Dirty, cObject} from './Object'
- import {cShape} from './Shape'
- const Mat3 = cc.Mat3
- const Quat = cc.Quat
- const Vec3 = cc.Vec3
- const IsIdentity = function (quat: cc.Quat) {
- if (quat.x != 0 || quat.y != 0 || quat.z != 0 || quat.w != 1) return false
- return true
- }
- export class cBody {
- id: number = 0
- mask: number = 0
- group: number = 0
- shape: cShape = null
- object: cObject = null
- priority: number = 0
- // 碰撞体的entity
- entity: number
- //脏区更新标记
- isDirty: number = 1 | 2 | 4
- //缓存
- lower: number = 0
- upper: number = 0
- aabb: Array<number> = [0, 0, 0, 0, 0, 0]
- //状态
- isRemove: boolean = false
- isRetrieve: boolean = false
- isIdentity: boolean = false
- inCollider: boolean = false
- //缓存
- raidus: number = 0
- center: cc.Vec3 = new Vec3()
- rotMat3: cc.Mat3 = new Mat3()
- halfSize: cc.Vec3 = new Vec3()
- scale: cc.Vec3 = new Vec3(1, 1, 1)
- //Agent
- isAgent: boolean = false //Agent 开关
- maxNeighbors: number = 0
- neighborDist: number = 0 //物体半径
- maxVelocity: number = 0 //最大速度
- newVelocity: cc.Vec3 = new Vec3()
- prefVelocity: cc.Vec3 = new Vec3()
- orcaLines: Array<Line> = []
- constructor(obj: cObject) {
- this.object = obj
- }
- updateBound(force: Dirty = Dirty.NON) {
- let object = this.object //force 强制全局刷新
- let isDirty = force | object.isDirty
- if (this.isAgent) {
- let v = object.velocity
- this.newVelocity.x = v.x
- this.newVelocity.y = v.y
- // let pv = this.prefVelocity;
- // pv.x+= (v.x - pv.x)*0.75;
- // pv.y+= (v.y - pv.y)*0.75;
- }
- if (isDirty > 0) {
- let aabbChange = false
- const shape = this.shape
- if (isDirty & Dirty.S) {
- aabbChange = true
- let s = this.getScale()
- this.scale.x = s.x >= 0 ? s.x : -s.x
- this.scale.y = s.y >= 0 ? s.y : -s.y
- this.scale.z = s.z >= 0 ? s.z : -s.z
- }
- if (isDirty & Dirty.R) {
- //旋转更新aabb
- this.isIdentity = IsIdentity(this.getRotation())
- aabbChange = true
- }
- if (aabbChange) shape.updateAABB(this.getScale(), this.getRotMat3(), this.isIdentity)
- const AABB = this.aabb // world aabb
- const aabb = shape.aabb //local aabb
- const p = this.getPosition() //world postion
- AABB[0] = aabb[0] + p.x
- AABB[1] = aabb[1] + p.y
- AABB[2] = aabb[2] + p.z
- AABB[3] = aabb[3] + p.x
- AABB[4] = aabb[4] + p.y
- AABB[5] = aabb[5] + p.z
- this.isDirty = 1 | 2 | 4 //设置脏区标记
- object.isDirty = Dirty.NON //清除object状态
- return true
- }
- return false
- }
- clear() {
- this.id = -1
- this.shape = null
- this.object = null
- this.isRemove = false
- this.inCollider = false
- this.orcaLines.length = 0
- // this.object.body = null;
- }
- //body 坐标统一使用世界数据进行计算
- getScale() {
- return this.object.worldScale
- } // world scale
- getPosition() {
- return this.object.worldPosition
- } //wold position
- getRotation() {
- return this.object.worldRotation
- } //world rotation quat
- getRotMat3() {
- return this.object.worldRotatioMat3
- } //world rotation mat3
- getCenter() {
- if (this.isDirty & 1) {
- this.isDirty &= ~1
- const aabb = this.aabb
- const center = this.center
- center.x = (aabb[0] + aabb[3]) * 0.5
- center.y = (aabb[1] + aabb[4]) * 0.5
- center.z = (aabb[2] + aabb[5]) * 0.5
- }
- return this.center //world center
- }
- getRaidus() {
- if (this.isDirty & 2) {
- this.isDirty &= ~2
- const scale = this.scale
- const raidus = this.shape.radius
- this.raidus = Math.max(scale.x, scale.y, scale.z) * raidus
- }
- return this.raidus //world raidus
- }
- getHalfSize() {
- if (this.isDirty & 4) {
- this.isDirty &= ~4
- const scale = this.scale
- const size = this.shape.size
- const halfSize = this.halfSize
- halfSize.x = scale.x * size.x * 0.5
- halfSize.y = scale.y * size.y * 0.5
- halfSize.z = scale.z * size.z * 0.5
- }
- return this.halfSize //world halfsize
- }
- }
|