不可预测的指针行为
Unpredictable pointer behavior
目前我正在使用OpenFrameworks构建一个布料物理应用程序。我是C++新手,请注意。
在我的应用程序中,两个"相邻"粒子对象作为指针传递给弹簧对象。作为测试,我让弹簧对象在两个粒子之间(在它们的 3d 矢量位置之间)画线。出于某种原因,每次运行程序时,这些行都不同,即使不涉及随机值。当我从弹簧结构中找出粒子位置的值时,它们通常是荒谬的值,例如 -4.15301e-12。我几乎逐字逐句地遵循示例代码,所以我不确定我哪里出错了。
这是我遵循的示例代码:
https://sites.google.com/site/ofauckland/examples/17-cloth-physics
这是我的弹簧结构:
#pragma once
#include "ofMain.h"
#include "Particle.h"
struct Spring {
float k, restLength;
Particle *a, *b;
ofVec3f posA, posB;
Spring(Particle *a, Particle *b, float k = .2) : a(a), b(b), k(k) {
restLength = (b->pos - a->pos).length();
}
void update() {
posA = a->pos;
posB = b->pos;
}
void draw() {
ofSetLineWidth(5);
ofSetColor(0, 255, 0);
ofLine(posA.x, posA.y, posB.x, posB.y);
}
};
粒子结构:
#pragma once
#include "ofMain.h"
struct Particle {
ofVec3f pos;
Particle(ofVec3f pos) : pos(pos) {
}
void update() {
}
void draw() {
ofSetColor(ofRandom(255), 0, 0);
ofFill();
ofCircle(pos.x, pos.y, 3);
}
};
这就是我将两个粒子作为指针传递给弹簧的地方:
#pragma once
#include "ofMain.h"
#include "Particle.h"
#include "Spring.h"
struct Petal {
float maxWidth, spacing;
vector<Particle> particles;
vector<Spring> springs;
Petal(float maxWidth, float spacing) : maxWidth(maxWidth), spacing(spacing) {
setupPoints();
}
void setupPoints() {
float x = 0;
float y = 0;
for(int r = 1; r <= maxWidth; r++) {
x = (int)(r / 2) * -spacing;
y += spacing;
for(int c = 1; c <= r; c++) {
ofVec3f pos = ofVec3f(x, y, 0);
Particle p(pos);
particles.push_back(p);
x+=spacing;
}
}
for(int r = maxWidth; r > 0; r--) {
x = (int)(r / 2) * -spacing;
y += spacing;
for(int c = 1; c <= r; c++) {
ofVec3f pos = ofVec3f(x, y, 0);
Particle p(pos);
particles.push_back(p);
x+=spacing;
}
}
//find neighbors
for(int i = 0; i < particles.size(); i++) {
Spring s(&particles[i], &particles[findNeighbor(i)]);
springs.push_back(s);
}
}
int findNeighbor(int pIndex) {
float leastDist = 0;
float leastDistIndex = 0;
for(int i = 0; i < particles.size(); i++) {
if(i != pIndex) {
float distance = particles[pIndex].pos.distance(particles[i].pos);
if(abs(distance) < abs(leastDist) || leastDist == 0) {
leastDist = distance;
leastDistIndex = i;
}
}
}
return leastDistIndex;
}
void update() {
for(int i = 0; i < particles.size(); i++) {
particles[i].update();
}
for(int s = 0; s < springs.size(); s++) {
springs[s].update();
}
}
void draw() {
for(int i = 0; i < particles.size(); i++) {
particles[i].draw();
}
for(int s = 0; s < springs.size(); s++) {
springs[s].draw();
}
}
};
这就是发生的事情。奇怪的是,一些弹簧似乎处于正确的位置。
如果我能澄清一些事情,请告诉我。
谢谢!
您的particles
向量按值保存Particles
,vector
可以复制和移动这些值。当您将Particles
作为指向Spring
的指针传递时,您正在传递将来某个时候可能不存在的内容的地址。我不确定这是否是问题所在,但这肯定是需要解决的问题。
相关文章:
- 不可预测的C++睡眠/等待行为
- 超过 N 时出现不可预测的位移结果
- DirectShow CSourceStream::FillBuffer 暂停和查找后对第一帧的不可预测的调用数
- 多线程文件 IO 程序在线程数增加时行为不可预测
- 使用 std::find 查找从二进制文件中读取的字符并转换为 std::vector 中的 std::string<string> 会产生这种不可预测的行为?
- std::sleep_for在Windows 10上的不可预测行为
- 不可预测的文件描述符泄漏
- 循环程序的行为是不可预测的
- C++中不可预测的输出
- 当来自外部库的线程不可预测地崩溃我的应用程序时,我该怎么办
- 不可预测的伪RNG
- C++ 自定义运算符(+=) 的行为方式不可预测
- 如何优化间接基数排序?(又名如何优化不可预测的内存访问模式)
- 修复不可预测的崩溃
- 不可预测的指针行为
- wxWidgets 不可预测的 Seg 故障
- 当 cin.getline() 的第二个参数大于数组长度时,不可预测的字符串长度
- 子表达式求值顺序的不可预测性
- 将QString转换为const-char*时的不可预测的运行时行为
- 是什么让指针的使用变得不可预测