I am trying to make a little solution for IK and I ran into a big wall with this error. My lack of pointers and references have kicked me down. I get this error "this was 0xFFFFFFFFFFFFFFF7" because I think I cannot access the Segment to change its values.
Segment class:
struct Vector2 {
float x;
float y;
};
class Segment {
public:
Segment(float ix, float iy, int len, float ia, Segment* ip = nullptr) {
x = ix;
y = iy;
angle = ia;
length = len;
hasParent = (ip != nullptr);
parent = ip;
}
void pointAt(float tx, float ty) {
float dx = x - tx;
float dy = y - ty;
angle = atan2(-dy, -dx);
}
void drag(float tx, float ty) {
pointAt(tx, ty);
x = tx - cos(angle) * length;
y = ty - sin(angle) * length;
if (hasParent) {
parent->drag(x, y);
}
}
Vector2 getEnd() {
return { (float)(x + cos(angle) * length), (float)(y + sin(angle) * length)};
}
Vector2 getStart() {
return { x, y };
}
void setAngle(float newAngle) {
angle = newAngle;
}
private:
bool hasParent;
float angle;
float x;
float y;
int length;
Segment* parent;
};
I initialize the vector of segments here, notice how im puting in a reference of the last segments parent for the declaration of the new segment.
I then try to drag the last segment but that's when I get a crash on the endSeg.drag
call below
int main() {
std::vector<Segment> segments;
size_t segment_amt = 10;
int length = 0;
for (int i = 0; i < segment_amt; i++) {
if (i == 0) {
segments.push_back(Segment(0.0f, 0.0f, length, 0.0f));
}
else {
segments.push_back(Segment(segments[i - 1].getStart().x, segments[i - 1].getStart().y, length, 0.0f, &segments[i - 1]));
}
}
Segment& endSeg = segments[segments.size() - 1];
endSeg.drag(0, 0); // crashes here
}