避免碰撞示例或帮助
我一直在尝试寻找一个可以适应并用于我正在开发的游戏的避免碰撞示例。它将用于模拟滑雪者避开山上树木的动作。我的运动基于自主角色的转向行为,并且有很多关于路径跟踪和聚集的好例子,但我找不到任何好的避免碰撞的例子。 The Nature of Code 网站有很棒的转向教程,但似乎涵盖了除障碍之外的所有内容回避。
我从此处转换了代码,但它不起作用它应该是这样的,因为碰撞是通过将障碍物中心投影到速度矢量上来发现的,而不考虑障碍物中心可能超出碰撞限制但圆仍在碰撞的情况。这是我改编的代码(用Processing(基于Java)编写)。
// Method to update location
void update() {
// Update velocity
vel.add(acc);
// Limit speed
vel.limit(maxspeed);
loc.add(vel);
// Reset accelertion to 0 each cycle
acc.mult(0);
}
void obstacleAvoid() {
float checkLength = 30*vel.mag();
PVector forward,diff,ray,projection,force;
float dotProd,dis;
forward = vel.get();
forward.normalize();
ray = forward.get();
ray.mult(checkLength);
for ( int i = 0; i < obs.size(); i++ ) {
Obstacle ob = (Obstacle)obs.get(i);
diff = ob.pos.get();
diff.sub(loc);
PVector temp2 = forward.get();
temp2.mult(ob.r);
diff.sub(temp2);
dotProd = diff.dot(forward);
if ( dotProd > 0 ) {
projection = forward.get();
projection.mult(dotProd);
dis = PVector.dist(projection,diff);
if ( (dis < (ob.r + r)) && (projection.mag() < ray.mag()) ) {
ob.hit = true;
force = forward.get();
force.mult(maxforce);
if ( sign(diff,vel) == -1 ) { //CCW
force.set(force.y,-force.x,0);
}
else { //CW
force.set(-force.y,force.x,0);
}
force.mult(1-(projection.mag())/ray.mag());
force.limit(maxforce);
acc.add(force);
}
}
}
}
因此,为了帮助我,我想知道是否有人知道任何完整的避免碰撞示例,这些示例遵循自主角色的转向行为方式来做得更好。 此站点是本文的示例小程序,也是我希望的确切示例可以看到代码。遗憾的是,它没有附带代码,我尝试反编译它,但它只显示了主类,所以这不是很有帮助。如果有人有这个示例的代码或类似的代码,或者教程,我将非常感激。
I have been trying to find a collision avoidance example that I can adapt and use for a game I am working on. It will be used to model a skier's movements to avoid trees on the hill. I am basing the movement off of Steering Behaviors for Autonomous Characters and there are a lot of good examples for path following and flocking, but I can't find any good ones for collision avoidance. The Nature of Code website had awesome tutorials for steering but seemed to cover everything but obstacle avoidance.
I converted the code from here but it doesn't work as well as it should because collisions are found by projecting the obstacles center onto the velocity vector without taking into account when the obstacles center may be outside the limits of collision but the circle is still colliding. Here is the code I adapted (written in Processing (Java based)).
// Method to update location
void update() {
// Update velocity
vel.add(acc);
// Limit speed
vel.limit(maxspeed);
loc.add(vel);
// Reset accelertion to 0 each cycle
acc.mult(0);
}
void obstacleAvoid() {
float checkLength = 30*vel.mag();
PVector forward,diff,ray,projection,force;
float dotProd,dis;
forward = vel.get();
forward.normalize();
ray = forward.get();
ray.mult(checkLength);
for ( int i = 0; i < obs.size(); i++ ) {
Obstacle ob = (Obstacle)obs.get(i);
diff = ob.pos.get();
diff.sub(loc);
PVector temp2 = forward.get();
temp2.mult(ob.r);
diff.sub(temp2);
dotProd = diff.dot(forward);
if ( dotProd > 0 ) {
projection = forward.get();
projection.mult(dotProd);
dis = PVector.dist(projection,diff);
if ( (dis < (ob.r + r)) && (projection.mag() < ray.mag()) ) {
ob.hit = true;
force = forward.get();
force.mult(maxforce);
if ( sign(diff,vel) == -1 ) { //CCW
force.set(force.y,-force.x,0);
}
else { //CW
force.set(-force.y,force.x,0);
}
force.mult(1-(projection.mag())/ray.mag());
force.limit(maxforce);
acc.add(force);
}
}
}
}
So to help me I was wondering if anyone knew of any complete examples of collision avoidance that follow the Steering Behaviors for Autonomous Characters way of doing things better. This Site is the example applet for the paper and is the exact example I wish I could see the code for. Sadly there is no code to come with it and I tried decompiling it but it just showed the main class so that wasn't very helpful. If someone has the code for this example or something like it, or a tutorial, I would appreciate it a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Craig Reynolds 无法发布您感兴趣的小程序的源代码。类似的 C++ 源代码位于 OpenSteer,由雷诺兹维护。克里斯蒂安·施内尔哈默 (Christian Schnellhammer) 和托马斯·菲尔卡斯 (Thomas Feilkas) 致力于扩展雷诺兹的原始论文。他们的论文被翻译成英语,其中包含有关避障的部分。他们的工作源代码可在 Java 中找到。但是,我认为 Shiffman 的代码是一个很好的起点,听起来您已经非常接近您想要的了。
我的第一个处理程序之一修改了 Boid 示例以模拟僵尸启示录。三角形追逐着躲避它们的圆圈。每个幸存者都会检查视野中的其他僵尸,并在函数
PVectorpanic(ArrayListfected)
中对威胁的位置向量进行平均。之后,需要对新向量进行负权重并将其添加到幸存者的当前向量中,就像任何其他力一样。例如:如果您的滑雪者成功检测到障碍物,那么回避就是问题所在。为回避向量添加更强的权重,增加滑雪者可以“看到”与对象交互的半径,甚至向对象添加一个返回距离滑雪者最近点位置的方法都可以解决您的问题。您还可以根据滑雪者到其前面最近的障碍物的距离添加减速度。
请记住,即使您感兴趣的小程序也不能完全避开障碍。我的解决方案可能并不完全是小程序中发生的情况,但通过尝试确定滑雪者方向的力,您可以实现非常相似(并且可能更好)的效果。
Craig Reynolds cannot release the source code for the applets you're interested in. Similar source code is available in c++ at OpenSteer, which is maintained by Reynolds. Christian Schnellhammer and Thomas Feilkas worked to expand Reynolds original paper. Their paper is translated into english and contains a section on obstacle avoidance. The source code for their work is available in Java. However, I think Shiffman's code is a great starting point, and it sounds like you're pretty close to what you want already
One of my first Processing programs modified the Boids example to simulate a zombie apocalypse. Triangles chased circles that were avoiding them. Each survivor checks for other zombies in their vision, and averages the location vectors of threats in a function,
PVector panic(ArrayList infected)
. After that, it was a matter of weighting the new vector negatively and adding it to the survivor's current vector like any other force. Something like:If your skier is successfully detecting the obstacles, then avoidance is the problem. Adding a stronger weight to the avoidance vector, increasing the radius the skier can 'see' to interact with objects, or even adding a method to the object that returns the location of the closest point to the skier could solve your problem. You could also add deceleration based on the distance from the skier to the nearest obstacle in front it.
Remember that even the applet you're interested in does not perfectly avoid obstacles. My solution may not be exactly what is happening in the applet, but by playing around with the forces determining you skier's direction, you can achieve a very similar (and possibly better) effect.
在 NEHE 游戏开发网站上查看此链接:
http://nehe.gamedev.net/data/lessons/lesson.asp ?lesson=30
它是使用 c++ 和 win32 API 完成的。尽管您会找到使用 JOGL 的 Java Port 链接。
还有
http://www.red3d.com/cwr/steer/
的源代码可在此处http://opensteer.sourceforge.net/
使用 C++ 获得。你检查过了吗??Checkout this link on NEHE game development site:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=30
It is done using c++ and win32 API. Although you will find a link for Java Port using JOGL.
Also
The Source code for
http://www.red3d.com/cwr/steer/
is available herehttp://opensteer.sourceforge.net/
though in c++. Have you checked it??