# Finding collision between two Images using virtual Rect

I'm making a game. There is a turtle and lots of platforms it can walk on. It walks on its own and you can change the direction it is walking. I implemented a working logic, but I think it can be improved.

I made 2 classes (one for the turtle and one for the platforms).

class Turtle
{
public Rect HitBox;
public bool IsMoving;
public bool IsDirectionForward;
public double DistanceWalked;
public double MetersFalled;
}

class Ground
{
public Rect HitBox;
}


Then I created a virtual Rect that would simulate the right width and height of the ground and the turtle only after using Inflate function.

public MainWindow()
{
InitializeComponent();

ground1 = new Ground();
ground1.HitBox = new Rect(groundImg1.Margin.Left, groundImg1.Margin.Top, groundImg1.Width, groundImg1.Height);
ground1.HitBox.Inflate(0, -35);

zelvicka = new Turtle();
zelvicka.IsMoving = true;
zelvicka.IsDirectionForward = true;

timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(20);
timer.Tick += Update;
timer.Start();
}


I've done the same with the turtle, whose virtual Rect is updating every time it moves in Update function generated by DispatcherTimer so the virtual Rect is always up to date.

    private void Update(object sender, EventArgs e)
{
zelvicka.HitBox = new Rect(turtleImg.Margin.Left, turtleImg.Margin.Top, turtleImg.Width, turtleImg.Height);
zelvicka.HitBox.Inflate(-110, -30);

// ZELVICKA IS GOING FORWARD AND IS TOUCHING THE GROUND
// ZELVICKA IS GOING BACKWARDS AND IS TOUCHING THE GROUND
if (zelvicka.IsMoving && zelvicka.IsDirectionForward && zelvicka.HitBox.IntersectsWith(ground1.HitBox))
{
turtleImg.Margin = new Thickness(zelvicka.DistanceWalked += 3, zelvicka.MetersFalled, 0, 0);
}
else if (zelvicka.IsMoving && !zelvicka.IsDirectionForward && zelvicka.HitBox.IntersectsWith(ground1.HitBox))
{
turtleImg.Margin = new Thickness(zelvicka.DistanceWalked -= 3, zelvicka.MetersFalled, 0, 0);
}
}


Rate this code, please, and suggest improvements, if any.

• It works properly, but I have to manually set new height and new width with Inflate function, which I found very ineffective and time-consuming, hence I'm looking for a better way as I don't think my code is good enough and it needs some more improvements! – innominateArt Oct 29 '20 at 14:55
• The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How do I ask a good question?. – BCdotWEB Oct 29 '20 at 15:12