What's wrong with this functions definitions? I've created this two functions and the second one calls the first one, so what i want is, depending on the if in the first one, the calling of AlignCamera(); will change what happens in AlignXAXis();
void AlignCamera();
{
double cx = ox+(0.5*(xmax-xmin))*sx;
double cy = oy+(0.5*(ymax-ymin))*sy;
double cz = oz+(0.5*(zmax-zmin))*sz;
int vx=0;
int vy=0;
int vz=0;
int nx=0;
int ny=0;
int nz=0;
int iaxis = current_widget->GetPlaneOrientation();
if (iaxis == 0)
{
vz = -1;
nx = ox + xmax*sx;
cx = ox + 256*sx;
}
else if (iaxis == 1)
{
vz = -1;
ny = oy + ymax*sy;
cy = oy + 512*sy;
}
else
{
vy = 1;
nz = oz +zmax*sz;
cz = oz + 64*sz;
}
int px = cx+nx*2;
int py = cy+ny*2;
int pz = cz+nz*3;
vtkCamera *camera=ren->GetActiveCamera();
camera->SetViewUp(vx, vy, vz);
camera->SetFocalPoint(cx, cy, cz);
camera->SetPosition(px, py, pz);
camera->OrthogonalizeViewUp();
ren->ResetCameraClippingRange();
renWin->Render();
}
// Define the action of AlignXAxis
void AlignXAxis();
{
int slice_number;
int po = planeX->GetPlaneOrientation();
if (po == 3)
{
planeX->SetPlaneOrientationToXAxes();
slice_number = (xmax-xmin)/2;
planeX->SetSliceIndex(slice_number);
}
else
{
slice_number = planeX->GetSliceIndex();
}
current_widget= planeX;
ui->horizontalScrollBar->setValue(slice_number);
ui->horizontalScrollBar->setMinimum(xmin);
ui->horizontalScrollBar->setMaximum(xmax);
AlignCamera();
}
the variables needed are defined before it, such as ox, oy, oz....
When i run it it says undefined reference to `AlignCamera()' or 'AlignXAxis()'.
void AlignCamera(); //This is merely a prototype, not a declaration.
Remove the Semicolon, or create a declaration afterwards.
void AlignCamera(); //Prototype
void AlignCamera() { //Declaration
//Do Stuff
}
The same applies for the other function. Remove that semicolon.
Remove the semicolons, so:
void AlignCamera();
{
...
}
becomes
void AlignCamera()
{
...
}
Related
I have a class that has x, y, and mass (which acts as radius) attributes. All of which are floats. I also have this method:
float shrink(float attackerMass) {
float shrinkAmount = attackerMass * GetFrameTime();
mass -= shrinkAmount;
return shrinkAmount;
}
This method is called when another circle is touching the circle, and it shrinks the circle by the right amount (I put an std::cout line underneath mass -= shrinkAmount to test it) but the value of mass is never actually applied to the object. My guess is that I'm somehow changing the value of a copy of my circle object and not the actual referenced one but I have no idea how that'd be happening.
Here's the entire object class if needed (I am using functions from Raylib):
class Blib {
private:
Color color;
Blib* address{ this };
public:
float x;
float y;
float mass;
/* Constructor */
Blib(float x, float y, float mass = 32.0f, Color color = Color{ 255, 255, 255, 200 }) {
this->x = x;
this->y = y;
this->mass = mass;
this->color = color;
}
/* Methods */
void check_collisions(std::vector<Blib> blibs) { //
for (Blib blib : blibs) {
if (CheckCollisionCircles(Vector2{ x, y }, mass, Vector2{ blib.x, blib.y }, blib.mass)) {
if (mass > blib.mass && address != blib.address) {
grow(blib.shrink(mass));
}
}
}
}
void draw() {
DrawCircle(x, y, mass, color);
}
void grow(float amount) {
mass += amount;
}
void move_with_keyboard() {
float speed = 5.0f * mass * GetFrameTime();
if (IsKeyDown(KEY_W)) {
y -= speed;
}
if (IsKeyDown(KEY_A)) {
x -= speed;
}
if (IsKeyDown(KEY_S)) {
y += speed;
}
if (IsKeyDown(KEY_D)) {
x += speed;
}
}
float shrink(float attackerMass) {
float shrinkAmount = attackerMass * GetFrameTime();
mass -= shrinkAmount;
return shrinkAmount;
}
};
Here:
for (Blib blib : blibs)
Simply change to:
for (Blib &blib : blibs)
You want a reference, otherwise you are just changing a temporary variable that disappears at the end of each for loop iteration.
PS: I usually prefer:
for (auto &blib : blibs)
PPS: Your function signature also needs to be a reference:
void check_collisions(std::vector<Blib> &blibs)
I decided to make a multiplayer game in sfml, the map is a text variable
one step lower,! - block with a collider, # - ground.
It looks something like this:
"## !;
! # !;
###;"
I have a special class "Qardrat" that represents a block with a texture, that is, an alternative to a sprite.
class Quardrat {
public:
void spriteSetPosition(int Vx, int Vy) {
sprite.setPosition(Vx, Vy);
}
void LoadTexture(String textureName) {
texture.loadFromFile(textureName);
// RectangleShape tmpSprite(texture);
sprite.setScale(sizeX, sizeY);
sprite.setTexture(&texture);
}
void ShapeMove(int Vx, int Vy) {
sprite.move(Vx, Vy);
//sprite.setPosition(sprite.getPosition().x+Vx, sprite.getPosition().y+Vy);
std::cout << "Сдвинулся на " << Vx << "По x" << std::endl;
}
Quardrat(float x = 0, float y = 0, float sx = 1, float sy = 1, String textureName = "player.png") {
LoadTexture(textureName);
sizeX = sx;
sizeY = sy;
sprite.setPosition(x, y);
sprite.setSize(Vector2f(sx, sy));
}
sf::RectangleShape GetShape() {
return sprite;
}
void DrawShape() {
::window.draw(sprite);
}
float GetSizeX() {
return sizeX;
}float GetSizeY() {
return sizeY;
}
private:
Texture texture;
std::string texutreName = "player.png";
float sizeX = 10;
float sizeY = 10;
//Sprite sprite;
RectangleShape sprite;
};
It is declared as follows: x position, y position, height, width, texture name.
To draw objects, 3 cities are used, which are placed in the square classes:
sloi0, sloi1, player /
here is the drawing code
void DrawOnDisplay(const std::vector<std::reference_wrapper<Quardrat>>& a) {//РИСОВАНИЕ
for (Quardrat i : sloi0)
i.DrawShape();
//::window.draw(i.GetShape());
for (Quardrat i : sloi1)
i.DrawShape();
// ::window.draw(i.GetShape());
for (Quardrat i : a) {
i.DrawShape();
// ::window.draw(i.GetShape());
}
}
And I'll leave the card reading code here, just in case:
for (char block : map) {
if (block == '#') {
Quardrat bl = Quardrat(BlockPosX * StandartBlockSize, BlockPosY * StandartBlockSize, StandartBlockSize, StandartBlockSize);
sloi0.push_back(bl); BlockPosX++;
// }
if (block == '!') {
Quardrat bl = Quardrat(BlockPosX * StandartBlockSize, BlockPosY * StandartBlockSize,
StandartBlockSize / 10, StandartBlockSize / 10, "block.png");
sloi0.push_back(bl); BlockPosX++;
collisions.push_back(bl);
}
if (block == ';') {
BlockPosY++;
BlockPosX = 0;
}
}
And here's the problem - from all the blocks of the map, a texture appears only for one, for all the rest - a white square.
I am trying to make a function for my convex hull project,
Calipers(). I tried creating a class for the points but
I cannot seem to use points to extract a value from p[].x into rotatingPoint.x
Thank you.
#include "point.h"
void Point::Set_value(Point p)
{
x=p.x;
y=p.y;
}
void Point::Set_value(double nx,double ny)
{
x=nx;
y=ny;
}
void comp_geo::Calipers(int n, Point *p)
{
double Cx = &p[n].x;
double Cy = &p[n].y;
rotatePoint.x = Cx;
rotatePoint.y = Cy;
}
Calipers() does not work.
You need to get rid of the & operator:
void comp_geo::Calipers(int n, Point *p)
{
double Cx = p[n].x;
double Cy = p[n].y;
rotatePoint.x = Cx;
rotatePoint.y = Cy;`
}
Which can be simplified to this:
void comp_geo::Calipers(int n, Point *p)
{
rotatePoint.x = p[n].x;
rotatePoint.y = p[n].y;
}
Or even this (assuming rotatePoint is a Point):
void comp_geo::Calipers(int n, Point *p)
{
rotatePoint = p[n];
// or:
// rotatePoint.Set_value(p[n]);
}
On a side note, Point::Set_value(Point) should be taking the Point by const reference instead of by value:
void Point::Set_value(const Point &p)
{
x = p.x;
y = p.y;
}
void comp_geo::Calipers(int n, Point *p)
{
double Cx = **&**p[n].x;
double Cy = **&**p[n].y;
rotatePoint.x = Cx;
rotatePoint.y = Cy;`
}
The ampersand is not needed as it refers to the address of the memory location, rather than the value.
void comp_geo::Calipers(int n, Point *p)
{
rotatePoint.x = p[n].x;
rotatePoint.y = p[n].y;
}
I am attempting to create a graphical representation of finite automata using xcode, and as such I have created classes for states and transitions. In order to make moving objects easy, I have included a collection of pointers of transitions going in and out of the state. Compiling is fines, but when I try to append to the vector, it produces the following error. EXC_BAD_ACCESS(code=1, address=0x3f35)
Following the error takes me to the std library, and shows the error being in this line.
template <class _Tp, class _Allocator>
inline _LIBCPP_INLINE_VISIBILITY
void
vector<_Tp, _Allocator>::push_back(const_reference __x)
{
if (this->__end_ != this->__end_cap())
{
__annotate_increase(1);
__alloc_traits::construct(this->__alloc(),
_VSTD::__to_raw_pointer(this->__end_), __x);
++this->__end_;
}
else
__push_back_slow_path(__x);
}
Here is a simplified version of my State class, my Transition class is declared before and is then defined afterwards.
class State
{
int id;
std::vector<Transition *> links_in;
std::vector<Transition *> links_out;
float x;
float y;
int r = radius; //x, y are centre coordinates of the circle representing the state, while r is the radius
bool is_active = false;
bool is_end = false;
bool is_shown = true;
bool is_moving;
public:
// Get Functions go here
// Set Functions go here
//Add functions
void add_in_trans(Transition * t){
links_in.push_back(t);
}
void add_out_trans(Transition * t){
links_out.push_back(t);
}
//Delete Functions
void remove_in_trans(){
links_in.pop_back();
}
void remove_out_trans(){
links_out.pop_back();
}
void draw_state();
State(int ix, int iy);
State(){}
}
If you have any suggestions for a better way of doing this, I am more then happy to hear them. I have spent all day trying to sort this out, to no avail.
Thanks in advance.
UPDATE:
I attempted to use integers and vectors as a temporary fix, but I came up with the same problem, so I assume that the problem isn't the pointers but the way I'm using vectors.
This is the code
#include <vector>
class Transition;
class State
{
int id;
std::vector<int> links_in;
std::vector<int> links_out;
float x;
float y;
int r = radius; //x, y are centre coordinates of the circle representing the state, while r is the radius
bool is_active = false;
bool is_end = false;
bool is_shown = true;
bool is_moving;
public:
// Get Functions
int get_x(){
return x;
}
int get_y(){
return y;
}
int get_id(){
return id;
}
bool is_it_active(){
return is_active;
}
bool is_it_moving(){
return is_moving;
}
bool is_in(int ix, int iy){ //Function to tell if pair of coordinates are in the circle, used to select.
std::cerr << ix-x << " " << iy-y << " " << r*r << std::endl;
if ((ix-x)*(ix-x) + (iy-y)*(iy-y) < r*r)
return true;
else
return false;
}
// Set Functions
void set_active(bool s){
is_active = s;
}
void set_moving(bool s){
is_moving = s;
}
void end_switch(){
is_end = !is_end;
}
void set_start(){
g_start_state = id;
}
void set_x(int ix){
x = ix;
}
void set_y(int iy){
y = iy;
}
//Add functions
void add_in_trans(int t){
links_in.push_back(t);
}
void add_out_trans(int t){
links_out.push_back(t);
}
//Delete Functions
void remove_in_trans(){
links_in.pop_back();
}
void remove_out_trans(){
links_out.pop_back();
}
void draw_state();
State(int ix, int iy);
State(){}
};
State::State(int ix, int iy){
id = g_state_num;
if (g_start_state == 0)
g_start_state = id;
x = ix;
y = iy;
}
void State::draw_state(){
if (is_shown){
if (is_moving)
glTranslatef(g_cursor_x, g_cursor_y, 0.0);
else
glTranslatef(x, y, 0.0);
fill_colour();
if (is_active)
active_fill_colour();
glBegin(GL_POLYGON);
for (size_t i=0; i<24; i++){
float n[2] = {static_cast<float>(r * cos(i*6)), static_cast<float>(r * sin(i*6))};
glVertex2fv(n);
}
glEnd();
line_colour();
glBegin(GL_LINES);
for (size_t i=0; i<24; i++){
float n[2] = {static_cast<float>(r * cos(i*6)), static_cast<float>(r * sin(i*6))};
glVertex2fv(n);
}
glEnd();
if(is_end){
glPushMatrix();
glScalef(0.9, 0.9, 0.9);
for (size_t i=0; i<24; i++){
float n[2] = {static_cast<float>(r * cos(i*6)), static_cast<float>(r * sin(i*6))};
glVertex2fv(n);
}
glPopMatrix();
}
text_colour();
std::string s = std::to_string(id);
for (int i=0; i<s.length(); i++){
glPushMatrix();
glTranslatef(-radius/2 + i*kerning, -radius/2, 0.0);
glScalef(0.3, 0.3, 1.0);
glutStrokeCharacter(GLUT_STROKE_ROMAN, s[i]);
glPopMatrix();
}
}
}
class Character{
int id;
char c;
public:
int get_id(){
return id;
}
char get_char(){
return c;
}
void set_char(char ic){
c = ic;
}
Character(char ic);
Character(){};
};
Character::Character(char ic){
id = g_character_num;
g_character_num++;
c = ic;
}
class Transition{
int ident;
State * from_state;
State * to_state;
float from[2];
float to[2];
Character c;
public:
void set_from(float x, float y){
from[0] = x;
from[1] = y;
}
void set_to(float x, float y){
to[0] = x;
to[1] = y;
}
void set_char(Character ic){
c = ic;
}
int get_id(){
return ident;
}
void draw_trans();
void set_trans(State * ifrom, State * ito, Character ic){
from_state = ifrom;
to_state = ito;
from[0] = ifrom->get_x();
from[1] = ifrom->get_y();
to[0] = ito->get_x();
to[1] = ito->get_y();
c = ic;
}
Transition(){};
Transition(State ifrom, State ito, Character ic){
from_state = &ifrom;
to_state = &ito;
from[0] = ifrom.get_x();
from[1] = ifrom.get_y();
to[0] = ito.get_x();
to[1] = ito.get_y();
c = ic;
}
};
void Transition::draw_trans(){
line_colour();
glBegin(GL_LINES);
glVertex2fv(from);
glVertex2fv(to);
glEnd();
float grad = (from[0] - to[0]) /(from[1] - to[1]); //(By finding the gradient of the slope, we can fin good place to show it's information, it's character.
if (grad < -1 || grad > 1){
glPushMatrix();
glTranslatef(from[0] - to[0] - 20, from[1] - to[1], 1.0);
}
else{
glPushMatrix();
glTranslatef(from[0] - to[0], from[1] - to[1] + 20, 1.0);
}
glutStrokeCharacter(GLUT_STROKE_ROMAN, (c.get_char()));
glPopMatrix();
}
LeftCollision in CheckCollision() goes true, when ever Object1 collides with Object2(square which is scrolling from right to left of screen) left side. But in the GameObject::Update()
Leftcollision never updates to True, though it changes to true in the CheckCollision section!!
What i want is, whenever the LeftCollision is true, Update should stop, until it becomes false again!!
Below is the code for Header file and CPP file!!
The problem is with LeftCollision updation!! Why is the value not reflected in the Update if condition!!
#pragma once
#include <iostream>
#include <allegro5/allegro5.h>
#include <allegro5/allegro_primitives.h>
#include "Globals.h"
class GameObject
{
private :
int ID;
bool alive;
bool collidable;
protected :
float velX;
float velY;
int dirX;
int dirY;
int boundX;
int boundY;
int maxFrame;
int curFrame;
int frameCount;
int frameDelay;
int frameWidth;
int frameHeight;
int animationColumns;
int animationDirection;
ALLEGRO_BITMAP *image;
public :
float x;
float y;
bool LeftCollision;
bool pause;
GameObject();
void virtual Destroy();
void Init(float x, float y, float velX, float velY, int dirX, int dirY, int boundX, int boundY);
void virtual Update();
void virtual Render();
float GetX() {return x;}
float GetY() {return y;}
void SetX(float x) {GameObject ::x = x;}
void SetY(float y) {GameObject ::y = y;}
int GetBoundX() {return boundX;}
int GetBoundY() {return boundY;}
int GetID() {return ID;}
void SetID(int ID) {GameObject::ID = ID;}
bool GetAlive() {return alive;}
void SetAlive(bool alive) {GameObject :: alive = alive;}
bool GetCollidable() {return collidable;}
void SetCollidable(bool collidable) {GameObject :: collidable = collidable;}
bool CheckCollisions(GameObject *otherObject);
void virtual Collided(int objectID);
bool Collidable();
};
#include "GameObject.h"
GameObject :: GameObject()
{
x = 0;
y = 0;
velX = 0;
velY = 0;
dirX = 0;
dirY = 0;
boundX = 0;
boundY = 0;
LeftCollision = false;
maxFrame = 0;
curFrame = 0;
frameCount = 0;
frameDelay = 0;
frameWidth = 0;
frameHeight = 0;
animationColumns = 0;
animationDirection = 0;
image = NULL;
alive = true;
collidable = true;
}
void GameObject :: Destroy()
{
}
void GameObject :: Init(float x, float y, float velX, float velY, int dirX, int dirY, int boundX, int boundY)
{
GameObject :: x = x;
GameObject :: y = y;
GameObject :: velX = velX;
GameObject :: velY = velY;
GameObject :: dirX = dirX;
GameObject :: dirY = dirY;
GameObject :: boundX = boundX;
GameObject :: boundY = boundY;
}
void GameObject :: Update()
{
if(LeftCollision == false)
{
x += velX * dirX;
y += velY * dirY;
}
}
void GameObject :: Render()
{
}
bool GameObject :: CheckCollisions(GameObject *otherObject)
{
float oX = otherObject->GetX();
float oY = otherObject->GetY();
int obX = otherObject->GetBoundX();
int obY = otherObject->GetBoundY();
if( x + boundX > oX - obX &&
x - boundX < oX + obX &&
y + boundY > oY - obY &&
y - boundY < oY + obY && otherObject->GetID() == TRIANGLE)
{
return true;
}
else if(((oX < x + boundX + 14)&&(oX+ 40 >x - boundX))&&!((oY < y + boundY)&&(oY+40 > y - boundY))
&& otherObject->GetID() == SQUARE)
{
y = oY - boundX - 10;
//x = oX + 40;
return true;
}
else if((oX < x + boundX + 14) && (oX > x - boundX) && otherObject->GetID() == SQUARE)
{
LeftCollision = true;
return false;
}
else
{
return false;
LeftCollision = false;
}
}
void GameObject :: Collided(int objectID)
{
}
bool GameObject :: Collidable()
{
return alive && collidable;
}
Is this part of your problem - returning without setting the value
else
{
return false;
LeftCollision = false;
}
The only thing that jumps to mind is that you've got more than one object going around and you're not always using the one you expect. Try putting something like:
printf("here(%s:%d) this=%p\n", __FILE__, __LINE__, this);
at the start of each method to make sure that the this point is what you think it is. The problem may lie in the calling code that you haven't shown.