你怎么反映向量在另一个载体?向量、你怎么、载体

2023-09-08 14:28:24 作者:日晖下浅浅旳微笑

我使用AS3编程一些碰撞检测的Flash游戏时遇到了问题搞清楚如何反弹球断行。我跟踪了重新presents球的2D速度,我想,以反映它在垂直于该球的使用(又名正常)碰撞行向量的载体。我的问题是,我不知道如何找出新的向量(这也反映了正常的)。我想,你可以用Math.atan2找到正常和球的矢量之间的差别,但我不知道如何扩大,要解决我的问题。

I'm using AS3 to program some collision detection for a flash game and am having trouble figuring out how to bounce a ball off of a line. I keep track of a vector that represents the ball's 2D velocity and I'm trying to reflect it over the vector that is perpendicular to the line that the ball's colliding with (aka the normal). My problem is that I don't know how to figure out the new vector (that's reflected over the normal). I figured that you can use Math.atan2 to find the difference between the normal and the ball's vector but I'm not sure how to expand that to solve my problem.

推荐答案

向量代数 - 您想要的反弹载体: VEC 1是球的运动矢量和VEC 2是表面/线向量:

Vector algebra - You want the "bounce" vector: vec1 is the ball's motion vector and vec2 is the surface/line vector:

// 1. Find the dot product of vec1 and vec2
// Note: dx and dy are vx and vy divided over the length of the vector (magnitude)
var dpA:Number = vec1.vx * vec2.dx + vec1.vy * vec2.dy;

// 2. Project vec1 over vec2
var prA_vx:Number = dpA * vec2.dx;
var prA_vy:Number = dpA * vec2.dy;

// 3. Find the dot product of vec1 and vec2's normal
// (left or right normal depending on line's direction, let's say left)
var dpB:Number = vec1.vx * vec2.leftNormal.dx + vec1.vy * vec2.leftNormal.dy;

// 4. Project vec1 over vec2's left normal
var prB_vx:Number = dpB * vec2.leftNormal.dx;
var prB_vy:Number = dpB * vec2.leftNormal.dy;

// 5. Add the first projection prA to the reverse of the second -prB
var new_vx:Number = prA_vx - prB_vx;
var new_vy:Number = prA_vy - prB_vy;

指定的速度为你的球的运动矢量,让它反弹。 PS: vec.leftNormal - > VX = vec.vy; VY = -vec.vx;      vec.rightNormal - > VX = -vec.vy; VY = vec.vx;

Assign those velocities to your ball's motion vector and let it bounce. PS: vec.leftNormal --> vx = vec.vy; vy = -vec.vx; vec.rightNormal --> vx = -vec.vy; vy = vec.vx;