# [Silverlight动画]转向行为 - 对象回避

```  public partial class Circle : UserControl
{
private Color _color;

set { _radius = value; }
}

public Vector2D position {
get { return new Vector2D(_compositeTransform.TranslateX, _compositeTransform.TranslateY); }
}

private CompositeTransform _compositeTransform;
private TransformGroup _transformGroup;

public Circle()
{
InitializeComponent();
}

public void init(double radius,Color color) {
xCircle.Fill = new SolidColorBrush(color);
_color = color;
}

{
var transformGroup = this.RenderTransform as TransformGroup;
if (transformGroup == null)
{
_transformGroup = new TransformGroup();
this.RenderTransform = _transformGroup;
_compositeTransform = new CompositeTransform();
}
}

public double x
{
get
{
return _compositeTransform.TranslateX;
}
set
{
_compositeTransform.TranslateX = value;
}
}

public double y
{
get
{
return _compositeTransform.TranslateY;
}
set
{
_compositeTransform.TranslateY = value;
}
}
}```

```        private double _avoidDistance = 300;
private double _avoidBuffer = 20;
public void avoid(List<Circle> circles) {
foreach (var circle in circles)
{

//障碍物和机车的位移向量
Vector2D difference = circle.position.subtract(_postion);

//如果障碍物在机车前方
if (dorProd>0)
{
//机车的触角
//位移在触角上的映射
//障碍物离触角的距离
double dist = projection.subtract(difference).length;

//如果触角在计算上缓冲后和障碍物相交并且位移的映射的长度小于触角的长度，我们就说是将要发生碰撞，需要改变运行方向
{
//计算出一个90度的转向力
force.angle += difference.sign(_velocity) * Math.PI / 2;

//通过距离障碍物的距离，调整力度大小，使之足够小但又能避开
force = force.multiply(1.0 - projection.length / feeler.length);

// 叠加与转向力上

//刹车，转弯时要放慢机车的速度，里障碍物越近刹车力越大
_velocity = _velocity.multiply(projection.length / feeler.length);
}

}
}
}```

```<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
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-compatibility/2006"
xmlns:local="clr-namespace:Steer" xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
mc:Ignorable="d"
x:Class="Steer.AvoidTest"
d:DesignWidth="640" d:DesignHeight="480">

<Grid x:Name="LayoutRoot">
<local:SteeredVehicle x:Name="myWander" HorizontalAlignment="Left" Height="40" VerticalAlignment="Top" Width="40" RenderTransformOrigin="0.5,0.5">
<ed:RegularPolygon Fill="Blue" Height="40" InnerRadius="1" PointCount="3" Stretch="Fill" Stroke="Black" UseLayoutRounding="False" Width="40" RenderTransformOrigin="0.5,0.5" StrokeThickness="0">
<ed:RegularPolygon.RenderTransform>
<CompositeTransform Rotation="90"/>
</ed:RegularPolygon.RenderTransform>
</ed:RegularPolygon>
</local:SteeredVehicle>
<local:Circle x:Name="Circle1" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<local:Circle x:Name="Circle2" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Grid>
</UserControl>```
```	public partial class AvoidTest : UserControl
{
List<Circle> circles;
public AvoidTest()
{
// Required to initialize variables
InitializeComponent();

}

{
myWander.position = new Vector2D(200, 200);
Circle1.init(100, Colors.Orange);
Circle1.x = 600;
Circle1.y = 200;

Circle2.init(70, Colors.Red);
Circle2.x = 100;
Circle2.y = 300;

circles = new List<Circle>();

CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
}

void CompositionTarget_Rendering(object sender, EventArgs e)
{
myWander.wander();
myWander.avoid(circles);
myWander.update();
}
}```

152 篇文章45 人订阅

0 条评论

## 相关文章

2985

### carla无人驾驶模拟中文项目 carla_simulator_Chinese

https://github.com/xfqbuaa/carla_simulator_Chinese

1343

### 2018-母牛的故事

有一头母牛，它每年年初生一头小母牛。每头小母牛从第四个年头开始，每年年初也生一头小母牛。请编程实现在第n年的时候，共有多少头母牛？

3482

1003

1171

2141

### hdu----(1849)Rabbit and Grass（简单的尼姆博弈）

Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/3...

3617

2055

2967

### 1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路

1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路 Time Limit: 5 Sec  Memory L...

3366