-
Sep 28th, 2012, 10:17 AM
#1
Thread Starter
Member
Platform game help
I'm making a simple platform game using shapes, and I have a question about grouping objects. When the player bounds intersects with the grounds bounds, it stops at the ground. However, so far I only have one rectangle as my ground, and when I add more I don't want to write a collision code for every single rectangle. Is something somewhat similar to this possible to do? :
Code:
dim GroundRectangles as new objectgroup
GroundRectangles.Add(ground1, ground2, ground3, ground4)
If player.bounds.intersectswith(GroundRectangles.member.bounds) then 'do stuff
Note: the rectangles are made in the design view, not using a paint event.
-
Sep 28th, 2012, 12:08 PM
#2
Re: Platform game help
I think there will come a point, when you might realize, that creating a ground from rectangles and looping through them all for collision-detecting, will be too slow.
If you instead uses lines and/or arcs to define the edges of platforms/ground and have bounding boxes for each, collision-detecting might be faster.
For instance:
Code:
[ ___ ] (Platform bounding box)
[/ \]
[\___/] [ o ]
[ \|/ ] (Player bounding Box)
[ / \ ]
(Ground A Bounding Box)
[ ___ ] (Ground B Bounding box)
[__/ \ ] [ __ ]
[ \ ] [ __ / \ ]
[ \_ ] [ / \/ \____ _ ]
[ \_] [___/ \__/ \]
[----](Hole bounding box)
(Note: Bounding boxes can ofc be replaced/combined with any bounding area that has fast collision-detection, such as ellipses for instances.)
Only if the bounding-area of a player intersects the bounding-area of an object, will the object need to be examined further. That will most likely involve intersecting a bounding-area with a path or similar (You could even use rectangles, if the ground/platforms are made up of tiles).
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Sep 29th, 2012, 03:49 PM
#3
Thread Starter
Member
Re: Platform game help
Thank you for the answer, can you explain more about how bounding boxes works and how you create them?
-
Sep 30th, 2012, 02:49 AM
#4
Re: Platform game help
Well to stay within a terminology, that you're most likely familiar with: Assuming you would like to make a piece of the screen look like this (simply pictureboxes in a standard form used to illustrate tiles on a map):
If you were to test a player-collision against this piece of ground, you would have to loop through 24 rectangles (several of which the player can never reach). If you instead placed all 8x8 picture-boxes within a panel called bounding box, you could just check the borders of the panel, to see if the player is wihin the area of this piece of ground. And only if the player was inside the panel, would you ever have to loop through all the picture-boxes.
You could also add 4 more panels inside the bounding-box panel. Each of these would contain a 4x4 area of picture-boxes. The top-right of these smaller panels would contain nothing, so if the player is contained in this panel, there is no need to check any further. This process can be continued - each time the player is within a panel, you check to see, if he is in the left or right side and top or bottom half - until an empty or full panel is reached. To illustrate:
Typically you will not use panels/pictureboxes though. Instead you should create your own class, which will be a tree with 4 branches at each inner node and the leaves being tiles. Keeping all tiles on the screen in a nice structured way within bounding-boxes, that is contained in larger bounding-boxes, etc. will allow for rapid determination of player vs. background collisions.
There are even faster/more clever ways though, but they may be a tad on the complicated side. For instance an 8x8 tile area could be described simply by a 64-bit number where each bit denotes a tile in the area. The top-left quarter could be described by the first 16 bits of the number and so on. Then determining if a player hits a tile will be short section of comparisions and logical operations.
If you want to know more on how to implement datastructures etc. feel free to ask here or do a search on quad-trees (which is what ordering a square into 4 equally sized squares and continuing to a certain depth, used to describe shapes in 2d-space is called (octrees in 3d-space)).
Regards Tom
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|