• 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
toony

Calculating how much to move to some position

18 posts in this topic

I wrote some lines of code to calculate the exact number of pixels needed to increment the position of moving objects (each time around the game loop) in order to get the object to some "x, y" position like "224, 329".

Here is my aproach:
[code]
// float orig[2], dest[2]; // [0] = x, [1] = y positions

// path calculation
if (dest[0] >= dest[1]) {
orig[0] += dest[0] / (dest[1] + (dest[0]-dest[1]));
orig[1] += dest[1] / dest[0];
} else {
orig[0] += dest[0] / dest[1];
orig[1] += dest[1] / (dest[0] + (dest[1] - dest[0]));
}
[/code]

Is this approach clear enough and efficient in your opinion? The code isn't 100%, it's just a glimpse of the full code but this part is the juicy part !!

This code would be used to move a player by clicking on the ground where you want to move, like you see in games like baldurs gate for example.Try [url="http://www.mediafire.com/?z662k7e6c26fi8b"]this[/url] program to see what the code does.
0

Share this post


Link to post
Share on other sites
I really don't follow your math here.

The typical formula is this:

new_position = old_position + (speed * (target_position - old_position))

Repeat on X, Y, Z axes.


Your math seems to exhibit weirdness, and mixes axes, which I honestly can't unravel...
2

Share this post


Link to post
Share on other sites
Its a bit hard to get whats going on, somehting liek this is easier to follow:

[code]float dir[2];
dir[0] = dest[0] - orig[0]
dir[1] = dest[1] - orig[1];

// You could clamp them to limit speed or something
dir[0] = clamp(dir[0], -1, 1); // max or 1 pixel in x direction
dir[1] = clamp(dir[1], -1, 1); // max or 1 pixel in y direction
// obviously you can now go faster diagonally

orig[0] += dir[0];
orig[1] += dir[1];[/code]

I'd suggest implementing a basic Vector class with a few functions, subtraction, addition, scaling and normalising. Then you can do:

[code]float maxSpeed = speedPerSecond * time; // time since last frame
Vector dir = dest - orig;
dir.normalize();
dir *= maxSpeed;
orig += dir;[/code]
0

Share this post


Link to post
Share on other sites
i find it strange that to me your code looks hard to understand. In my code i'm just trying to perfectly move something to another position. And speed/velocity variables contain values that will normaly move ahead of the destination i need. I'll take a closer look at your codes and see where that gets me. And my code might seem weird, but it works and for some reason my code looks easy to figure out for me.
0

Share this post


Link to post
Share on other sites
Err ...[quote name='toony' timestamp='1309449007' post='4829628']
...
[code]
// float orig[2], dest[2]; // [0] = x, [1] = y positions

// path calculation
if (dest[0] >= dest[1]) {
orig[0] += dest[0] / (dest[1] + (dest[0]-dest[1]));
orig[1] += dest[1] / dest[0];
} else {
orig[0] += dest[0] / dest[1];
orig[1] += dest[1] / (dest[0] + (dest[1] - dest[0]));
}
[/code]
[/quote]
orig[0] + dest[0] / (dest[1] + (dest[0]-dest[1]))
= orig[0] + dest[0] / (dest[1] + dest[0] - dest[1])
= orig[0] + dest[0] / (dest[0])
= orig[0] + 1
and similarly
orig[1] + dest[1] / (dest[0] + (dest[1] - dest[0]))
= orig[1] + 1

Hence this is not "clear code". Perhaps it is efficient, but only if the compiler is smart enough to find the shorter version.
0

Share this post


Link to post
Share on other sites
[quote name='ApochPiQ' timestamp='1309452901' post='4829649']
I really don't follow your math here.

The typical formula is this:

new_position = old_position + (speed * (target_position - old_position))

Repeat on X, Y, Z axes.


Your math seems to exhibit weirdness, and mixes axes, which I honestly can't unravel...
[/quote]

new_position = old_position + (speed * (target_position - old_position))
step1: new_position = 100 + (1.0f * (338-100))
step2: = new_position = 338
Now im flying!

So much useful info in this forum! mostly from the mod team...
-4

Share this post


Link to post
Share on other sites
[quote]new_position = old_position + (speed * (target_position - old_position))
step1: new_position = 100 + (1.0f * (338-100))
step2: = new_position = 338
Now im flying![/quote]


target_position - old_position needs to be normalized before scaling for speed, it'll end up being eithe -1, 1 or 0 which will work out ok with speed. It makes more sense if these are vectors rather than just numbers.

new_position = old_position + (speed * normalize(target_position - old_position))
step1: new_position = 100 + (1.0f * normalize(338-100))
step2: new_position = 100 + (1.0f * normalize(238))
step3: new_position = 100 + (1.0f * 1))
step4: = new_position = 101

if speed was 10, you'd be at 110 now which makes sense.
1

Share this post


