Customize your cookie preferences

We respect your right to privacy. You can choose not to allow some types of cookies. Your cookie preferences will apply across our website.

We use cookies on our site to enhance your user experience, provide personalized content, and analyze our traffic.

[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Stupid question vx vy

You are currently reading a thread in /sci/ - Science & Math

Thread replies: 13
Thread images: 2
File: halp.png (10 KB, 1138x724) Image search: [Google]
halp.png
10 KB, 1138x724
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
>>
File: sci's programming.png (289 KB, 604x1640) Image search: [Google]
sci's programming.png
289 KB, 604x1640
>>7664657
>Using java.

>>>/g/tfo
Thread replies: 13
Thread images: 2

[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at imagescucc@gmail.com with that post and thread number and it will be removed as soon as possible.
If a post contains illegal content, please click on its [Report] button and follow the instructions.
This is a 4chan archive - all of the content originated from them. If you need information for a Poster - you need to contact them.
This website shows only archived content and is not affiliated with 4chan in any way.
If you like this website please support us by donating with Bitcoin at 1XVgDnu36zCj97gLdeSwHMdiJaBkqhtMK