Hi everybody

I'm trying to move one object towards position where I have clicked. for example

Started by Jul 19 2013 01:29 PM

,
4 replies to this topic

Posted 19 July 2013 - 01:29 PM

Hi everybody

I'm trying to move one object towards position where I have clicked. for example

if (Unicorn->getX() < ClickedPosX)

{

Unicorn->setX( Unicorn->getX() + 5);

}

if (Unicorn->getX() > ClickedPosX)

{

Unicorn->setX( Unicorn->GetX() - 5);

}

if (Unicorn->getY() > ClickedPosY)

{

Unicorn->setY( Unicorn->getY() + 5);

}

if (Unicorn->getY() < ClickedPosY)

{

Unicorn->setY( Unicorn->getY() - 5);

}

This works but doesn't go straight to that point, here is a picture representing what happens when game runs

I know this happens because I am adding 5 which is the speed. I don't know how to calculate what to add, so he would walk towards that direction. I'm guessing I have to find angle between those two points and somehow use that?

Thanks guys, this is not homework or anything just doing it for fun and learning.

Posted 19 July 2013 - 01:42 PM

POPULAR

If you want to do this on integer coordinates, with integer math, look up Bresenham's line algorithm. Otherwise some linear algebra will be useful. If you want to move at a specific speed, make a vector from the current position to the desired position and normalize it, that will give you a unit vector in the direction towards the target and you can just multiply it by the desired speed to move the object. If you want to move at a specific time, an easier approach is just to linearly interpolate between the current and the target position.

Posted 19 July 2013 - 02:09 PM

Otherwise some linear algebra will be useful. If you want to move at a specific speed, make a vector from the current position to the desired position and normalize it, that will give you a unit vector in the direction towards the target and you can just multiply it by the desired speed to move the object.

Got it working using that tactic Thank you Brother bob

Posted 19 July 2013 - 02:37 PM

the easiest thing is to make positions floats, then you can move by a fractional amount as well as integral, which is what happens when you move slowly

its never a good idea to use integral values for positions, unless perhaps you internally have precision, but render using integers

anyways,

first define a 2D vector:

class vec2 { public: float x, y; vec2() : x(0), y(0) {} // initialize (x, y) to (0, 0) vec2(float v) : x(v), y(v) {} // (x, y) to (v, v) // initialize both vec2(float X, float Y) : x(X), y(Y) {} float length() const { // length in euclidian distance: return sqrtf(x*x + y*y); } void normalize() { // make a vectors length be equal to exactly 1: float L = length(); if (L != 0.0) { x /= L; y /= L; } } };

of course, this 2D vector (vec2) is severely lacking in pretty much all aspects of what it should be able to do

however,

let's say you have 2 points in space (x1, y1) and (x2, y2)

both of these are vectors:

vec2 v1 = vec2(x1, y1); // or vec2 v1(x1, y2) if you want

vec2 v2 = vec2(x2, y2);

then, the direction-vector pointing exactly towards a point (x2, y2) is

the normalized vector of the difference between v2 and v1:

vec2 v3 = vec2(v2.x - v1.x, v2.y - v1.y);

v3.normalize();

now we have a vector that points towards our point.

to move towards the new point at a certain speed, multiply the new vector by the speed, or "scaling factor" if you will

float speed = 0.5f;

v3.x *= speed;

v3.y *= speed;

now we will move the unicorn (was it?) towards the new point at our determined speed:

unicorn.x += v3.x;

unicorn.y += v3.y;

hope this helps

i wrote all the code directly in here, so it might be flawed

oops, i see you already got it working

**Edited by Kaptein, 20 July 2013 - 05:20 AM.**