Ich versuche, ein Objekt von A-Punkt zu B-Punkt durch einen Klick zu verschieben. In jedem Frame wird das Objekt um einen Punkt zum Punkt B verschoben. Mein Code funktioniert, aber es tut nicht genau das, was ich will.C++ SDL Winkelbewegung Punkt zu Punkt mit einem Klick
Das ist, was ich meine: ich zwei Code haben, ist dies das erste ...
class tank {
public:
void setMoveTank(tank &_tank, int x, int y) {
_tank.tanke.moving = true;
_tank.tanke.x2 = x;
_tank.tanke.y2 = y;
int vx = _tank.tanke.x2 - _tank.tanke.x;
int vy = _tank.tanke.y2 - _tank.tanke.y;
if(vx==0) vx+=1; if(vy==0) vy+=1; //float core dumperra arreglateko
double alfa = atan2(vy,vx) * 180/M_PI;
_tank.tanke.vx = 2*cos(alfa*(M_PI/180));
_tank.tanke.vy = 2*sin(alfa*(M_PI/180));
}
void renderTank(render _render, tank &_tank) {
if(_tank.tanke.moving) {
_tank.tanke.x += _tank.tanke.vx;
_tank.tanke.y += _tank.tanke.vy;
}
SDL_Rect dst = {(int)(_tank.tanke.x),(int)(_tank.tanke.y),_tank.tanke.spr.w,_tank.tanke.spr.h};
SDL_RenderCopy(_render.ren, _tank.tanke.img, &_tank.tanke.spr, &dst);
}
};
... und tun so etwas wie dieses
I calculate angle once and I move object every frame, but it never arrive to destination
Dies ist mein zweiter Code ...
class tank {
public:
void setMoveTank(tank &_tank, int x, int y) {
_tank.tanke.moving = true;
_tank.tanke.x2 = x;
_tank.tanke.y2 = y;
}
void renderTank(render _render, tank &_tank) {
if(_tank.tanke.moving) {
int vx = _tank.tanke.x2 - _tank.tanke.x;
int vy = _tank.tanke.y2 - _tank.tanke.y;
if(vx==0) vx+=1; if(vy==0) vy+=1; //float core dumperra arreglateko
double alfa = atan2(vy,vx) * 180/M_PI;
_tank.tanke.vx = 2*cos(alfa*(M_PI/180));
_tank.tanke.vy = 2*sin(alfa*(M_PI/180));
_tank.tanke.x += _tank.tanke.vx;
_tank.tanke.y += _tank.tanke.vy;
}
SDL_Rect dst = {(int)(_tank.tanke.x),(int)(_tank.tanke.y),_tank.tanke.spr.w,_tank.tanke.spr.h};
SDL_RenderCopy(_render.ren, _tank.tanke.img, &_tank.tanke.spr, &dst);
}
};
... und tun so etwas wie diese
this code do what I want but I must recalculate de angle each frame
Das Problem ist, dass in dem zweiten Code, tut das Objekt eine rect Linie zeichnen und in dem ersten Code, das Objekt nie die ankommen B Punkt.
Ich bin sicher, dass das Problem in der RUNDE ist, aber wie kann ich es beheben? Was wäre der beste Weg, dies zu machen?
int main() {
if(event.type == SDL_MOUSEBUTTONDOWN) {
switch(event.button.button) {
case SDL_BUTTON_LEFT: _tank.setMoveTank(_tank, event.button.x, event.button.y); break;
}
}
}
SDL_RenderClear(_render.ren);
_tank.renderTank(_render, _tank);
SDL_RenderPresent(_render.ren);
}
}
hiii
EDIT: dies ist der Fixcode
void renderTank(render _render, tank &_tank) {
if(_tank.tanke.moving) {
double vx = _tank.tanke.x2 - _tank.tanke.x;
double vy = _tank.tanke.y2 - _tank.tanke.y;
if(vx==0) vx+=1; if(vy==0) vy+=1; //float core dumperra arreglateko
double alfa = atan2(vy,vx) * 180/M_PI;
_tank.tanke.vx = 2*cos(alfa*(M_PI/180));
_tank.tanke.vy = 2*sin(alfa*(M_PI/180));
_tank.tanke.x += _tank.tanke.vx;
_tank.tanke.y += _tank.tanke.vy;
}
SDL_Rect dst = {(int)(_tank.tanke.x),(int)(_tank.tanke.y),_tank.tanke.spr.w,_tank.tanke.spr.h};
SDL_RenderCopy(_render.ren, _tank.tanke.img, &_tank.tanke.spr, &dst);
}
Vielen Dank @Hedanito !!! Das ist es! Ich kann nicht glauben, dass ich nicht gesehen habe !! –