I'm having some problems with polymorphism I have a superclass of CEntity, and a subclass of unit type, I am dynamic casting and removing and emplacing a new entity of the dynamic casts type at its place, I have the opposite problem of my previous question.
Problem is the values being set are not changing, it remains with default values, it appeared to work before but now it has stopped working, I'm not sure what has caused the issue.
specifically buildsetup, sets the x-y-z values of the unit subclass's x,y,z, but when I look inside the vector the values remain unchanged, this is strange because it does actually manage to change the values for whether the unit is alive or not.
void builder(int no, string in , int top, int bot, CTeam &team, string owner, string original)
{
for (int i = top; i <= bot; i++)
{
EntityUnit* a;
a = dynamic_cast<EntityUnit*>(AWorld.EntitiesVector[i]);
a->unit_alive;
if (a->unit_alive == false)
{
float x = Player.returncity_add().cit_ret_X();
float y = Player.returncity_add().cit_ret_Y();
float z = Player.returncity_add().cit_ret_Z();
cout << "X:" << x;
cout << "Y:" << y;
cout << "Z:" << z;
float cost = MainAB.UnitTemplates[no]->UDRetCost();
float health = MainAB.UnitTemplates[no]->UDRetMaxHealth();
float damage = MainAB.UnitTemplates[no]->UDRetStrength();
float speed = MainAB.UnitTemplates[no]->UDRetSpeed();
float buildtime = MainAB.UnitTemplates[no]->UDRetBuildTime();
int popcost = MainAB.UnitTemplates[no]->UDRetPop();
a->set_owner(owner);
setmodel(i, x, y, z); // takes an xyz by ref and sets the model
to them then changes the model's localz by -10
Units[i]->SetSkin(setskin(owner, original));
a->BuildSetup(x, y, z, health, damage, speed, buildtime, cost, popcost);
team.inc_popcount(a->UDRetPop());
a->set_unit_alive(true);
sBuildUnit.play();
AWorld.EntitiesVector.erase(AWorld.EntitiesVector.begin() + i);
AWorld.EntitiesVector.emplace(AWorld.EntitiesVector.begin() + i, new EntityUnit(a));
AWorld.EntitiesVector[i]->set_x(x);
AWorld.EntitiesVector[i]->set_y(y);
AWorld.EntitiesVector[i]->set_z(z);
break;
}
}
Entity build setup
void EntityUnit::BuildSetup(float x, float y, float z,float _health, float _damage, float _speed, float _buildtime, float _cost, int _popcost)
{
unit_x = x;
unit_y = y;
unit_z = z;
unit_health[0] = _health;
unit_health[1] = _health;
unit_damage = _damage;
speed = _speed;
buildtime = _buildtime;
cost = _cost;
CUnitType = NA;
pop_req = _popcost;
}
After static debugging it, it most definately emplaces a new unit with the updated -is_alive, and while a's values change at the point of unitbuild, when its emplaced all x,y,z's return to 9999, which was what it was when they were pushed on the vector.
When you call
AWorld.EntitiesVector.erase(AWorld.EntitiesVector.begin() + i);
you destroy the object pointed to by a. The subsequent reference to it on the next line is Undefined Behavior, and anything is possible.
I'm not sure why you erase the entity, then try to put a new one in the same place. If you structure your code right you should be able to just reuse the existing entity (pointed to by a) without the erase and emplace calls.
Ok, Apparently the problem was it was totally unnecessary to erase/emplace, as I get what you mean now returning a pointer, it edited the value... odd it didn't work last time.
Related
I'm trying to translate a class from C++ to Matlab. In C++, the same data can be accessed in multiple ways because the values are declared as a union.
However, it seems like the only way to do this in Matlab is to override subsref and subsasgn, which opens up a can of worms of having to parse methods.
Is there a different way to handle this? I saw some comments from 2014 indicating there wasn't
This is the C++ enum in question:
template <class T>
class Euler
{
public:
union
{
T m[3];
struct
{
union { T x; T yaw; T psi; }; // m[0]
union { T y; T pitch; T theta; }; // m[1]
union { T z; T roll; T phi; }; // m[2]
};
};
};
The simple solution: forget about these names, and translate this class as a simple array, indexing m.x as m[1], m.roll as m[2], etc. This might not be great for some use cases, but certainly is the most efficient solution.
The alternative solution requires creating a custom class. subsref and subsasgn are not necessary, it is possible to declare dependent properties and define getters and setters (though it's not pretty!):
classdef Euler
properties
x = 0;
y = 0;
z = 0;
end
properties (Dependent)
yaw, pitch, roll;
psi, theta, phi;
end
methods
function value = get.yaw(obj)
value = obj.x;
end
function value = get.pitch(obj)
value = obj.y;
end
function value = get.roll(obj)
value = obj.z;
end
function obj = set.yaw(obj,value)
obj.x = value;
end
function obj = set.pitch(obj,value)
obj.y = value;
end
function obj = set.roll(obj,value)
obj.z = value;
end
% Add same methods for psi, theta, phi.
end
end
You can now do:
e = Euler;
e.x = 1;
e.yaw % returns 1
e.roll = 5;
e.z % returns 5
That is, the object has properties x, y, z, yaw, pitch and roll (and after adding appropriate setters and getters also psi, theta and phi), but only stores three values. The values of e.g. yaw and x are linked, and always identical.
I'm making a small OpenGL program for my intro to C++ class in Uni. I have a program that is complete but I want to change it up a bit to make it more unique. I have a Cube class:
class Cube {
public:
Cube(Mesh* mesh, Texture2D* texture, float x, float y, float z);
~Cube();
void Draw();
void Update(float rSpeed);
Vector3 position;
private:
GLfloat rotationSpeed;
Vector3 rotationVector;
Mesh* _mesh;
Texture2D* _texture;
};
I then create an array of type Cube:
Cube* cubes[CUBE_AMOUNT];
I then fill each index of this array with data to draw the cube on screen later in the program:
for (int i = 0; i < CUBE_AMOUNT; i++) {
float x = ((rand() % 400) / 10.0f) - 20.0f;
float y = ((rand() % 200) / 10.0f) - 10.0f;
float z = -(rand() % 1000);
if (i % 2 == 1) {
cubes[i] = new Cube(cubeMesh, textureStars, x, y, z);
}
else {
cubes[i] = new Cube(cubeMesh, texturePenguins, x, y, z);
}
}
With this new thing I want to add to the program, I want to check whether an index of cubes[] has been filled with the data yet. However I keep getting exceptions when running. I have tried to check whether cubes[i] is equal to nullptr, and tried checking whether it is NULL too, but neither seem to match.
Sorry for any errors in terminology that I used. New to C++, and having come from only doing Python before this, it is confusing!
Solution:
When I create the array, I changed it to Cube* cubes[CUBE_AMOUNT] = { NULL }, and now when checking the array, cubes[i] == NULL!
If cubes is not a global variable, you can use:
Cube* cubes[CUBE_AMOUNT] = {};
to initialize all the elements to nullptr.
You can also use:
std::vector<std::unique_ptr<Cube>> cubes(CUBE_AMOUNT);
to remove the burden of having to deallocate dynamic memory in your code.
In either case, can use:
if ( cubes[index] )
{
// Got a valid pointer. Use it.
}
Your cubes variable is not automatically initialized with null_ptr's. Until you either fill it with null_ptr's or good pointers it initially points to random garbage.
I think this would work
//This bit should check if theres anything stored currently.
cout << "\nWhich Slot would you like to store the informaton in ?(1-10)";
cin >> i;
i--;
if (information[i] != NULL){
// Already written
cout << "THERES SOMETHING HERE";
}
else{
cout << "\nEMPTY!!!!!!!!!";
}
I have a class called Pixel where I only store the values (x,y). Then I created a std::vector to store the pixels in a patch and compute some disparity.
Surprisingly, after some Pixels are push_back() into the vector I get a NULL reference but I don't see why. Here's my code
std::vector<Pixel*> disparityPatches;
std::vector<Pixel*> patchPos;
//COMPUTE THE DISPARITY FOR EACH OF THE PIXEL COORDINATES IN THE PATCH
for (uint iPatchPos = 0; iPatchPos < patchPos.size(); iPatchPos++)
{
disparityPatches.push_back(new Pixel(patchPos[iPatchPos]->getX()+dispNodeX, patchPos[iPatchPos]->getY()+dispNodeY));
if( (disparityPatches[disparityPatches.size()-1]) == NULL)
{
std::cout << "WHAT!?" << std::endl;
}
}
Can you see why is this hapenning? I've checked that patchPoshas the correct data and dispNodeX and dispNodeY have the correct info too.
It's weird because the first three iterations (iPatchPos = [0,1,2,3]) work fine then i get the NULL reference.
EDIT
class Pixel
{
float _x;
float _y;
public:
Pixel();
Pixel::Pixel(float x, float y)
{
_x = x;
_y = y;
}
const float Pixel::getX()
{
return _x;
}
const float Pixel::getY()
{
return _y;
}
};
You used below code to add a pix into the vector:
disparityPatches.push_back(new Pixel(patchPos[iPatchPos]->getX()+dispNodeX, patchPos[iPatchPos]->getY()+dispNodeY));
maybe new Pixel(patchPos[iPatchPos]->getX()+dispNodeX, patchPos[iPatchPos]->getY()+dispNodeY) may return null when the constructor failed in someway, so NULL will be added, then you got your error.
I have some code where I have a pointer to an object. I call a method on that pointer but the behaviour of the method is wrong in this case. I tried calling a method on the object itself and this actually gives the desired behaviour of the method.
Why does this cause different behaviour?
Also is there a way of assigning an object to a new variable without using pointers because I want the behaviour for the method called on the object itself?
Thanks.
EDIT:
Hopefully a sufficient example:
In a Robot class:
std::vector<ProjectOne::R_ID> Robot::positions;
int Robot::ID = -1;
Robot::Robot(double x, double y)
{
++ID;
robot_ID = ID;
initialX = x;
initialY = y;
// Placeholder. Doesn't actually get used properly atm.
fWidth = 0.35;
px = x;
py = y;
ProjectOne::R_ID msg;
msg.R_ID = robot_ID;
msg.x = x;
msg.y = y;
positions.push_back(msg);
string robotOdom = "robot_" + int2str(robot_ID) + "/odom";
string robotVel = "robot_" + int2str(robot_ID) + "/cmd_vel";
RobotOdometry_sub = n.subscribe<nav_msgs::Odometry>(robotOdom,1000,&Robot::ReceiveOdometry,this);
RobotVelocity_pub = n.advertise<geometry_msgs::Twist>(robotVel,1000);
ros::spinOnce();
}
void Robot::ReceiveOdometry(nav_msgs::Odometry msg)
{
//This is the call back function to process odometry messages coming from Stage.
px = initialX + msg.pose.pose.position.x;
py = initialY + msg.pose.pose.position.y;
ptheta = angles::normalize_angle_positive(asin(msg.pose.pose.orientation.z) * 2);
}
int Robot::selectLeader()
{
int leader_ID = robot_ID;
double lowestDistance = 9999999999.9;
for (unsigned int i=0;i<positions.size();i++)
{
double distance = calculateDistance(positions[i].x, positions[i].y, 0.0, 0.0);
if (distance < lowestDistance && distance != 0.0)
{
leader_ID = positions[i].R_ID;
lowestDistance = distance;
}
}
ROS_INFO("leader is: %d", leader_ID);
return leader_ID;
}
double Robot::calculateDistance(double x1, double y1, double x2, double y2)
{
double deltax = x2 - x1;
double deltay = y2 - y1;
double distanceBetween2 = pow(deltax, 2) + pow(deltay, 2);
double distanceBetween = sqrt(distanceBetween2);
return distanceBetween;
}
double Robot::calculateHeadingChange(double x, double y)
{
double deltax = x - px;
double deltay = y - py;
double angleBetween = atan2(deltay, deltax);
double headingChange = angleBetween - ptheta;
return headingChange;
}
void Robot::align(double x, double y)
{
ros::Rate loop_rate(10);
double headingChange = calculateHeadingChange(x, y);
double angularv = headingChange / 5;
double heading = ptheta + headingChange;
while (heading > 2 * M_PI)
{
heading -= 2 * M_PI;
}
while (heading < 0)
{
heading += 2 * M_PI;
}
geometry_msgs::Twist msg;
msg.linear.x = 0;
msg.angular.z = angularv;
while (ros::ok())
{
RobotVelocity_pub.publish(msg);
ros::spinOnce();
ROS_INFO("Heading Change %f pthea is %f %f %f", headingChange, ptheta, px, py);
loop_rate.sleep();
}
}
And this is the code that calls the method:
int main(int argc, char **argv) {
ros::init(argc, argv, "robotNode");
Robot r1(5,10);
Robot r2(15,20);
Robot r3(10,30);
Robot r4(25,16);
Robot r5(5,28);
Robot r6(10,10);
Robot Group[6] = {r1, r2, r3, r4 ,r5, r6};
std::vector<Robot> Herd;
int leaderID = r1.selectLeader();
Robot * leader;
for (int i=0;i<6;i++) {
if (Group[i].robot_ID == leaderID) {
leader = &Group[i];
} else {
Herd.push_back(Group[i]);
}
}
(*leader).align(0.0, 0.0); //Problem area
}
The problem is that your array (Group) and vector (Herd) both contain copies of the automatic objects (r1 and friends); so anything you do to those will not affect the originals.
You probably want to work with pointers instead:
Robot * Group[6] = {&r1, &r2, &r3, &r4, &r5, &r6};
std::vector<Robot*> Herd;
In general, you need to be careful not to dereference these pointers after the objects are destroyed; in this case you're fine, since the lifetimes of the array and vector are contained within those of the objects.
It might make sense to make the Robot class uncopyable, to prevent this kind of mistake.
In C++11, you do this by deleting the copy constructor and copy assignment:
Robot(Robot const &) = delete;
void operator=(Robot const &) = delete;
In older language versions, declare them private, with no implementation; or (better still) derive from a base class that does that.
Here's your problem:
Robot Group[6] = {r1, r2, r3, r4 ,r5, r6};
int leaderID = r1.selectLeader();
The group contains copies of the items. You didn't show us the Robot copy constructor, but I assume it assigns a unique ID to the newly constructed Robot. If so, none of the elements in the group will have an ID equal to your leaderID, and thus your leader pointer is never set.
One solution is to make your Group an array of Robot* pointers rather than an array of Robot objects. A similar problem occurs with your Herd of robots.
I've implemented a 3D strange attractor explorer which gives float XYZ outputs in the range 0-100, I now want to implement a colouring function for it based upon the displacement between two successive outputs.
I'm not sure of the data structure to use to store the colour values for each point, using a 3D array I'm limited to rounding to the nearest int which gives a very coarse colour scheme.
I'm vaguely aware of octtrees, are they suitable in this siutation?
EDIT: A little more explanation:
to generate the points i'm repeatedly running this:
(a,b,c,d are random floats in the range -3 to 3)
x = x2;
y = y2;
z = z2;
x2 = sin(a * y) - z * cos(b * x);
y2 = z2 * sin(c * x) - cos(d * y);
z2 = sin(x);
parr[i][0]=x;
parr[i][1]=y;
parr[i][2]=z;
which generates new positions for each axis each run, to colour the render I need to take the distance between two successive results, if I just do this with a distance calculation between each run then the colours fade back and forth in equilibrium so I need to take running average for each point and store it, using a 3dimenrsionl array is too coarse a colouring and I'm looking for advice on how to store the values at much smaller increments.
Maybe you could drop the 2-dim array off and use an 1-dim array of
struct ColoredPoint {
int x;
int y;
int z;
float color;
};
so that the code would look like
...
parr[i].x = x;
parr[i].y = y;
parr[i].z = z;
parr[i].color = some_computed_color;
(you may also wish to encapsulate the fields and use class ColoredPoint with access methods)
I'd probably think bout some kind of 3-d binary search tree.
template <class KEY, class VALUE>
class BinaryTree
{
// some implementation, probably available in libraries
public:
VALUE* Find(const KEY& key) const
{
// real implementation is needed here
return NULL;
}
};
// this tree nodes wil actually hold color
class BinaryTree1 : public BinaryTree<double, int>
{
};
class BinaryTree2 : public BinaryTree<double, BinaryTree1>
{
};
class BinaryTree3 : public BinaryTree<double, BinaryTree2>
{
};
And you function to retreive the color from this tree would look like that
bool GetColor(const BinaryTree3& tree, double dX, double dY, double& dZ, int& color)
{
BinaryTree2* pYTree = tree.Find(dX);
if( NULL == pYTree )
return false;
BinaryTree1* pZTree = pYTree->Find(dY);
if( NULL == pZTree )
return false;
int* pCol = pZTree->Find(dZ);
if( NULL == pCol )
return false;
color = *pCol;
return true;
}
Af course you will need to write the function that would add color to this tree, provided 3 coordinates X, Y and Z.
std::map appears to be a good candidate for base class.