Recent

Author Topic: collision detection  (Read 33361 times)

shs

  • Sr. Member
  • ****
  • Posts: 310
collision detection
« on: October 06, 2017, 01:14:01 pm »
how to code collision detection
can you show me example with 2 ball objects?
ball1 and ball2

Noodly

  • Jr. Member
  • **
  • Posts: 70
Re: collision detection
« Reply #1 on: October 06, 2017, 02:04:31 pm »
See https://www.freepascal.org/docs-html/rtl/objects/trect.intersect.html

The result is the coordinates of the rectangle formed by two overlapping rectangles. If the result is 0,0,0,0 then there is no overlap / collision.

This works fine in most situations, but if you want to be completely accurate in checking collisions for non-rectangular shapes (e.g. balls) then first detect the collision of their bounding rectangles (which will be square) and only if a rectangle collision is detected then call a second function to see if one or more of the pixels in your balls are overlapping within the overlapped rectangle.

I'm sure one of the graphics experts on the forum can expand on this.
Windows 10 Home, Lazarus 2.02 (svn 60954), FPC 3.04

Fungus

  • Sr. Member
  • ****
  • Posts: 354
Re: collision detection
« Reply #2 on: October 06, 2017, 02:31:14 pm »
If your balls are in 2d space, it is quite simple..

Code: Pascal  [Select][+][-]
  1. //Calculate the distance between the center of the two balls:
  2. distance = sqrt(sqr(ball1.x-ball2.x) + sqr(ball1.y-ball2.y)) //Pythagoras: A^2 + B^2 = C^2
  3.  
  4. //If the distance is less than ball1 radius + ball2 radius, we have a collision
  5. if distance < radius1 + radius2 then begin
  6.   //BANG!!!
  7. end;

EDIT: "*" became "+" and abs() was removed, looong time since math class :-[
« Last Edit: October 06, 2017, 07:14:16 pm by Fungus »

wp

  • Hero Member
  • *****
  • Posts: 13586
Re: collision detection
« Reply #3 on: October 06, 2017, 02:50:08 pm »
If the absolute (non negative) distance is less than ball1 radius + ball2 radius, we have a collision
How should it become negative? The square root returns always positive numbers (although, of course, -sqrt(a) is the second solution of the equation x^2 = a).

RAW

  • Hero Member
  • *****
  • Posts: 871
Re: collision detection
« Reply #4 on: October 06, 2017, 04:15:18 pm »
Just use aunt google... there are a lot of functions out there from people who needed this before...  :)

One way is this:
Code: Pascal  [Select][+][-]
  1. USES LCLIntf;
  2.  
  3. IntersectRect
  4. RectInRegion
  5. PtInRect
  6. RectVisible
... I guess there is more...

But don't ask me if this is working on LINUX and MAC...  :)

balazsszekely

  • Guest
Re: collision detection
« Reply #5 on: October 06, 2017, 04:21:06 pm »
You need "+" instead of "*" in the formula between two points.

wp

  • Hero Member
  • *****
  • Posts: 13586
Re: collision detection
« Reply #6 on: October 06, 2017, 04:36:04 pm »
Just use aunt google... there are a lot of functions out there from people who needed this before...  :)

One way is this:
Code: Pascal  [Select][+][-]
  1. USES LCLIntf;
  2.  
  3. IntersectRect
  4. RectInRegion
  5. PtInRect
  6. RectVisible
... I guess there is more...

??? If squares intersect it does not mean that the enclosed circles intersect as well. Although IntersectRect can be useful to avoid the expensive calculation of squares and square roots (if the rects don't intersect then the enclosed circles won't either).

RAW

  • Hero Member
  • *****
  • Posts: 871
Re: collision detection
« Reply #7 on: October 06, 2017, 04:58:06 pm »
Quote
??? If squares intersect it does not mean that the enclosed circles intersect as well....
Yes... just a little tip in general to show that there are ready to use routines...  :)
On the other hand sometimes it can be a lot faster to use a broad rect and not the exact form... Then you don't have to check every point...
Yeah.. definitely not with 2 balls ...  :D

RAW

  • Hero Member
  • *****
  • Posts: 871
Re: collision detection
« Reply #8 on: October 06, 2017, 05:31:56 pm »
I found a simple Delphi example and changed it a little bit... Now it compiles with LAZARUS very fine..
The only thing you have to do is to use the code from @Fungus and take a look at the comments from @wp and @GetMem and you are in the game...  :)


