I just can't seem to solve this.
Heres what I want to do:
I want my program to calculate movement, from one point to another.
The function _updatepos is the main function, where I have troubles. It is supposed to, as the name says, update the position (posx and posy), and move it a bit closer to the destination (desx and desy);
I would be very thankful if you could help me, thanks in advance!
#include <iostream>
#include <windows.h>
#include <time.h>
using namespace std;
time_t current_time;
double posx = 10000;
double posy = 10000;
double desx = 0;
double desy = 0;
int speed = 520;
double getDistance(double x1, double y1, double x2, double y2) {
return (((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1))/((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1))); //no need for maths.h....
}
double timeNeeded() {
return getDistance(posx,posy,desx,desy)/speed;
}
void _updatepos() { //******THIS*********
if((desx>0)&&(desy>0)) {
//just cant think of a way to solve this....
double timeneeded = timeNeeded();
double xleft = desx-posx;
double yleft = desy-posy;
double incx = xleft/timeneeded;
double incy = yleft/timeneeded;
time_t isthere = clock();
posx = posx + (incx*(isthere-current_time));
posy = posy + (incy*(isthere-current_time));
}
}
void stopmov() {
if((posx==desx)&&(posy==desy)) {
desx = 0;
desy = 0;
}
}
void updatePos() {
current_time = clock();
_updatepos();
stopmov();
}
void moveTo(int x, int y) {
desx = x;
desy = y;
}
int main() {
moveTo(2000,1000);
while(true) {
updatePos();
//cout << posx << ", " << posy << endl;
}
cin.get();
return 0;
}