How would I go about implementing realistic ball movement in my brickbreaker game? I can’t find anything on google that makes sense. Right now, the ball moves in four directions, NW,NE,SE,SW. It’s basically always moving at a 45 degree angle. I want it to move around and bounce off objects realistically. I’m not sure where to start…
Something to do with the normals of the surface it bounces off.
Not sure of the formula, but I think it involves vectors & dot products.
EDIT: Just saw it is always moving at 45 degree angle.
if(collidesOnXAxisMovement())
{
velocity.x = -velocity.x;
}
if(collidesOnYAxieMovement())
{
velocity.y = -velocity.y;
}
HeroesGraveDev’s version will work, also check this out.
I really think you should use google/your own brain a bit more, though…this is a problem easily solved just by thinking, and even if that fails you can always search.
Well, you’re going to have to implement velocity, accelaration, deceleration, bounce, etc etc.
I’ve already got a full system implemented. The ball moves, bounces, collides, all that. I just don’t like the way it moves. I’ve been racking my brain over this for a few days now and the only thing i’ve been able to come up with is detecting the degree of the angle before it hits a wall and compensating…I just haven’t been able to figure out how to put this into code yet.
Believe me, i’ve been searching and thinking about this. I try not to ask anything here until i’ve fully researched it…Maybe i’m just researching it wrong, but like I said, I couldn’t find what I was looking for. His solution above, doesn’t even have anything to do with what i’m asking…But thank you.
Yeah, i’m working on the velocity/deceleration already. That isn’t too much of a problem. Right now, the ball moves by -1 or 1 on the X and Y axis. That’s the problem.
You’re awesome. That link was what I was looking for. Thank you.
An arbitrary bounce is a reflection about the normal of the surface. The parallel part is negated and the orthogonal part remains the same (assuming idealized interaction…no friction, idealized surfaces, etc).
Old article I wrote:
http://petesqbsite.com/sections/express/issue21/index.html#bounce
There’s also a link there for an even older Ball to ball collision and response.
I stuck a mini write-up at the bottom of this: http://www.java-gaming.org/index.php/topic,28991.msg264958.html
[quote=“Bassex96,post:6,topic:41602”]
If you´ve come to the point that you want realistic movement it´s time to start learning vector math. If you used vectors you could get what is called “reflection vector” which will give you a more realistic behaviour. Here´s a link to a nice tutorial (not java code but a pretty nice description of the idea):
http://www.wildbunny.co.uk/blog/2011/04/06/physics-engines-for-dummies/
It requires that you know your way around vectors though…
You guys are great. I’ve been pouring over the article Jimmt posted and it’s great but a little overwhelming. I’m going to check all of these resources out and get it implemented. I can’t wait to finish this game so I can start on something else. I’m going to change the levels to use Tiled so I can get some experience for my 2d tiled game. I think i’m going to also add some lights using box2dlights. That should be about it. It’s about finished. Everything else works great.