0

I have been trying to implement The second answer of this question

My variables are mainly named the same as in the link.

Here they calculate the bounce angle of a point on a rotated surface with normals, But I can't seem to do it, am I doing something wrong?

The value output I'm expecting is the new velocity after bouncing on the rotated line, currently my output is doing all crazy kind of things, bouncing to high, bouncing the other way and sometimes the right way but mostly ignoring the rotated angle of my line.

Here's my current Code:

Variables:

  • Top is the first point of my line
  • Right is the second point of my line
  • n is the normal
  • v is the velocity
  • dotv is n * v
  • dotn is n * n

Old Code:

sf::Vector2f n(-(top.y - right.y),(top.x - right.x));
sf::Vector2f dotv = sf::Vector2f(ball.getSpeed().x * n.x, ball.getSpeed().y * n.y);
sf::Vector2f dotn = sf::Vector2f(n.x*n.x,n.y*n.y);
sf::Vector2f u = sf::Vector2f(dotv.x/dotn.x,dotv.y/dotn.y);
u = sf::Vector2f(u.x*n.x,u.y*n.y);
sf::Vector2f w = ball.getSpeed() - u;
ball.setSpeed(sf::Vector2f((sf::Vector2f(w.x*0.5,w.y*0.5)-u)));

Edit:

New Code:

sf::Vector2f n(-(top.y - right.y),(top.x - right.x));
double dotv = ball.getSpeed().x*n.x + ball.getSpeed().y*n.y;
double dotn = n.x*n.x + n.y*n.y;
sf::Vector2f u = sf::Vector2f((dotv/dotn)*n.x, (dotv/dotn)*n.y);
sf::Vector2f w = ball.getSpeed() - u;
ball.setSpeed(sf::Vector2f(ball.getSpeed().x,ball.getSpeed().y) - w);

At first I made the mistake of calculating dotproducts as vectors this has been resolved, now it still gives me a strange output It fires my ball directly trough my object in the angle of the reversed normal

Any help would be greatly appreciated.

Community
  • 1
  • 1
user3488393
  • 47
  • 1
  • 1
  • 6

1 Answers1

0

One problem I see is that you have dot products as vectors. A dot product results in a scalar (single value).

Also, to make your life a lot easier, you make functions for vector operations and even overload operators when appropriate. E.g. vector addition, subtraction. It's probably best to regular functions for dot product and cross product. Here are some examples of what I mean

class Vector2f
{
    // Member of Vector2f
    X& operator+=(const Vector2f& other)
    {
        x += other.x;
        y += other.y;

        return *this;
    }

};

// Not member of Vector2f
inline Vector2f operator+(Vector2f a, const Vector2f& b)
{
  a += b;
  return a;
}

double dot(const Vector2f& a, const Vector2f& b)
{
    return a.getX()*b.getX() + a.getY()*b.getY();
}
Simon
  • 6,293
  • 2
  • 28
  • 34
  • First of all thanks for your input. but the scalar is calculated from 2 vectors, in the link I send you they calculate it from the normal and the velocity, how can I make one value from 2 2value values? – user3488393 Apr 10 '14 at 08:08
  • I updated my answer with some code. For this kind of programming knowing linear algebra and a bit of physics is really helpful. Here's a link to read more about the dot product http://en.wikipedia.org/wiki/Dot_product – Simon Apr 10 '14 at 08:23
  • A question about the second answer in the link, it states: u = (v · n / n · n) n, v*n and n*n are dotproducts made out of vectors, but how would I do (dotv/dotn)*n, what would n be? I assume you can't do double * vector.(should I do n.x + n.y to get n?) – user3488393 Apr 10 '14 at 08:30
  • Yes, you can multiply a vector with a scalar. You just multiply each of the vectors components with the scalar. http://www.dummies.com/how-to/content/using-scalar-multiplication-with-vectors.html – Simon Apr 10 '14 at 08:35
  • You helped a lot earlier I edited my new code in (will also use your methods after I got it working). But could you take another look at my new code? currently with this new code my balls fly trough my objects in the angle of the normal. – user3488393 Apr 10 '14 at 08:55