Sign in to follow this  
Amablue

Simple 2d reflection question

Recommended Posts

I have a ball bouncing against a wall defined by points p1 and p2. When the ball hits the wall it should bounce off it realistically. I've already got the code to find the collision and to push it back outside of the shape, I just can't think of how to decide the ball's new velocity vector. I'm thinking it's just going to be a reflection over the normal of the wall, but I can't think of how to do that.

Share this post


Link to post
Share on other sites
I use this. Not sure if it's the best way, but it works alright.

vLine is just pt2 - pt1 of the line if I remember correctly.
[source="c#"]
var lineMag:Number = Math.sqrt(vLine.x * vLine.x + vLine.y * vLine.y);

var v1v:Point = new Point(particles.vel.x, particles.vel.y);
var v2v:Point = new Point(vLine.x, vLine.y);
var v2n:Point = new Point(v2v.y, -v2v.x);//normal

var proj1:Point = new Point();
var proj2:Point = new Point();

var dp1:Number = v1v.x * v2v.x + v1v.y * v2v.y;

proj1.x = ( dp1 / (v2v.x * v2v.x + v2v.y * v2v.y) ) * v2v.x;
proj1.y = ( dp1 / (v2v.x * v2v.x + v2v.y * v2v.y) ) * v2v.y;

var dp2:Number = v1v.x * v2n.x + v1v.y * v2n.y;

proj2.x = -( dp2 / (v2n.x * v2n.x + v2n.y * v2n.y) ) * v2n.x;
proj2.y = -( dp2 / (v2n.x * v2n.x + v2n.y * v2n.y) ) * v2n.y;

var friction:Number = 1;
var bounce:Number = 1;

particles.vel.x = proj1.x * friction + proj2.x * bounce;
particles.vel.y = proj1.y * friction + proj2.y * bounce;


sorry it's in actionscript. I'm gonna go sleep.

Share this post


Link to post
Share on other sites
When you get the chance, could you explain the code a little for me? I'm used to C style stuff (C# is what I'm going for specifically, but I know java and c/c++ too) and it's hard to translate that code to C# when I don't know the syntax or the algorithm.

Share this post


Link to post
Share on other sites
http://www.tonypa.pri.ee/vectors/tut06.html

There's the tutorial.

Okay honestly I find it hard that you have a problem just converting the code.

Point is just Vector2 basically from the Microsoft.DirectX namespace and Number is just double, but float works fine. so:

Get some graph paper out and draw a line with some coordinates. Then draw a vector that represents a particle's path (aka velocity in this case). Get a calculator and step through the code to understand it. There's not much I can say for you to understand it.

[source = "c#"]
Vector2 vLine = p2 - p1;
//above line just does this:
//vLine.x = p2.x - p1.x;
//vLine.y = p2.y - p1.y;
float lineMag = (float)Math.sqrt(vLine.x * vLine.x + vLine.y * vLine.y);

Vector2 v1v = new Vector2(particle.vel.x, particle.vel.y);
Vector2 v2v = new Vector2(vLine.x, vLine.y);
Vector2 v2n = new Vector2(v2v.y, -v2v.x);//normal

Vector2 proj1 = new Vector2();
Vector2 proj2 = new Vector2();

float dp1 = v1v.x * v2v.x + v1v.y * v2v.y;

proj1.x = ( dp1 / (v2v.x * v2v.x + v2v.y * v2v.y) ) * v2v.x;
proj1.y = ( dp1 / (v2v.x * v2v.x + v2v.y * v2v.y) ) * v2v.y;

float dp2 = v1v.x * v2n.x + v1v.y * v2n.y;

proj2.x = -( dp2 / (v2n.x * v2n.x + v2n.y * v2n.y) ) * v2n.x;
proj2.y = -( dp2 / (v2n.x * v2n.x + v2n.y * v2n.y) ) * v2n.y;

float friction = 1;
float bounce = 1;

particle.vel.x = proj1.x * friction + proj2.x * bounce;
particle.vel.y = proj1.y * friction + proj2.y * bounce;

Share this post


Link to post
Share on other sites
Thanks for the help. I've never used or really looked at Actionscript before so it's just hard to look at and make sense out of it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this