Get access to over 100 FireMonkey cross platform samples for Android, IOS, OSX, Windows, and Linux!

AndroidAppmethodCode SnippetDelphiDemoFiremonkeyIOSOSXWindows

Point Based Collision Detection With The Ouya For Delphi XE5 Firemonky On Android

| Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOS

Delphi XE5 Firemonkey Ouya Collision Detection | Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOSJim McKeeth from Embarcadero has a demo up where he built an app for the Ouya game console using Delphi XE5 Firemonkey. The Ouya runs Android but it has a controller so you have to check for keystrokes (the buttons from the controller) instead of mouse movements or implement a mouse cursor controlled by the controller. The Ouya demo contains two main pieces of code where the first is the key down event and the second is a point based collision detection function. If you want rectangle based collision detection check out this other post. The point based collision detection function just checks the distance between two points and if they are close enough it does it’s action. You could use the point based collection detection in any of your apps as it is not specific to Android or the Ouya console so it should also work on IOS, Windows, and OSX. Here is a sample of the keydown code:
procedure TForm5.FormKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char;
Shift: TShiftState);
case Key of
37: MoveLeft;
38: MoveUp;
39: MoveRight;
40: MoveDown;

label1.Text := 'Key: ' + Key.ToString;

And here is a sample of the point based collision detection. You could easily convert this function into a re-usable function where you pass in the two objects as variables and the distance that you want to check and then return true of false.
procedure TForm5.HitCheck;
shipPt, targetPt: TPointF;
distance: Integer;
shipPt := ship.Position.Point;
targetPt := target.Position.Point;
distance := round(shipPt.Distance(targetPt));

label2.Text := 'Dist: ' + round(distance).ToString;

if distance < fMove then
targetX.StartValue := target.Position.X;
targetY.StartValue := target.Position.Y;
targetX.StopValue := Random(round(playArea.width));
targetY.StopValue := Random(round(playArea.height));
label2.Text := 'Hit!';
fScore := fScore + 1;
lblScore.Text := 'Score: ' + fScore.ToString;

Check out the full blog post by Jim about the Ouya app demo and download the full source code.

The direct link to the source code is here. It is packed up with a bunch of other demos but just open the Ouya demo from the archive to get to the code. You have to register to download the file.

Have Delphi Firemonkey questions? Ask and get answers on StackOverflow.

Related posts

AutoBlogAI: FireMonkey Client To Leverage LLMs And Generative AI For Blogging


Unleashing Creativity With Song Writer AI: A Deep Dive


How To Build Stable Diffusion Text To Image Prompts


FireMonkey 10.4.2 Features Updated iOS 14, Android 11, And macOS 11 Support Plus Hundreds Of Fixes

Sign up for our Newsletter and
stay informed

Leave a Reply