@@ -212,82 +212,140 @@ Phaser.Physics.Ninja.AABB.prototype = {
212212 */
213213 reportCollisionVsBody : function ( px , py , dx , dy , obj ) {
214214
215- // Here - we check if obj is immovable, etc and then we canswap the p/o values below depending on which is heavy
216- // But then still need to work out how to split force
217-
218- var p = this . pos ;
219- var o = this . oldpos ;
220-
221- // Calc velocity
222- var vx = p . x - o . x ;
223- var vy = p . y - o . y ;
224-
225- // Find component of velocity parallel to collision normal
226- var dp = ( vx * dx + vy * dy ) ;
227- var nx = dp * dx ; //project velocity onto collision normal
228-
229- var ny = dp * dy ; //nx,ny is normal velocity
230-
231- var tx = vx - nx ; //px,py is tangent velocity
232- var ty = vy - ny ;
215+ var vx1 = this . pos . x - this . oldpos . x ; // Calc velocity of this object
216+ var vy1 = this . pos . y - this . oldpos . y ;
217+ var dp1 = ( vx1 * dx + vy1 * dy ) ; // Find component of velocity parallel to collision normal
218+ var nx1 = dp1 * dx ; // Project velocity onto collision normal
219+ var ny1 = dp1 * dy ; // nx, ny is normal velocity
220+
221+ var dx2 = dx * - 1 ;
222+ var dy2 = dy * - 1 ;
223+ var vx2 = obj . pos . x - obj . oldpos . x ; // Calc velocity of colliding object
224+ var vy2 = obj . pos . y - obj . oldpos . y ;
225+ var dp2 = ( vx2 * dx2 + vy2 * dy2 ) ; // Find component of velocity parallel to collision normal
226+ var nx2 = dp2 * dx2 ; // Project velocity onto collision normal
227+ var ny2 = dp2 * dy2 ; // nx, ny is normal velocity
228+
229+ console . log ( this . body . sprite . name , 'hit' , obj . body . sprite . name , 'at' , px , py , 'dx' , dx , dy , 'dx2' , dx2 , dy2 ) ;
230+ console . log ( this . body . sprite . name , 'x' , ( this . pos . x + this . xw ) , obj . body . sprite . name , 'x' , ( obj . pos . x - obj . xw ) ) ;
231+ console . log ( 'vx1' , vx1 , vy1 , 'dp1' , dp1 , 'nx1' , nx1 , ny1 ) ;
232+ console . log ( 'vx2' , vx2 , vy2 , 'dp2' , dp2 , 'nx2' , nx2 , ny2 ) ;
233233
234234 // We only want to apply collision response forces if the object is travelling into, and not out of, the collision
235- var b , bx , by , fx , fy ;
236235
237- if ( dp < 0 )
236+ if ( this . body . immovable && obj . body . immovable )
238237 {
239- fx = tx * this . body . friction ;
240- fy = ty * this . body . friction ;
238+ // Split the separation then return, no forces applied as they come to a stand-still
239+ px *= 0.5 ;
240+ py *= 0.5 ;
241241
242- b = 1 + this . body . bounce ;
242+ this . pos . add ( px , py ) ;
243+ this . oldpos . set ( this . pos . x , this . pos . y ) ;
243244
244- bx = ( nx * b ) ;
245- by = ( ny * b ) ;
245+ obj . pos . subtract ( px , py ) ;
246+ obj . oldpos . set ( obj . pos . x , obj . pos . y ) ;
247+
248+ return ;
246249 }
247- else
250+ else if ( ! this . body . immovable && ! obj . body . immovable )
248251 {
249- // Moving out of collision, do not apply forces
250- bx = by = fx = fy = 0 ;
252+ px *= 0.5 ;
253+ py *= 0.5 ;
254+
255+ this . pos . add ( px , py ) ;
256+ this . oldpos . set ( this . pos . x , this . pos . y ) ;
257+
258+ obj . pos . subtract ( px , py ) ;
259+ obj . oldpos . set ( obj . pos . x , obj . pos . y ) ;
260+
261+ // x velocity
262+ var nv1 = Math . sqrt ( ( vx2 * vx2 * 1 ) / 1 ) * ( ( vx2 > 0 ) ? 1 : - 1 ) ;
263+ var nv2 = Math . sqrt ( ( vx1 * vx1 * 1 ) / 1 ) * ( ( vx1 > 0 ) ? 1 : - 1 ) ;
264+ var avg = ( nv1 + nv2 ) * 0.5 ;
265+ nv1 -= avg ;
266+ nv2 -= avg ;
267+
268+ this . pos . x += avg + nv1 * this . body . bounce ;
269+ obj . pos . x += avg + nv2 * obj . body . bounce ;
270+
271+ // y velocity
272+ var nv1 = Math . sqrt ( ( vy2 * vy2 * 1 ) / 1 ) * ( ( vy2 > 0 ) ? 1 : - 1 ) ;
273+ var nv2 = Math . sqrt ( ( vy1 * vy1 * 1 ) / 1 ) * ( ( vy1 > 0 ) ? 1 : - 1 ) ;
274+ var avg = ( nv1 + nv2 ) * 0.5 ;
275+ nv1 -= avg ;
276+ nv2 -= avg ;
277+
278+ this . pos . y += avg + nv1 * this . body . bounce ;
279+ obj . pos . y += avg + nv2 * obj . body . bounce ;
280+ }
281+ else if ( ! this . body . immovable )
282+ {
283+ /*
284+ if (dp1 < 0)
285+ {
286+ this.pos.add(px, py);
287+ // px + bounce + friction
288+ this.oldpos.x += px + (nx1 * (1 + this.body.bounce)) + ((vx2 - nx1) * this.body.friction);
289+ this.oldpos.y += py + (ny1 * (1 + this.body.bounce)) + ((vy2 - ny1) * this.body.friction);
290+ }
291+ else
292+ {
293+ // Moving out of collision, do not apply forces
294+ this.pos.add(px, py);
295+ this.oldpos.add(px, py);
296+ }
297+ */
298+ }
299+ else if ( ! obj . body . immovable )
300+ {
301+ /*
302+ if (dp2 < 0)
303+ {
304+ obj.pos.add(px, py);
305+ // px + bounce + friction
306+ obj.oldpos.x += px + (nx2 * (1 + obj.body.bounce)) + ((vx2 - nx2) * obj.body.friction);
307+ obj.oldpos.y += py + (ny2 * (1 + obj.body.bounce)) + ((vy2 - ny2) * obj.body.friction);
308+ }
309+ else
310+ {
311+ // Moving out of collision, do not apply forces
312+ obj.pos.add(px, py);
313+ obj.oldpos.add(px, py);
314+ }
315+ */
251316 }
252317
253- // working version
254- // p.x += px;
255- // p.y += py;
256- // o.x += px + bx + fx;
257- // o.y += py + by + fy;
258-
259- // Project object out of collision (applied to the position value)
260- p . x += px ;
261- p . y += py ;
262-
263- // obj.pos.x += px;
264- // obj.pos.y += py;
265-
266-
267- // Apply bounce+friction impulses which alter velocity (applied to old position, thus setting a sort of velocity up)
268- var rx = px + bx + fx ;
269- var ry = py + by + fy ;
270-
271- // let's pretend obj is immovable
272- // rx *= -1;
273- // ry *= -1;
274-
275-
276- // Now let's share the load
277- o . x += rx ;
278- o . y += ry ;
279318
280- // work out objs velocity
281319
320+ /*
321+ if (!body1.immovable && !body2.immovable)
322+ {
323+ this._overlap *= 0.5;
282324
283- // rx *= -1 ;
284- // ry *= -1 ;
325+ body1.x = body1.x - this._overlap ;
326+ body2.x += this._overlap ;
285327
286- // obj.oldpos.x += rx;
287- // obj.oldpos.y += ry;
328+ this._newVelocity1 = Math.sqrt((this._velocity2 * this._velocity2 * body2.mass) / body1.mass) * ((this._velocity2 > 0) ? 1 : -1);
329+ this._newVelocity2 = Math.sqrt((this._velocity1 * this._velocity1 * body1.mass) / body2.mass) * ((this._velocity1 > 0) ? 1 : -1);
330+ this._average = (this._newVelocity1 + this._newVelocity2) * 0.5;
331+ this._newVelocity1 -= this._average;
332+ this._newVelocity2 -= this._average;
288333
334+ body1.velocity.x = this._average + this._newVelocity1 * body1.bounce.x;
335+ body2.velocity.x = this._average + this._newVelocity2 * body2.bounce.x;
336+ }
337+ else if (!body1.immovable)
338+ {
339+ body1.x = body1.x - this._overlap;
340+ body1.velocity.x = this._velocity2 - this._velocity1 * body1.bounce.x;
341+ }
342+ else if (!body2.immovable)
343+ {
344+ body2.x += this._overlap;
345+ body2.velocity.x = this._velocity1 - this._velocity2 * body2.bounce.x;
346+ }
289347
290- // console.log(this.body.sprite.name, 'o.x', rx, ry, obj);
348+ */
291349
292350 } ,
293351
0 commit comments