Programming 2D Games

The official forum for "Programming 2D Games" the book by: Charles Kelly

It is currently Mon Aug 21, 2017 7:32 pm

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Fri May 26, 2017 8:53 pm 
Offline

Joined: Fri May 26, 2017 8:29 pm
Posts: 5
Hello. I have a question on this method for detecting collisions discussed in chapter 6. I'm currently studying it and trying to understand it properly. My question is, is Rotated Box Collision Detection also the method known as Separating Axis Theorem? I'm trying to read up on anything that can help me understand this collision detection method intuitively and I would appreciate any help you can offer.


Top
 Profile  
Reply with quote  
PostPosted: Sat May 27, 2017 1:10 pm 
Offline
Site Admin
User avatar

Joined: Sat Jan 28, 2012 4:36 pm
Posts: 536
Rotated Box collision detection uses the Separating Axis test. To keep things a little easier to visualize, I hope, I restrict the polygons to rectangles.

Chuck

_________________
Professor Kelly


Top
 Profile  
Reply with quote  
PostPosted: Sat May 27, 2017 8:17 pm 
Offline

Joined: Fri May 26, 2017 8:29 pm
Posts: 5
Thank you very much for your prompt reply professor. I will go over Rotated Box Collision Detection method again with this understanding and see if I will now understand it well. Though I think the reason I am struggling with this is because I don't intuitively get vectors and their projection. My problem is that I'm not seeing the polygon edges but points, although the data type is VECTOR2. I don't know if I'm explaining myself well, but basically I'm having a hard time visualizing the bounding box edges and thus their projection onto the projection lines, and so seeing for myself that the method is sound and works. For example, on page 153 of the book, we have a code snippet of the computeRotatedBox function (Listing 6.7(a)). The caption under this snippet is "The computeRotatedBox function calculates the corners of the rotated box". This is confusing to me. Is it computing corners (points) or edges of the rotated box? The function begins by first creating an x and y vector from the current entity angle (Listing 6.7(b). The way these vectors are created, is this using the 2D rotation formula? Because I don't understand how vector rotatedY is being calculated. (VECTOR2 rotatedY(-sin(spriteData.angle), cos(spriteData.angle));) Shouldn't it instead read VECTOR2 rotatedX(cos(spriteData.angle), -sin(spriteData.angle)); and VECTOR2 rotatedY(sin(spriteData.angle), cos(spriteData.angle)); What are these vectors accomplishing? Please help me understand this function and what it is doing especially Listing 6.7(c). Thank you in advance.


Top
 Profile  
Reply with quote  
PostPosted: Mon May 29, 2017 2:09 pm 
Offline
Site Admin
User avatar

Joined: Sat Jan 28, 2012 4:36 pm
Posts: 536
Thanks for the question. It reminded me that I need to do a better job of explaining the math in my next book.

Keep in mind that the Y axis in Windows graphics is inverted. Positive Y is down and negative Y is up.
A 2D vector is simply a point in 2D space (x,y).

rotatedX and rotatedY represent the X and Y axis of the entity in model space translated into world space where positive Y is down.
The following diagram shows the rotatedX and rotatedY vectors obtained from angles of 0, 90, 180 and 270 degrees.
Code:
                  rotatedX( 0, 1)
                  rotatedY(-1, 0)

                         90
                          |
                          |
 rotatedX(-1, 0)  180 --------- 0  rotatedX( 1, 0)
 rotatedY( 0,-1)          |        rotatedY( 0, 1)
                          |
                         270

                   rotatedX( 0,-1)
                   rotatedY( 1, 0)

The code in Listing 6.7(c) is calculating the screen location of the corners of the rotated box by multiplying the X,Y coordinates of each corner by rotatedX and rotatedY and then adding it to the center(X,Y) of the entity.

_________________
Professor Kelly


Top
 Profile  
Reply with quote  
PostPosted: Mon May 29, 2017 11:51 pm 
Offline

Joined: Fri May 26, 2017 8:29 pm
Posts: 5
Thank you very much for your reply Professor Kelly. I think it is very cool to be able to ask you questions on the material in the book. I have understood your explanation up to 2D vector is a point in space. Now I find myself not understanding how the rotatedY values were found in your diagram. I think I understand how the rotatedX values were found from reading the article at http://chortle.ccsu.edu/vectorlessons/v ... h07_5.html. Am I right that this is how rotatedX values are found? You explain that rotatedX and rotatedY represent the X and Y axis of the entity in model space translated into world space. Do you mean that rotatedX and rotatedY are the X and Y coordinates on the axis of the entity in model space translated into world space? A thorough explanation of this translation from model space to world space with the entity axis will really help. Diagrams will go a long way too if you may. Thank you.


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 07, 2017 4:45 pm 
Offline

Joined: Fri May 26, 2017 8:29 pm
Posts: 5
Hello Professor Kelly. I understood how those rotatedX and Y values are calculated. All this time the formula is in Listing 6.7(b) of the book. It was a misunderstanding on my part but I have got it thanks to your previous explanation.


Top
 Profile  
Reply with quote  
PostPosted: Sun Jun 11, 2017 5:15 pm 
Offline
Site Admin
User avatar

Joined: Sat Jan 28, 2012 4:36 pm
Posts: 536
Attachment:
Rotated Box Collision.png


You do not have the required permissions to view the files attached to this post.

_________________
Professor Kelly


Top
 Profile  
Reply with quote  
PostPosted: Fri Jul 28, 2017 8:46 am 
Offline

Joined: Fri May 26, 2017 8:29 pm
Posts: 5
Thank you so much Professor Kelly for that illustrated example. It has really helped me to understand the code better.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group