Hello, I'm doing a program and I have been stuck for a while.
Imagine I want to move the blue dot to the red dot through the smallest path, d, by clicking on the screen (red dot).
I tried many different ways but the blue dot won't ever go through the coordinates I pressed, it seems to act kinda randomly.
How do I make it move to where I pressed?
Basically I want to get vx and vy.
Using java.
>Inb4 >>>/g/
I literally have not the foggiest idea.
Try asking /g/ in the programming general.
>>7664657
babby's first rpg walk code
If you're really set against /g/ then stack overflow could be helpful
Piggybacking on the stupid question thread:
Do secondary alcohol molecules form hydrogen bonds with other molecules of the same compound? Or only primary alcohols do that?
>>7664657
if you have the starting point A and the ending point B, then you can describe the line which A and B belong to with the equations: xt=(1-t)*xA+t*xB and yt=(1-t)yA+t*yB, given parameter t.
with d=|AB|, dx=xB-xA, dy=yB-yA, if your desired total speed is v, then vx=v*dx/d and vy=v*dy/d
you don't even need vx and vy to move the blue dot, because by calculating dt=v/d, you can calculate the next position of the dot by putting t'=t+dt and substituting it in the equations above.
this is called linear interpolation, fun stuff, i may also have made some errors here, anyone who can correct me feel free to do so
>>7664782
I think it might be a problem with my code then. The blue dot does not go exactly to where I want in the first click, same direction and constant speed but not the exact coordinates and then if I click in the same spot ,for example, it will switch directions each click.
>>7664657
V E C T O R S
E
C
T
O
R
S
>codemonkeys are stupid
>/sci/ is the smartest board
shake my head to be honest friend
>>7664657
#define SOME_EPSILON (float)0.000001f
#define SOME_SCALER (float)0.5f
class Vector2
{
public:
float x, y;
Vector2() : x(0), y(0) {}
Vector2( const float& X, const float& Y ) : x(X), y(Y) {}
Vector2( const Vector2& v ) : x(v.x), y(v.y) {}
void set( const float& X, const float& Y ) { x=X; y=Y; }
const Vector2& operator + ( const Vector2&a, const Vector2& b ) { return Vector2( a.x + b.x, a.y + b.y ); }
const Vector2& operator - (const Vector2& a, const Vector2& b) { return Vector2( a.x-b.x, a.y-b.y ); }
const Vector2 &operator += (const Vector2 &v) { x += v.x; y += v.y; return *this; }
const Vector2 &operator *= (const float &f) { x *= f; y *= f; return *this; }
void normalize() { float n =1.f / Length(); x *= n; y *= n; z *= n; }
float length() { return (float)sqrt(x*x + y*y + z*z); }
const bool operator == (const Vector2 &a, const Vector2 &b) {
if (fabs(a.x - b.x) < SOME_EPSILON) {
if (fabs(a.y - b.y) < SOME_EPSILON) {
return true;
}
}
return false;
}
const bool operator != (const Vector2 &a, const Vector2 &b) {
return !(a == b); }
}
inside your program...
Vector2 blue_position( blue_x, blue_y );
Vector2 red_position( red_x, red_y );
Vector2 direction = red_position - blue_position;
direction.normalize();
direction *= SOME_SCALER;
while( blue_position != red_position) {
blue_position += direction;
}
>>7668076
EDIT: the z's from normalize() and length() need to be removed since these are 2d vectors
>>7664657
Subtract the bluedot pos from the reddot pos then get arctangent of that then use sin and cos with linear interpolation and add that back to bluedot
>>7664657
>Using java.
>>>/g/tfo