Currently, I am in the process of developing a simulation involving a bouncing ball. In this simulation, users have the ability to place lines on the canvas that the ball can collide with by dragging from one point to another. There are specifically four types of lines that can be generated:
https://i.sstatic.net/qxINA.png
The structure that represents a line is outlined as follows:
export interface pathSection {
xfrom: number;
yfrom: number;
xto: number;
yto: number;
length: number;
}
It is important to note that the first and third lines in the image do not produce identical values from
Math.atan2(yto - yfrom, xto - from);
Given the intricate nature of the surfaces involved, it is necessary to determine the angle between a moving object and the surface at the moment of collision:
https://i.sstatic.net/tVQZY.png
When the ball collides with the surface, it does so at an angle denoted as 'a'.
However, I am encountering difficulties in calculating the angle between the two vectors. Here is my current approach:
var dx = this.path[index_for_path_section].xfrom - this.path[index_for_path_section].xto;
var dy = this.path[index_for_path_section].yfrom - this.path[index_for_path_section].yto;
var posX = this.particle.pos.x;
var posY = this.particle.pos.y;
var posNextX = posX + this.particle.v.x;
var posNextY = posY + this.particle.v.y;
var angleOfRamp = Math.atan2(dy, dx);
var angleOfvelocity = Math.atan2(posNextY - posY, posNextX - posX);
var angleBetween = angleOfRamp - angleOfvelocity;
This calculation is then utilized to determine the object's speed post-collision:
var spd = Math.sqrt(this.particle.v.x * this.particle.v.x + this.particle.v.y * this.particle.v.y);
var restitution = this.elasticity / 100;
this.particle.v.x = restitution * spd * Math.cos(angleBetween);
this.particle.v.y = restitution * spd * Math.sin(angleBetween);
However, the angle calculated seems to be approximately -4.5 Pi, translating to around -90 degrees when the object is moving directly downward and the surface is inclined at approximately 45-60 degrees...
https://i.sstatic.net/BQ4bh.png
The red arrow indicates the trajectory of the object intersecting with the surface - the white dots signify points where a collision has been detected between the object and the surface.
If you have any insights on how to accurately determine the applicable angle between the two velocities and the line, your assistance would be greatly appreciated!
Please note that I have made an attempt to reference this solution, but I have encountered difficulties in adapting it to fit my specific project.