Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Need a little help with the math behind a rasterized line for a tile engine...

Author
Message
BuccaneerBob
AGK Developer
14
Years of Service
User Offline
Joined: 18th Aug 2010
Location: Canada
Posted: 17th Jan 2019 06:55 Edited at: 17th Jan 2019 06:57
Hey everyone,

Looking for a little help or suggestions/pointers.

I have a tile engine build with App Game Kit, it renders a 400x200 tile map (tiles are 60x60). I did some fancy foot work so that to cut down on battery drain, there's only Screen Width / 60 number of actual tiles on the screen and as you go left/right on the map, it simply updates the "View" to the textures of the offset we're looking at, I'm quite impressed with that, anyway, that's just me bragging. What I'm trying to work out, is rasterized line movement.

The way it works, is based on a tick count (when the tick = 100) if a "Troop" has a Destination X/Y tile that is greater than their current X/Y position, it moves them one step closer to the destination X/Y. The way I build it right now is simply this



The problem here is that the troop moves diagonal until the x or y is the same, then they walk in a straight line to the destination tile.

What I'm looking to do is something similar to this:



Here's my entire move function, what I suspect I'd have to do is do the math for the invisible line from Start X, Start Y to End X, End Y, find out where the next tile works out to based on that line and then move the troop the proper direction. However, it's apparent I just stink at math!

Robert Janes (Samu Games)
http://www.samugames.com/artifact
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 17th Jan 2019 07:12 Edited at: 17th Jan 2019 07:14
I think you need to calculate a slope.
First of all you will need to use floats not integers

slope.x = target.x - current.x
slope.y = target.y - current.y

Then you can divide the slope by the number of steps you want to get to your destination.

step.x = slope.x / totalSteps
step.y = slope.y / totalSteps

Then you add the step value to the troop

inc troop.x, step.x
inc troop.y, step.y

Is that what you are after?
puzzler2018
User Banned
Posted: 17th Jan 2019 07:13
Hi,

I did a sprite creator with a line feature in there too, here is my algo



Thread to this is

https://forum.thegamecreators.com/thread/223450

Hope this helps for now - others may have better algo's too
BuccaneerBob
AGK Developer
14
Years of Service
User Offline
Joined: 18th Aug 2010
Location: Canada
Posted: 17th Jan 2019 07:15
You might be onto something there blink0k, though I perhaps am not getting the total steps part or how I'd factor that into it, I'm guessing total steps would be the distance / time to travel from Point A to Point B or something similar to that? The slope part makes total sense, don't know why I didn't think of that!
Robert Janes (Samu Games)
http://www.samugames.com/artifact
BuccaneerBob
AGK Developer
14
Years of Service
User Offline
Joined: 18th Aug 2010
Location: Canada
Posted: 17th Jan 2019 09:13 Edited at: 17th Jan 2019 09:13
Got it solved with

Robert Janes (Samu Games)
http://www.samugames.com/artifact

Login to post a reply

Server time is: 2024-11-23 08:31:55
Your offset time is: 2024-11-23 08:31:55