shs

  • Sr. Member
  • ****
  • Posts: 310
Re: collision detection
« Reply #9 on: October 12, 2017, 06:42:18 am »
hi how can i make 2 balls distance to be always equal so it looks like it is attached to each other
i want to move ball 1 and i want ball 2 to be attached behind the ball 1 in the direction of movement( like a tail);

Fungus

  • Sr. Member
  • ****
  • Posts: 354
Re: collision detection
« Reply #10 on: October 12, 2017, 12:38:36 pm »
hi how can i make 2 balls distance to be always equal so it looks like it is attached to each other
i want to move ball 1 and i want ball 2 to be attached behind the ball 1 in the direction of movement( like a tail);

Make them follow the same path? You need to store (in a buffer) the movement of ball1 and use the same movement on ball2 with a delay.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: collision detection
« Reply #11 on: October 12, 2017, 01:19:30 pm »
hi how can i make 2 balls distance to be always equal so it looks like it is attached to each other
i want to move ball 1 and i want ball 2 to be attached behind the ball 1 in the direction of movement( like a tail);

Make them follow the same path? You need to store (in a buffer) the movement of ball1 and use the same movement on ball2 with a delay.

um no not the same path. i just want the ball 2 to be attached behind ball 1. and i thought it would work if the distance between 2 balls are always 0. but i don't know how to code that

J-G

  • Hero Member
  • *****
  • Posts: 1057
Re: collision detection
« Reply #12 on: October 12, 2017, 01:27:20 pm »
um no not the same path. i just want the ball 2 to be attached behind ball 1. and i thought it would work if the distance between 2 balls are always 0. but i don't know how to code that
If you're dealing with 'balls' then you need to keep the distance between the 'centres' always the same - ie. the sum of their radii (in case they are not the same diameter).

Your knowledge of geometry will be a deciding factor as to whether you can achieve your objective.
« Last Edit: October 12, 2017, 04:19:54 pm by J-G »
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: collision detection
« Reply #13 on: October 12, 2017, 01:38:30 pm »
um no not the same path. i just want the ball 2 to be attached behind ball 1. and i thought it would work if the distance between 2 balls are always 0. but i don't know how to code that
And what exactly does "behind" mean in your interpretation ?

e.g. what happens to your 'behind' when you have two balls going from top to bottom and ball 1 reaches the bottom of your form/screen/display ? Does ball 2 also need to reach the bottom position in that case ? does ball 1 bounce back in upper direction (or perhaps even diagonal) ?

Imagine you have two actual soccer balls that you drop from a certain height, ball 2 attached to ball 1 and these balls fall down. what happens in your scenario when ball 1 reaches the ground ? does ball 2 automatically become ball 1 (front ball) and bounce back or, do the balls perhaps 'overlap' ?

In case the balls 'overlap' when they reach the 'playfield' then user fungus' suggestion of using a stored path makes sense (although you could also do with a matching formula and have a delayed function parameter for the second ball).

Nitorami

  • Hero Member
  • *****
  • Posts: 605
Re: collision detection
« Reply #14 on: October 12, 2017, 01:47:28 pm »
Obviously for two balls just touching each other the restriction for the ball centres is
sqrt (sqr(x2-x1) + sqr(y2-y1)) =! 2r
where (x1,y1), (x2,y2) are the center coordinates of ball 1 and 2 and r is the radius (assuming same radius).
(x1,y1) are known, so we have one equation with two unknown variables. The equation is fulfilled for all (x2,y2) which are in distance 2r to (x1,y1), i.e. a circle around (x1,y1).  You need a second restriction to reduce the solution to a single point. If you do not know what the restriction shall be, you won't be able to formulate a solution. Just "behind ball 1" is not sufficient.
You could say as second restriction that ball2 should be in the opposite direction of where ball1 moved. Say the movement of ball1 is (dx1,dy1), then the second restriction could be of sorts x2/y2 = -dx1/dy1. Probably not quite, and requires a bit more to avoid division by zero but such an additional restriction will allow a unique solution of the first one, and give you a target point for ball2 center, rather than a circle.
I guess however this scheme would look somehow unnatural. Personally I would do physical modelling, i.e. iterate  the differential equations for two balls with an inertia, and a sort of magnetic force between them; the force will be attracting as long as distance > 2r, and repelling when  distance <2r. The equations are trivial, a bit of care may be required to keep the system stable. This will look very natural, as if there is magnetism between the balls.

 

TinyPortal © 2005-2018