Public Group

# warlords

This topic is 4465 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am programming an warlords clone using openGL and c++.I have drawn the ball and the paddles and the castles,I need an algorithm for collision detection of when the ball hits the castle walls, and the paddles, I am trying to avoid alot of complex math and physics,here is my collision detection so far: //check for collision detection if (x+20.0 > i && x < i+80.0f && y < 40.0f) { ystep=-ystep; } if (x+20.0 > i && x < i+80.0f && y > 580.0f) { ystep=-ystep; } this checks to see if the ball hits the paddle

##### Share on other sites
More compactly,
if (x + 20.0f > i && x < i + 80.0f)   if (y < 40.0f or y > 580.0f)      ystep -= ystep;

OT, it's typically a good idea to use consts instead of literal values. Doing so will make your code much more flexible if the dimensions of your screen, castle, or paddle changes in the future.

##### Share on other sites
I need an algorithm for when the ball hits a castle wall

##### Share on other sites
you posted this same question a few days earlier (http://www.gamedev.net/community/forums/topic.asp?topic_id=389421), there's really no need to double-post.

I'm not too familiar with the game, but as I mentioned in the earlier thread there's a pretty simply way to test for collision between a circle and a wall:

|
| (C)
|
|
|
o-->N
P

Given a circle with center C, radius R, and a wall originating at point P with a (normalized) outward (I said inward last time, which was a mistake, sorry) normal N:

vector v = C - P;
projection of v onto n = (v dotproduct N) * N;
if (magnitude(projection) <= R)
//collision occured

Hope it helps.

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631759
• Total Posts
3002138
×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!