Forum Thread
by member
  Posts  
Java Rectangle Collision (Forums : Coding & Scripting : Java Rectangle Collision) Locked
Thread Options
May 11 2013, 6:50pm Anchor

I'm making an RTS game and I need it so when two units collide to stop moving towards the collision but still move towards the point that you initially click to send them. The hitboxes of these two units are rectangle objects. The solution also needs to be applicable to any number of units, not just two. So if you didn't understand that, let me phrase it in a question. How do you get two units that have hitboxes of rectangles to stop moving in the direction they collide when they collide, but still move towards the point set initially?

iQew
iQew Concept Art
May 13 2013, 7:41am Anchor

Well, what's really the problem here? I think you should define it more precisely. Do you have problems in getting to the core problem or do you have problems with the implementation and if yes, in what parts?

What do you want the units to do, if the collision appears? Evade the block to the right side or to the left side? Should the moving unit use the fastest route or the shortest one? What cases can appear and what should happen then? If you have answered those questions, you can go ahead and implement it.

I'm a beginner myself, but I think your problem can be solved quite easily. Before each move of a unit, the program should test, if there would appear a collision between two or more units. If you've achieved to implement that, you can easily say what happens next. The moving unit stops moving towards the direction where the collision appeared (a rectangle only has a top, bottom and two sides, so the collision is always clearly defined) and moves either to the right/left/top/down side until there's no more collision. If there is no more collision, the unit is then allowed to move into that direction again.

Solve the problem on paper first before you implement it. I hope this was of any help.

Edited by: iQew

May 13 2013, 10:09pm Anchor
iQew wrote:Well, what's really the problem here? I think you should define it more precisely. Do you have problems in getting to the core problem or do you have problems with the implementation and if yes, in what parts?

What do you want the units to do, if the collision appears? Evade the block to the right side or to the left side? Should the moving unit use the fastest route or the shortest one? What cases can appear and what should happen then? If you have answered those questions, you can go ahead and implement it.

I'm a beginner myself, but I think your problem can be solved quite easily. Before each move of a unit, the program should test, if there would appear a collision between two or more units. If you've achieved to implement that, you can easily say what happens next. The moving unit stops moving towards the direction where the collision appeared (a rectangle only has a top, bottom and two sides, so the collision is always clearly defined) and moves either to the right/left/top/down side until there's no more collision. If there is no more collision, the unit is then allowed to move into that direction again.

Solve the problem on paper first before you implement it. I hope this was of any help.


Thanks for the answer, I think I solved it.

Reply to Thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.