✅ Collision in Avalonia
Hello, I am trying to find out how Collisions can be detected through Avalonia, i tested these things alerady:
Here my Cs Code of the Window I use:
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media.Imaging;
using Avalonia.Threading;
using MsBox.Avalonia;
using MsBox.Avalonia.Enums;
using System;
using System.Threading.Tasks;
namespace CollidingObjects
{
public partial class MainWindow : Window
{
public MainWindow() { InitializeComponent(); Image1.Source = new Bitmap("red.png"); Image2.Source = new Bitmap("green.png"); SetupPhysics(); } private async Task SetupPhysics() { Rect Object1 = new Rect(Canvas.GetLeft(Image1), Canvas.GetTop(Image1), Image1.Width, Image1.Height); Rect Object2 = new Rect(Canvas.GetLeft(Image2), Canvas.GetTop(Image2), Image2.Width, Image2.Height); if (Object1.Intersects(Object2)) { Console.Write("Collision"); var COllisionMedia = MessageBoxManager.GetMessageBoxStandard("Collision", "", ButtonEnum.Ok, MsBox.Avalonia.Enums.Icon.None, WindowStartupLocation.CenterScreen); await COllisionMedia.ShowAsync(); } } } } and here the AXAML code of my window: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compat<Window xmlns="https://github.com/avaloniaui" ibility/2006" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="CollidingObjects.MainWindow" Title="CollidingObjects"> <Canvas Name="GameCanvas"> <Image Name="Image1" Canvas.Top="0" Canvas.Left="20" />
<Image Name="Image2" Canvas.Top="30" Canvas.Left="-20" /> </Canvas>
</Window>
public MainWindow() { InitializeComponent(); Image1.Source = new Bitmap("red.png"); Image2.Source = new Bitmap("green.png"); SetupPhysics(); } private async Task SetupPhysics() { Rect Object1 = new Rect(Canvas.GetLeft(Image1), Canvas.GetTop(Image1), Image1.Width, Image1.Height); Rect Object2 = new Rect(Canvas.GetLeft(Image2), Canvas.GetTop(Image2), Image2.Width, Image2.Height); if (Object1.Intersects(Object2)) { Console.Write("Collision"); var COllisionMedia = MessageBoxManager.GetMessageBoxStandard("Collision", "", ButtonEnum.Ok, MsBox.Avalonia.Enums.Icon.None, WindowStartupLocation.CenterScreen); await COllisionMedia.ShowAsync(); } } } } and here the AXAML code of my window: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compat<Window xmlns="https://github.com/avaloniaui" ibility/2006" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="CollidingObjects.MainWindow" Title="CollidingObjects"> <Canvas Name="GameCanvas"> <Image Name="Image1" Canvas.Top="0" Canvas.Left="20" />
<Image Name="Image2" Canvas.Top="30" Canvas.Left="-20" /> </Canvas>
</Window>
9 Replies
I couldn´t find a solution that works, and I want to understand how to do it correctly
I suggest doing this instead https://github.com/AvaloniaUI/Avalonia/blob/master/samples/RenderDemo/Pages/CustomSkiaPage.cs#L28
Instead of using the controls
How does this work - i don´t understand this.. I am sorry but I am very new into coding lol
Because with avalonia it recalculates the layout constantly if you use controls
and with that I linked, you render it manually with no layout recalculations
Such as DrawRectangle(..)
ok - no I don´t think that might be a good idea as I am a noob - is there any other way?
I just want to make a Collision detection
That code will only run once upon control initialization, doesnt mean all of the other controls has yet been added. I suggest you start up the debugger because Width and Height could be NaN
NaN = Not a Number
In this case it means uninitialized
ooohh ok I need to add a height and width that might be my problem
isn´t it?
Try
k
it works 🙂 Thank you 🙂