Jump to content


Photo

4-way version of breakout help needed


  • Please log in to reply
3 replies to this topic

#1 northcuttjj

northcuttjj

    GMC Member

  • New Member
  • 3 posts
  • Version:GM8

Posted 11 May 2012 - 05:40 PM

Ok so I'm working on a 4 paddle version of breakout, Basically I have a paddle at the very bottom of the screen, one on the left, on the right, and one on top of the screen.

My problem is trying to get the ball to bounce relative to it's position on the paddle EX: If it hits the left edge of the paddle I want it to ge left.

I don't understand coding that much, however I was able to find someone with a similar problem on another forum: http://gmc.yoyogames...howtopic=538413

I found this piece of code there: direction = (obj_paddle.x - obj_ball.x) +90.

This code solved the problem for my paddle on the bottom of the screen, But my paddles on the right, left, & upper part of the screen still don't have the ball bounce the right way.

Any help would be greatly appreciated.
  • 0

#2 Locust

Locust

    GMC Member

  • GMC Member
  • 42 posts
  • Version:GM8

Posted 11 May 2012 - 07:02 PM

If they're done the same way you did the bottom paddle:

Top paddle:
direction = x-other.x + 270;
Left paddle:
direction = other.y-y;
Right paddle:
direction = y-other.y + 180;
It's not the best way to do it because the angle is just based on how wide the paddle is, so you can't control how much it will bounce. And it won't even work if the paddle is too wide.

A better way:

Bottom paddle:
direction = 80*(other.x-x)/(other.sprite_width/2) + 90;
Top paddle:
direction = 80*(x-other.x)/(other.sprite_width/2) + 270;
Left paddle:
direction = 80*(other.y-y)/(other.sprite_height/2);
Right paddle:
direction = 80*(y-other.y)/(other.sprite_height/2) + 180;
This way it takes the width/height of the paddle into account and you can specify a maximum angle the ball can bounce to, I used 80 in the example.
I put the code in the Collision events of the ball.

Edited by Locust, 11 May 2012 - 07:15 PM.

  • 0

#3 troido

troido

    GMC Member

  • GMC Member
  • 150 posts
  • Version:GM8

Posted 11 May 2012 - 07:12 PM

For the top, you should simply take the negative, like this:
direction = -(obj_paddle.x - obj_ball.x) -90;
For the left and right, you should take the y position instead of the x position.
Your code for the right side would look like this:
direction = (obj_paddle.y - obj_ball.y) +180;
the left side would be the negative of this.

EDIT:
didn't see Locust's reply. I think he's correct too.

Edited by troido, 11 May 2012 - 07:15 PM.

  • 0

#4 northcuttjj

northcuttjj

    GMC Member

  • New Member
  • 3 posts
  • Version:GM8

Posted 15 May 2012 - 06:20 PM

Hey sorry for the late response, everything that Locust and troido said worked so thanks you guys.

The game works beautifully now
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users