Simple intersection testing
This commit is contained in:
parent
68c046a953
commit
1644012dfe
2
dist/dwelibs.min.js
vendored
2
dist/dwelibs.min.js
vendored
File diff suppressed because one or more lines are too long
56
src/math.js
56
src/math.js
@ -42,20 +42,70 @@ DWE.Math.Vector2 = class {
|
||||
var len = this.magnitude
|
||||
this.x /= len
|
||||
this.y /= len
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
get isNormal () {
|
||||
return 1 === Math.floor(this.magnitude)
|
||||
}
|
||||
|
||||
get angle () {
|
||||
if (!this.isNormal) return NaN
|
||||
return Math.atan2(this.x, this.y)
|
||||
}
|
||||
|
||||
distance (vec) {
|
||||
if (!(vec instanceof DWE.Math.Vector2)) throw new Error('Distance function takes another Vector2 as an argument.')
|
||||
return Math.abs(Math.sqrt(Math.pow(vec.x - this.x, 2) + Math.pow(vec.y - this.y, 2)))
|
||||
}
|
||||
|
||||
add (vec) {
|
||||
if (!(vec instanceof DWE.Math.Vector2)) throw new Error('Add function takes another Vector2 as an argument.')
|
||||
this.x += vec.x
|
||||
this.y += vec.y
|
||||
return this
|
||||
}
|
||||
|
||||
subtract (vec) {
|
||||
if (!(vec instanceof DWE.Math.Vector2)) throw new Error('Subtract function takes another Vector2 as an argument.')
|
||||
this.x -= vec.x
|
||||
this.y -= vec.y
|
||||
return this
|
||||
}
|
||||
|
||||
multiply (vec) {
|
||||
if (typeof vec === 'number' && !isNaN(vec)) {
|
||||
this.x *= vec
|
||||
this.y *= vec
|
||||
return this
|
||||
}
|
||||
|
||||
if (!(vec instanceof DWE.Math.Vector2)) throw new Error('Multiply function takes a Vector2 or Number as an argument.')
|
||||
this.x *= vec.x
|
||||
this.y *= vec.y
|
||||
return this
|
||||
}
|
||||
|
||||
divide (vec) {
|
||||
if (typeof vec === 'number' && !isNaN(vec)) {
|
||||
this.x /= vec
|
||||
this.y /= vec
|
||||
return this
|
||||
}
|
||||
|
||||
if (!(vec instanceof DWE.Math.Vector2)) throw new Error('Division function takes a Vector2 or Number as an argument.')
|
||||
this.x /= vec.x
|
||||
this.y /= vec.y
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
// A box.
|
||||
|
||||
DWE.Math.Box2 = class {
|
||||
DWE.Math.Box2 = class Box2 extends DWE.Math.Vector2 {
|
||||
constructor (x, y, w, h) {
|
||||
this.x = x
|
||||
this.y = y
|
||||
super(x, y)
|
||||
this.width = w
|
||||
this.height = h
|
||||
}
|
||||
|
23
src/tiled.js
23
src/tiled.js
@ -144,11 +144,28 @@ DWE.Tiled.Map = class {
|
||||
}
|
||||
}
|
||||
|
||||
collideLayer (object, name) {
|
||||
// object: Object that collides (i.e. a player, monster)
|
||||
// name: Name of the layer (generally "static")
|
||||
// effectiveRange: Range around object where tiles are tested for collisions
|
||||
collideLayer (object, origin, viewport, name, effectiveRange) {
|
||||
if (!this.layers[name]) return null
|
||||
var layer = this.layers[name]
|
||||
// TODO: Handle object collisions with map tiles
|
||||
return null
|
||||
var collisions = null
|
||||
var posAbs = new DWE.Math.Box2((object.x * this.scale) + origin.x + layer.x - viewport.x,
|
||||
(object.y * this.scale) + origin.y + layer.y - viewport.y, object.width, object.height)
|
||||
|
||||
for (let i in layer.tiles) {
|
||||
let tile = layer.tiles[i]
|
||||
let tilePosAbs = new DWE.Math.Box2((tile.x * this.tileWidth * this.scale) + origin.x + layer.x + viewport.x,
|
||||
(tile.y * this.tileHeight * this.scale) + origin.y + layer.y + viewport.y, this.tileWidth, this.tileHeight)
|
||||
if (tilePosAbs.distance(posAbs) > effectiveRange) continue
|
||||
if (posAbs.intersectsBox(tilePosAbs)) {
|
||||
if (!collisions) collisions = []
|
||||
collisions.push(tilePosAbs.subtract(posAbs).normalize())
|
||||
}
|
||||
}
|
||||
|
||||
return collisions
|
||||
}
|
||||
|
||||
setScale (scale) {
|
||||
|
Reference in New Issue
Block a user