• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Tommy Boy

Move Object towards another object? Correctly.

4 posts in this topic

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.

Share this post

Link to post
Share on other sites

Sorry this is probably going to be stupid question, but how do I normalize 2D vector?

Is it dividing the X and Y by the length of the line?


Share this post

Link to post
Share on other sites

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


Share this post

Link to post
Share on other sites

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



first define a 2D vector:

class vec2
    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


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);



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 tongue.png

Edited by Kaptein

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0