# 2 ploygons colliding

• 12-22-2011, 06:38 PM
osiris_z0r
2 ploygons colliding
Hi there!

I'm recently new to java and i have two questions for u guys.

I'm creating a game in which we have one polygon (our 'ship') and two monsters. Both of the monsters and the ship are triangles like my code show down below
Code:

```    void buildmonster(){         m_monster = new Polygon();         m_monster.addPoint(10,0);         m_monster.addPoint(-10,10);         m_monster.addPoint(-10,-10);         m_monster.addPoint(10,0);        /* fecha o ciclo */         m_monster_angle = 0;         spawnarmonster();           m_monster_speed = 0;           /**     * constroi o norris     */     void buildnorris(){         m_norris = new Polygon();         m_norris.addPoint(10,0);         m_norris.addPoint(-5,5);         m_norris.addPoint(-5,-5);         m_norris.addPoint(10,0);                m_norris_angle = 0;         spawnarnorris();               m_norris_speed = 0;           m_norris_max_forward_speed = param_norris_max_forward_speed;         m_norris_max_back_speed = param_norris_max_back_speed;         m_norris_acceleration_speed =param_norris_acceleration_speed;         m_norris_turn_speed = param_norris_turn_speed;     }```
First i would like to ask how can i put the monsters moving randomly through the screen (There are no borders, example, if one monster hits the top it appears on the bot going top).
Last i would like to know what's the way to detect the collision between the ship and the monster and
• if the ship hits the monster back, destroys them
• if not it destroys the ship (in my game loses 1 up)

Thanks
• 12-22-2011, 06:46 PM
christopherx
Re: 2 polygons colliding
... Riiiight. Well it seems to be that you'll need to be comparing co-ordinates. If you thread this, and have one thread analysing the co-ordinates of the different objects, or for every movement, you can have each object announce it's co-ordinates and if there are any similarities then a collision is detected. If you want to know which side, then you'll have to split the object up into different sections that are governed differently. you could say, if the ship is hit by a monster then boolean shipHit = true, and if at one point, their co-ordinates are different, then at another point, within a time frame, the co-ordinates are the same or similar, then shipHitBack = true. Then at the end of all of it, a little if statement calling the "destroy()" method. Hope this helps.
• 12-22-2011, 07:21 PM
osiris_z0r
Re: 2 ploygons colliding
Any about the monster random movement? I've my ship moving with this code
Code:

```void handlenorrisMotion(){         double sin = -Math.sin(m_norris_angle);         double cos = Math.cos(m_norris_angle);             /* move x */         m_norris_x += cos * m_norris_speed;             /* wrap x  */              if(m_norris_x > m_width)             m_norris_x = 0;         if(m_norris_x < 0)             m_norris_x = m_width;             /* move y */         m_norris_y += sin * m_norris_speed;             /* wrap y  */         if(m_norris_y > m_height)             m_norris_y = 0;         if(m_norris_y < 0)             m_norris_y = m_height;             }```
• 12-22-2011, 10:15 PM
pbrockway2
Re: 2 ploygons colliding
Quote:

what's the way to detect the collision between the ship and the monster
Perhaps one of the contains() methods could help. More reliable would be to create Area instances from them. Area has intersect() and isEmpty() methods.

Or, (and this could be faster) Line2D has a intersectsLine() method which tests for line segment intersection. Two triangles intersect iff no pair of sides intersect and no vertex of one is within the other. (I think)

Quote:

how can i put the monsters moving randomly through the screen
Paint them onto the screen with the appropriate Graphics2D method.

Define "randomly". Most likely you want their movement to be continuous, so they should have a velocity which lets you determine their position from one instant to the next. There is rather more freedom with respect to how their velocity changes. Keep it simple to start with, you can always tweak it afterwards if it doesn't look realistic. As regards the "no borders" business (it sounds impressive to describe it as a "toroidal topology": in effect the world is shaped like a torus), the % operator is your friend.
• 12-23-2011, 12:26 AM
Re: 2 ploygons colliding
Doing efficient collision detection also depends on the number of objects. If you only ever have 20 - 50 or so objects on the screen which you are checking, then a simple brute force might work just fine. However, if you intend to vomit particles all over the place like some of the old school shoot 'em ups, you'll need something better than a bunch of nested for-loops. Quad trees are often used for this, but this is a more advanced topic. I advise you keep your particle count low while you're learning this.
• 12-26-2011, 05:50 PM
osiris_z0r
Re: 2 ploygons colliding
Hm and how do i create Separate areas from a triangle?
I need to create 2 separate cases
• If my ship hits the monster back it destroys the ship
• else it turns otherway

And i thought i could create 2 areas, one the side that causes destruction , the other its the rest of the triangle.

Can u sample me some code for this?
• 12-27-2011, 03:19 AM
pbrockway2
Re: 2 ploygons colliding
Quote:

Hm and how do i create Separate areas from a triangle?
Area has a constructor that takes any shape (including a polygon). You can test two polygons for intersection by forming their intersection and checking to see if it is empty.

Code:

```Polygon monster; Polygon norris; // later Area mArea = new Area(monster); Area nArea = new Area(norris); boolean hit = mArea.intersect(nArea).isEmpty();```
• 12-27-2011, 11:24 PM
osiris_z0r
Re: 2 ploygons colliding
Its something like that, but i want to check a collision from behind the monster and that includes all points