Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Correct direction of bounce?

Author
Message
Fluorescent
FPSC Reloaded TGC Backer
19
Years of Service
User Offline
Joined: 1st Aug 2005
Location: Stockholm, Sweden
Posted: 31st May 2015 12:26
I've decided to revisit a small project I did some time ago, a Breakout\Arkanoid-clone.

As long as the ball hits the brick from underneath just negating the y-axis seems to work to create a convincing bounce. But if the ball as bounced of the wall and strikes the brick from the side negating the y axis isn't the way to go I think, I need to negate the x-axis also. I think.

How can I correctly detect the direction the ball is traveling versus the position of the brick it hits?
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 1st Jun 2015 19:18 Edited at: 1st Jun 2015 19:19
if u hit left/right its x=-x speed
if u hit top/bottom its y=-y speed
but use only once.

alternate try using physic system.

AGK 108 (B)19 + AppGameKit V2 Beta .. : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 1st Jun 2015 19:45
Just set them up with physics as Markus suggests, disable any gravity and take away any friction and you are set.

Unless you want to learn the math, then doing it yourself is a good idea.

Fluorescent
FPSC Reloaded TGC Backer
19
Years of Service
User Offline
Joined: 1st Aug 2005
Location: Stockholm, Sweden
Posted: 2nd Jun 2015 16:54
Quote: "
Unless you want to learn the math, then doing it yourself is a good idea."

Yes, that is a big part of my little project. To learn more about code and implementing these types of algorithms.

Quote: "if u hit left/right its x=-x speed
if u hit top/bottom its y=-y speed
but use only once."

I've figured that out. It's the part about finding the direction that the ball is actually traveling that I'm having problems with.
Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 2nd Jun 2015 17:12
You need to know whether the ball hit the top/bottom or the left/right of the brick. Not sure how you are detecting collisions so can't really tell you what the next step is without more info.

If you have 'normal' info from the collision then you will know whether you hit the top/bottom/left/right.

Fluorescent
FPSC Reloaded TGC Backer
19
Years of Service
User Offline
Joined: 1st Aug 2005
Location: Stockholm, Sweden
Posted: 3rd Jun 2015 17:35
I've stored all the brick sprite id's in an array. Then for every iteration in my game loop I loop through the array and test a collision with GetSpriteCollision(ball, brick) = 1. Not the most efficient way perhaps, but there are never that many bricks after all.

What's 'normal' info from a collision by the way?
Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 3rd Jun 2015 17:56
The 'normal' is the direction the surface is facing. For instance the bottom of the brick has a normal of 0,-1(facing down).

After thinking again you may not need normal data if all your blocks are rectangular. When you detect a collision you can check yourself which side(face) has been hit.

If the collision is underneath the block, the bottom, then the ball y position will be less than the y position of the brick. Also the ball x position will be no more than half the brick width plus half the ball width away in either direction(left/right). This all presumes the sprites are positioned using the centre of the actual image.

If you get a collision and the x position of the ball IS more than half the width of the brick plus half the width of the ball away, then you know it's a side collision.

Hope that helps.

Here is a great youtube channel which explains how to do all this stuff from pure math:

Math for Game Developers

Fluorescent
FPSC Reloaded TGC Backer
19
Years of Service
User Offline
Joined: 1st Aug 2005
Location: Stockholm, Sweden
Posted: 3rd Jun 2015 18:39
You know what. That is a ridiculously simple solution. And that is probably why I didn't think of it

Thanks!

Login to post a reply

Server time is: 2024-11-16 18:08:49
Your offset time is: 2024-11-16 18:08:49