• Post Reply Bookmark Topic Watch Topic
  • New Topic

Ball Stopping at Edge of Maze  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone

My name is Rob and this is my first post on the forum.
Until the start of this year I have never done any programming before except mark up languages like HTML and CSS.
I am a mature student gone back to Uni trying a different path from my previous career which was not in IT or programming.
Worth mentioning my background as my programming problems may seem extremely simple to you to solve.
Anyway I am doing an assignment which involves creating a maze and a ball that needs to go through the maze to an end tile then stop the scenario.
The maze is made up of 208 buttons in a Jbutton Grid layout. I am replacing certain buttons with a brown tile icon to make up the maze. The ball needs to run along those brown tiles as its path and not be able to move off the brown tile icon images.
I have built the maze and have got quite far with this but now I am stumped on the concept of keeping the ball within the boundary edges and on the brown tiles.
I have been told to use if statements, but not had the process explained to me in a way I can understand.
I would much appreciate it if anyone here would be willing to explain what direction I need to take and the logic behind the method. Been trying for days to figure it out on my own but now stumped.

Thanks in advance

Rob
 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post your code (or a subset if it's large)? Tell us where in the code that you're stuck.

P.S. Don't forget to use code tags when posting code.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
…And welcome to the Ranch
 
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Nowlan wrote: I am stumped on the concept of keeping the ball within the boundary edges and on the brown tiles.
I have been told to use if statements, but not had the process explained to me in a way I can understand.


It is a collision problem. Assume the tiles are squares. You maintain somewhere the center of each square and you can calculate it's breadth and height and therefore where its edges are, from that. Assume the ball is round. You maintain a center point for that and from its radius you know its edges from that. Chances are in the assignment you'll only need upper edge of the ball = center - yradius , right edge of the ball = center + xradius, and so on for all the directions you need. You have the centers of all the tiles in a list somewhere. Then you solve the collision problem which is determining if the ball contacts any of the tile edges. for that you'll use >= and <= for example instead of == so you can be off by a pixel or several.

The if statements he's talking about are just stuff like for example

if (ballcenter-ballradius <= square[i]+sidelen*.5)
stopMoving();
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!