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.

Newcomers DBPro Corner / Advanced? collision detection

Author
Message
sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 30th May 2011 13:04 Edited at: 30th May 2011 18:05
Hi all,

I am making a break out clone and I have a question about collision detection.

I want to make the ball bounce at a slightly different angle depending on where it hits the bat sprite.
If it hits near the edge of the bat I want it to bounce more horizontally and if it hits near the middle of the bat I want it to bounce more vertically.

the bat is at the bottom of the screen and the ball drops from the middle of the screen in the first instance.

the only collision detection experience I have is in a pong game I made but that was a much simpler system which when 1 sprite hit another the y angle was reversed whilst the x angle was maintained giving a predictable bounce effect.

This worked ok for a simple project but I don't think I can really apply it to this project.

If anyone can point me in the right direction I would really appreciate it.

Regards,

Paul.

[edit]I should point out that the bat the ball and the blocks i will be destroying are all sprites.[/edit]
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 30th May 2011 19:00
Quote: "This worked ok for a simple project but I don't think I can really apply it to this project."


Why not? It's basically the same as a pong game just with blocks to destroy. If it doesn't work as well as you want you can always change that part of the program later.

sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 30th May 2011 19:13
Quote: "Why not? It's basically the same as a pong game just with blocks to destroy. If it doesn't work as well as you want you can always change that part of the program later."


In the pong game you get a very very predictable pattern of bounce.
I have tried to add some variety to the bouncing pattern by varying the x and y angle of the ball randomly when the ball comes off a surface.

It works to some extent but it's doesn't achieve what I want to do here.

I don't want to run before I can walk but I'm making these simple games to learn before I try to make a game I actually want to make.

I want the ball to react as if the bat had a curved front edge for want of a better description.

but if the ball hit here:

______x__
| |

I want it to bounce at the same angle every time and I want it to bounce at a different angle if it hit here for instance.

___x_____
| |

I think it will make the game more enjoyable to play (if I can implement all of the features I plan to over time), and it will hopefully provide me with information I can use in other projects.

Thanks,

Paul.
SH4773R
14
Years of Service
User Offline
Joined: 18th Jan 2010
Location: AMERICA!!!
Posted: 31st May 2011 06:04
make the paddle out of three sprites position them together, check each one of collision if the ball hits deflect in logical direction for that segment.


My software never has bugs, it just develops random features.
sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 31st May 2011 10:59
Thanks, that's a great idea. I'll give it a go
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 31st May 2011 13:44
Or you could just get the offset of the ball from the centre of the bat and just calculate your angle based on that distance...

[eg]
Angle#=90 + ((ballX# - batX#)/(batsize# * 0.5)) * 60.0
[/eg]

That should give you an angle in a 120 degree arc depending on the distance from the centre of the bat, but it's just a quick attempt. How you use the angle you get will depend on your game engine...

sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 31st May 2011 18:46 Edited at: 1st Jun 2011 02:07
Thanks for the suggestions.

@baxslash That sounds like a great idea but...
How do I make my sprite move according to an angle?

at the moment my code looks like this....



As you can see the 'angle' of the ball is currently determined
by the x and y speed.

Would I be able to apply my new angle to this or would I be better off re writing the code?

Sorry to be asking such basic questions.
I don't want someone to write the code for me, if you could just point me in the right direction I'm sure I will learn more by figuring the rest of it out.

Thanks again,

Paul.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st Jun 2011 12:42
Try replacing the code under your collision detection with this:

It just uses some simple math to work out the direction based on the angle. It's essentially the same equeation but simplified because you are using a speed of 1 unit anyway. If you want to vary the speed it'll look more like this:


sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 1st Jun 2011 19:49
Quote: "rem collision detection for the bat and ball

if sprite hit (2,1)
dx#=sprite x(2) - sprite x(1)
nx#=dx#/25
a#=acos(nx#)
ny#=-sin(a#)
endif"


That's fantastic thank you very much for taking the time to help!!!

I have one more question for you. Would you mind explaining what the code is doing so I can tweak it and get the exact effect I am after?

Thanks again,

Paul.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 3rd Jun 2011 11:02 Edited at: 3rd Jun 2011 11:02
Sure:

1.
"if sprite hit(2,1)" detects the collision.

2.
"dx#=sprite x(2) - sprite x(1)" determines the distance between the two sprites.

3.
"nx#=dx#/25" gives us a reflection value in the X direction between 1.0 and 0.0 based on that distance (your bat is 50 wide) and sets the ball moving by that value.

4.
"a#=acos(nx#)" a# is the angle of the collision worked out using the sin/ cosine / tangent rules from standard trigonometry.

5.
"ny#=-sin(a#)" gives us a reflection value between 1.0 and 0.0 in the Y direction based on the angle of the impact.

This is a fairly quick method for getting a value and only one way of doing it. You could estimate it in a number of ways really.

I hope that gives you some idea but for future reference it's worth knowing how SIN, COS and TAN as well as ASIN, ACOS and ATAN work so here's a quick reference:
http://en.wikipedia.org/wiki/Law_of_sines

I prefer to remember the basic rule using the SOH/CAH /TOA where Sin(angle)=Opposite/Hyp, Cos(angle)=Adjacent/Hyp and Tan(angle)=Opposite/Adjacent


Attachments

Login to view attachments
sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 5th Jun 2011 16:10
Thank you very much. Beautifully explained

I'll look into sin,cos and tan.

Login to post a reply

Server time is: 2024-11-22 22:11:19
Your offset time is: 2024-11-22 22:11:19