• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
P0jahn

Rotated rectangle vs circle collision

7 posts in this topic

I am trying to translate the code found here:

http://www.migapro.com/circle-and-rotated-rectangle-collision-detection/

The collision detection do not work as intended.

 

Here is my code:

public static boolean rotatedRectangleVsCircle(GameObject rect, GameObject circle)
{
  double rectCx = rect.currX + rect.width / 2, //Rectangle center x
         rectCy = rect.currY + rect.height / 2, //Rectangle center y
         ccX = circle.currX + circle.width / 2, //Circle center x
         ccY = circle.currY + circle.height/ 2, //Circle center y
         rot = Math.toRadians(rect.rotation),

        cx = Math.cos(rot) * (ccX - rectCx) - Math.sin(rot) * (ccY - rectCy) + rectCx,
        cy = Math.sin(rot) * (ccX - rectCx) + Math.cos(rot) * (ccY - rectCy) + rectCy,
  
        x,y; 

if (cx < rect.currX)
   x = rect.currX;
else if (cx > rect.currX + rect.width)
   x = rect.currX + rect.width;
else
   x = cx;

if (cy < rect.currY)
   y = rect.currY;
else if (cy > rect.currY + rect.height)
   y = rect.currY + rect.height;
else
   y = cy;

if (findDistance(cx, cy, x, y) < circle.width / 2)
   return true;
else
   return false;
}

Point currX, currY represent the top-left corner of the sprite.

The sprite image looks like this at the start:

[attachment=14670:rectangle.png]

 

Finally, the rotation uses the start value 0 and increases with 1 every frame, rotating around its center point.

 

Why is it not working?

Edited by P0jahn
0

Share this post


Link to post
Share on other sites

Hey there.

 

I think it's just a typo, but you are missing the type declaration at

 

 

x, y;
 

 

Have you tried taking logs outputting the values? See if the outputs correspond to the graphic representation or if they look correct in the scenes behind.Try to implement piece by piece making sure they work at lower level.

 

How are you graphically rotating them?

 

What actually works and what doesn't?

 

This is all I can say right now because I'm gonna be out all day today.

0

Share this post


Link to post
Share on other sites

Hello

 

I think it's just a typo, but you are missing the type declaration at

 

Actually, it is not a typo, it compiles just fine.

 

How are you graphically rotating them?


I am using Slick2D and I use image.rotate(float degree). Not exactly sure how the actual rotation is done.

 

The collision detection is simply inconsistent. It does not detect properly.

 

I increase the rectangles rotation variable with 1 every frame, rotating it clockwise. This differ from yours, which rotates counter-clockwise. 

I tried decreasing my rotation variable with 1 every frame instead, making it rotate ccw, no luck.

 

Also wanted to note that rect.currX and rect.currY points to the unrotated top-left corner of the rectangle.

Edited by P0jahn
0

Share this post


Link to post
Share on other sites

Ah I missed those commanssmile.png

 

I've never used Slick2D which should not be a problem here at all. You just match the way it rotates.

 

Does it work with 0 degrees? Did you try outputting the values? If so, how are they acting? If I'm debugging it, I would start from there to find out what is not acting properly.

 

I can't tell much from the given piece of code. If you can show me a code that I can actually run and test, I could help more.

 

You do understand the idea, right?

Edited by miga
0

Share this post


Link to post
Share on other sites

It seems to work fine when the rotation is 0.

Check the attached file, everything is logged in a text area. You can pause the game with escape.

[attachment=14740:Game Test.rar]

Edited by P0jahn
0

Share this post


Link to post
Share on other sites

I can't open your game. It crashes as soon as it tries to create its window.

0

Share this post


Link to post
Share on other sites

Is the "res" folder in the same directory as game.exe?

And I assume you are using windows with java7 installed.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0