Repository navigation
Expand file tree
/
Copy pathCircle.cs
More file actions
80 lines (78 loc) · 2.48 KB
/
Copy pathCircle.cs
File metadata and controls
80 lines (78 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PhysicsEngine
{
public class Circle : Shape
{
public float Radius;
public Circle(Vector position, Vector velocity, float radius, float mass, Color color) : base(position, velocity, mass, color)
{
Radius = radius;
}
public override void Update(float deltaTime)
{
Velocity += Acceleration * deltaTime;
position += Velocity * deltaTime;
}
public override bool IsColided(Shape shape)
{
if (shape is Circle circle)
{
Vector delta = circle.position - this.position;
float distance = delta.Length();
return distance < (this.Radius + circle.Radius);
}
else if (shape is Rectangle rectangle)
{
return Collision.AreColliding(rectangle, this);
}
return false;
}
public override void ResolveCollision(Shape shape)
{
if (shape is Circle circle)
{
Collision.ShapeCollision(this, circle);
}
//else if (shape is Rectangle rectangle)
//{
// Collision.ShapeCollision(rectangle,this);
//}
}
public override void ResolveScreenCollision(float screenWidth, float screenHeight)
{
if (position.X - Radius < 0)
{
position.X = Radius;
Velocity.X *= -1;
}
else if (position.X + Radius > screenWidth)
{
position.X = screenWidth - Radius;
Velocity.X *= -1;
}
if (position.Y - Radius < 0)
{
position.Y = Radius;
Velocity.Y *= -1;
}
else if (position.Y + Radius > screenHeight)
{
position.Y = screenHeight - Radius;
Velocity.Y *= -1;
}
}
public override void Fill(Graphics g)
{
g.FillEllipse(new SolidBrush(color), (int)position.X, (int)position.Y, (int)(2 * Radius), (int)(2 * Radius));
}
public override void Draw(Graphics g)
{
g.DrawEllipse(new Pen(color, 1), (int)position.X, (int)position.Y, (int)(2 * Radius), (int)(2 * Radius));
}
}
}