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;
}``````
2
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by Tvirusx1

Things happen so fast inside the computer these days that the difference between isthere and current_time may be effectively zero making the update to posx and posy essentially zero as well.

So how could I solve this?

If it's the source of your problem then you have several options. I'd probably have the program do something inane for a period of time, then come back for the real thing.

``````double incy = yleft/timeneeded;
long i = 0;
long target = 10000;
while(i < target)
++i;
time_t isthere = clock();``````

Make the pause long enough to make a difference and "prove" it's the problem. Then cut the duration down to meet your needs.

You could also look into sleep() or pause(), or versions thereof.

Edited by Lerner: n/a

Ok, thanks alot for your help and reply, but it seems like i had so solve it in a different way.

This code works perfectly + it can update the movement of up to 200 npcs and palyers (can be increased t any time), and if the desx and desy change while moving it will re-calculate accordingly :D.

This might come in handy for some game developers, (or bot makers like myself).

So heres the Code:

``````#include <iostream>
#include <windows.h>
#include <time.h>
using namespace std;

time_t current_time;

time_t isthere[100];
double posx[200];
double posy[200];
double posxbuf[200];
double posybuf[200];
double desx[200];
double desy[200];
double desxbuf[200];
double desybuf[200];
double incx[200];
double incy[200];
bool moving[200];

double timeneeded[200];

int speed[200];

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(double x1, double y1, double x2, double y2,double speed) {
return (getDistance(x1,y1,x2,y2)/(speed*10));
}

DWORD WINAPI thread(LPVOID lpData) {
while(true) {
for(int i = 0; i<200; i++) {
if((desx[i]!=0)&&(desy[i]!=0)) {
if(moving[i] == true) {
posx[i] = posx[i] + (incx[i]*(current_time-isthere[i]));
posy[i] = posy[i] + (incy[i]*(current_time-isthere[i]));
}
timeneeded[i] = timeNeeded(posx[i],posy[i],desx[i],desy[i],speed[i]);
desxbuf[i] = desx[i];
desybuf[i] = desy[i];
incx[i] = (desxbuf[i]-posx[i])/timeneeded[i];
incy[i] = (desybuf[i]-posy[i])/timeneeded[i];
isthere[i] = clock();
desx[i] = 0;
desy[i] = 0;
moving[i] = true;
}
if(moving[i] == true) {
posxbuf[i] = posx[i]+(incx[i]*(current_time-isthere[i]));
posybuf[i] = posy[i]+(incy[i]*(current_time-isthere[i]));
if((current_time-isthere[i])>=timeneeded[i]) {
moving[i] = false;
cout << posxbuf[i] << ", " << posybuf[i] << endl;
}
}
}
Sleep(5);
}
return 0;
}

int main() {
posx[0] = 1000;
posy[0] = 1000;
desx[0] = 12000;
desy[0] = 6000;
speed[0] = 540;
moving[0] = false;

posx[1] = 5000;
posy[1] = 6200;
desx[1] = 4080;
desy[1] = 7000;
speed[1] = 320;
moving[1] = false;

while(true) {
current_time = clock();
Sleep(5);
}
cin.get();
return 0;
}``````
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.