-1

I'm using awt and it's been working great. However, I need to detect the side that one object hits another. For a rectangle, it'd be a simple math, however for another shape (let's say text), it's not as simple.

For example, I have a 'player' (a rectangle shape) which moves with the arrow keys, and a text with a collision box matching the text (a shape matching the text).

If the player went in between letters, then collided into the right side of a letter, I would want it to be distinguished as right, not left (like the rectangle example would most likely would be).

I already have the collision detection done, that's not the issue. I want to detect the face (left, right, top, bottom) where the collision happened.

In other words, if someone made a custom shape, with two rectangles inside of it, but the center is blank between them, I want to detect which face the collision happened. So, if I have the left rectangle on the right side, I want to know that I hit the right side, and if I hit the right rectangle on the left side, I want to know that I hit the left side.

Is this possible? And if so, is it practical?

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
My Name
  • 19
  • 3
  • To answer, information like how is the player and text defined is essential. Please post [mcve] demonstrating what you want to achieve, and what makes this question different from your [previous](https://stackoverflow.com/questions/52748408/checking-if-a-rectangle-intersects-text) one. – c0der Oct 21 '18 at 04:06
  • @c0der I updated the player part, however the text part is irrelevant. It was an example, I want to do this with any shape, and I thought maybe I could use the same functions that awt uses to draw the shapes. And my previous question has nothing to do with this? – My Name Oct 21 '18 at 09:12
  • " I want to do this with any shape" does it mean that all text is made of [Shape](https://docs.oracle.com/javase/7/docs/api/java/awt/Shape.html) objects ? – c0der Oct 21 '18 at 09:31
  • Alright, so it's clear I'm not getting an answer here. This isn't a duplicate of the flagged question, but I have an idea of what to do by myself. – My Name Oct 21 '18 at 10:21
  • DIY is always the best. I marked the question as too broad. If you expect answers please give all the relevant information. You may want to take a [tour](https://stackoverflow.com/tour) and see [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – c0der Oct 21 '18 at 10:44

1 Answers1

0

I recommend using a hitbox to first detect if the player is within a rectangular area surrounding the letter, and if he is - then do detailed collision checks.

You'll have to create a special set of shape objects for each letter to match it's shape, where each shape is a simple convex shape that you can do easy checks on it.

potato
  • 995
  • 11
  • 19