## 1 decade ago by Robodude

First, I haven't taken trig in a LONG time, but I was hoping to get some help with this 'cause I've spent several hours now trying to figure it out.

Weltmeister has the sloped tiles and I'm trying to make entities bounce off them at the correct angle.

How can I do this?

I know that the res.collision.slop object has a couple values that can be needed to determine the angle of reflection.

I think that

nAngle gives me the angle of the normal line with respect to a straight line going towards the right is 0 radian.

tAngle gives me the angle of the collision triangle with respect to a straight line going towards the right is 0 radian

here's an image of how I understand what's going on.

Weltmeister has the sloped tiles and I'm trying to make entities bounce off them at the correct angle.

How can I do this?

I know that the res.collision.slop object has a couple values that can be needed to determine the angle of reflection.

I think that

var slope = res.collision.slope; var ny = slope.ny; var nx = slope.nx; var tx = slope.x; var ty = slope.y; var nAngle = Math.atan2(nx, ny); var tAngle = Math.atan2(tx, ty);

nAngle gives me the angle of the normal line with respect to a straight line going towards the right is 0 radian.

tAngle gives me the angle of the collision triangle with respect to a straight line going towards the right is 0 radian

here's an image of how I understand what's going on.

## 1 decade ago by Robodude

EDIT: It wasn't working for all the different slopes, so I had to make some adjustments. I'm not sure it's perfect, but it looks correct.

I think I got it... switching the values to degrees instead of radians helped me understand what was going on much easier.

I think I got it... switching the values to degrees instead of radians helped me understand what was going on much easier.

calculateVelocity: function(angle){ this.initialVel.y = Math.sin(angle) * this.desiredVel; this.initialVel.x = Math.cos(angle) * this.desiredVel; this.vel.x = this.initialVel.x; this.maxVel.x = this.initialVel.x; this.vel.y = this.initialVel.y; this.maxVel.y = this.initialVel.y; this.angle = angle; }, getAngle: function(){ return Math.atan(this.vel.y/this.vel.x); }, handleMovementTrace: function(res){ this.parent(res); if (res.collision.x || res.collision.y || res.collision.slope){ var pi = Math.PI; this.bounceCounter++; console.log(this.bounceCounter); if (this.bounceCounter > this.bounceLimit) { this.kill(); } console.log("Before Angle: " + this.r2d(this.angle)); this.previousAngle = this.angle; var ny; var nx; var tx; var ty; var iAngle; var x; if (res.collision.x) { console.log("*** HIT X ***"); nx = 0; ny = 1; tx = 1; ty = 0; var x = iAngle = pi - (this.angle); } if (res.collision.y) { console.log("*** HIT Y ***"); nx = 1; ny = 0; tx = 0; ty = 1; var x = iAngle = -(this.angle); } if (res.collision.slope) { console.log("*** HIT SLOPE ***"); var slope = res.collision.slope; ny = slope.ny; nx = slope.nx; tx = slope.x; ty = slope.y; iAngle = this.angle * -1; var nAngle = Math.atan2(ny, nx); var tAngle = Math.atan2(ty, tx); var d = pi - iAngle; var d2; if ((nAngle >= 0 && tAngle >= 0) || (nAngle <= 0 && tAngle <= 0)) { d2 = pi - (d + (pi/2)); } else { d2 = -(pi + d) + (pi/2); } x = (d2); } this.calculateVelocity(x); } },

## 1 decade ago by dominic

Also note the existence of Number.toDeg :)

Page 1 of 1

« first
« previous
next ›
last »