Author Topic: Check if two Images collide  (Read 415 times)


  • New member
  • *
  • Posts: 8
Check if two Images collide
« on: February 05, 2023, 12:56:08 am »
I am working on my own little game in Lazarus.
My problem: I have two TImages and I want to test if they collide.
My idea was to give both images a rectangle (hitbox) and then test if the edges of the images overlap. But I can't find the right solution for this task. Is the idea with the rectangles and then checking with IntersectRect the right idea in the first place? Or is there an even better alternative?

In my Canvas are two images and then the following code:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormActivate(Sender: TObject);
  2. var
  3.   Image1Rect, Image2Rect: TRect;
  4.   Intersection: TRect;
  5. begin
  6.   Image1.Picture.LoadFromFile('UserTest.jpg');
  7.   Image2.Picture.LoadFromFile('BulletTest.jpg');
  9.    Image1Rect := Rect(Image1.Left, Image1.Top, Image1.Left + Image1.Width, Image1.Top + Image1.Height);
  10.   Image2Rect := Rect(Image2.Left, Image2.Top, Image2.Left + Image2.Width, Image2.Top + Image2.Height);
  12.   if IntersectRect(Image1Rect, Image2Rect) then
  13.   begin
  14.     // collision detected
  15.   end;
  17. end;    
I am looking forward to your answers.


  • Hero Member
  • *****
  • Posts: 4833
  • My goal: build my own game engine using Lazarus
Re: Check if two Images collide
« Reply #1 on: February 05, 2023, 03:33:19 am »
TImage is not optimized for game programming, it is slow. Also, I saw you put the collision detection inside FormActive event, that seems weird to me.

I already provided you the things you need, it's clear you didn't read:,61959.msg468054.html#msg468054
« Last Edit: February 05, 2023, 03:37:01 am by Handoko »


  • Hero Member
  • *****
  • Posts: 1028
Re: Check if two Images collide
« Reply #2 on: February 05, 2023, 04:16:05 am »
TImage is not optimized for game programming, it is slow.
I couldn't agree more even if I wanted to.

imho there is only one small caveat: you do not know how slow unless experienced that yourself.

In my opinion using images (TImage) for grasping some of the concepts that are used for/by games are more beneficial. The things you learn from that (positioning (all axis), collision detection etc) can be used later on when switching to direct canvas/bitmap drawing or even when using a dedicated graphical engine.

I do not know anything about what TS is working on but as long as it does not require much speed then it also does not matter much (e.g. there is no need for speed for a game of chess/checkers).


TinyPortal © 2005-2018