Link to post
Share on other sites
Ok the code will cap the number to the nearest -1 to 1, but the code will cause the object to move in zigzag form. Thats why my method is important to me. Well i have to have some sort of a divider variable to make sure the object moves in a strait line. I must have something that holds the number of increments it will take to get to the desired destination.I already have it working just wanted to see other approaches, not silliness.
-3

Share this post


Link to post
Share on other sites
So you you only wanted to move 1 tile at a time down only one axis at a time (the longest axis)? Or have I misunderstood?
1

Share this post


Link to post
Share on other sites
[quote name='toony' timestamp='1309456095' post='4829685']

So much useful info in this forum! mostly from the mod team...
[/quote]

You ask for help, then berate the guys trying to help you. That's called being a dick.
2

Share this post


Link to post
Share on other sites
People are trying to [i]help[/i] you here. Being dismissive, rude, and calling it "silliness" is not really polite behaviour. It sure as hell doesn't make me interested in helping you any further.
1

Share this post


Link to post
Share on other sites
[quote name='Nanoha' timestamp='1309459373' post='4829711']
So you you only wanted to move 1 tile at a time down only one axis at a time (the longest axis)? Or have I misunderstood?[/quote]
No, i wanted to move from lets say my position (x: 100, y: 136) to (x: 330, y: 542) "not instantly" in a straight line. try the linked program i provided on this topic and you know what im trying to do.


[quote name='ApochPiQ' timestamp='1309459723' post='4829714']
People are trying to [i]help[/i] you here. Being dismissive, rude, and calling it "silliness" is not really polite behaviour. It sure as hell doesn't make me interested in helping you any further.[/quote]
Fair enough, but i dont call providing wrong information help (i call it silliness)... My first message was fairly clear to understand, i also provided a working program example of my goal.
-3

Share this post


Link to post
Share on other sites
[quote name='toony' timestamp='1309458997' post='4829708']
Ok the code will cap the number to the nearest -1 to 1, but the code will cause the object to move in zigzag form. Thats why my method is important to me. Well i have to have some sort of a divider variable to make sure the object moves in a strait line. I must have something that holds the number of increments it will take to get to the desired destination.I already have it working just wanted to see other approaches, not silliness.
[/quote]

Your code is crap, you have no speed component (except 1 pixel per frame). The object's actual speed, in your code, is dependent on the speed of the computer it is running on, unless you're capping the framerate of your game.

You should have something like one of these methods (general methods, to get point across),
[code]

// variables are floats, CurrentLocation and DestinationLocation are points (with an x and y component)

// Get the distance between 2 points
Distance = GetDistance(CurrentLocation, DestinationLocation);

// compute the time it would take the object to get there based on it's current speed (in pixel's per second)
Time = Distance/Speed;

SpeedX = (DestinationLocation.x - CurrentLocation.x)/Time;
SpeedY = (DestinationLocation.y - CurrentLocation.y)/Time;

// move my current location
CurrentLocation.x += SpeedX;
CurrentLocation.y += SpeedY;

[/code]

That's the easy, conceptual, way. The proper way is to use trig functions:

[code]
Angle = atan2((DestinationLocation.y - CurrentLocation.y), (DestinationLocation.x - CurrentLocation.x)) * 180 / PI;


CurrentLocation.x += cos(Angle) * Speed;
CurrentLocation.y += sin(Angle) * Speed;

[/code]

But, this is just silliness. Continue using what you know is the right way.
0

Share this post


Link to post
Share on other sites
Let me guess, Distance = DestinationLocation.x - CurrentLocation.x + DestinationLocation.y - CurrentLocation.y? Cant check the code if i dont know what's going on inside GetDistance(). My code shows you up front how i calculate the speed.
[code]orig[...] += "everything on this side is the speed!"; [/code]
-3

Share this post


Link to post
Share on other sites
[quote name='toony' timestamp='1309462636' post='4829741']
Let me guess, Distance = DestinationLocation.x - CurrentLocation.x + DestinationLocation.y - CurrentLocation.y? Cant check the code if i dont know what's going on inside GetDistance(). My code shows you up front how i calculate the speed.
[/quote]

No, wrong guess.

Have you had your 9th grade geometry class yet? Heard of pythagorean theorem? A^2 + B^2 = C^2? You want C, where A is (DestX - OrigX), B is (DestY - OrigY). There are more efficient ways of calculating distance besides having to do a square root, but a smart guy like should've known them all.

And, again, your "speed" is always 1 pixel per frame or less. You can't control the speed of different objects with your code. What will you do for a "fast" object, versus a "slow" object.


0

Share this post


Link to post
Share on other sites
[font="Courier New"][size="4"]A^2 + B^2 = C^2 doesnt make u smart, not by a long shot star (read star in revers order)! How about you get your lilprick outurass and into your ignorant mouth then smell your breath. That alone should give you more entertainment than the bs u post on random topics. Thanks for the thumbs down, too bad they didnt program the forum to allow rates below 0! Like i give a pile of shit about some pricks rating me down. It's simple you guys are a bunch of woofters and have no life, only the idiotic crap like rating random people down and providing wrong info. [/size][/font]
-10

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  
Followers 0