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 / Point to point algorithm

Author
Message
Atreyu
12
Years of Service
User Offline
Joined: 16th Oct 2012
Location: Australia
Posted: 16th Oct 2012 16:24
Hi all
Need some help with the \"point a to point b\" algorithm.
Please don\'t cut/paste Bresenham\'s line alg, although this is the idea I need.
Let\'s say I have currentX and currentY and want to move to newX and newY. Can anyone provide AppGameKit Tier 1 code to loop through this iteration.
Have tried without success. To give you a head start on my thoughts I have been using abs to check the distances between points and establish which direction requires a \'dot\' something movement and the other a 1 movement. Please help!
Jimmanator
14
Years of Service
User Offline
Joined: 12th Nov 2010
Location:
Posted: 16th Oct 2012 18:29
do you mean like this:

Marl
13
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 16th Oct 2012 19:43 Edited at: 16th Oct 2012 19:45
That's one way to do it, but it depends on how it will be used.

If your app doesn't need the polar coordinates (angle and radius), a basic linear move will do the trick and is a lot less maths intensive.

Headcoded so may need work
lilpissywilly
AGK Developer
14
Years of Service
User Offline
Joined: 10th Sep 2010
Location: Office Chair
Posted: 17th Oct 2012 01:47


I assume it's a sprite you want to move?
This will move sprite.id to screenToWorldX(4.4) and screenToWorldY(96.6) in this example. Change those numbers to where you want to move it, remove the screenToWorld if you want to move it to a world position. Also the / 10 will define how fast it moves, so you can change that around until it feels nice. Just loop this until it's reached the destination.

My hovercraft is full of eels
Atreyu
12
Years of Service
User Offline
Joined: 16th Oct 2012
Location: Australia
Posted: 21st Oct 2012 05:34
Thankyou all for your replies. I have the code working . One small prob- the speed seems to be platform dependant ie runs slightly faster on the pc than on the Android phone. Marl - you mentioned movement in seconds although you are not referencing time - what did you mean by this?
Atreyu
12
Years of Service
User Offline
Joined: 16th Oct 2012
Location: Australia
Posted: 21st Oct 2012 06:15
Sorry, just noticed the getFrameTime. Marl, one problem with your code for my use is that the speed is in relation to the distance travelled, rather than a constant speed at any distance. Jimmantor thanks for the post; some nice trig there.
Atreyu
12
Years of Service
User Offline
Joined: 16th Oct 2012
Location: Australia
Posted: 21st Oct 2012 06:21 Edited at: 22nd Oct 2012 13:45
Here's my code. To explain, I scroll the viewport with player in centre until player reaches an edge then the actual sprite moves to the edge. I have only posted one of the walk functions but you can imagine the other 3 are similar.
Cheers

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 21st Oct 2012 09:32 Edited at: 21st Oct 2012 09:33
Quote: "Marl - you mentioned movement in seconds although you are not referencing time - what did you mean by this?"

He's making reference to GetFrameTime() which returns the number of seconds it took to process and render the previous frame. He then subtracts this number from timeLeft# (which is originally equal to moveTime#) and then move's the sprite proportionally to this time. So the longer it takes to render the screen, the more the sprite moves to make up for the delay. I hope that makes sense. It's pretty clever in my opinion.

AgentSam
12
Years of Service
User Offline
Joined: 14th Mar 2012
Location: Virtual Space
Posted: 28th Jan 2013 15:42 Edited at: 28th Jan 2013 15:50
Atreyu,

Have you taken a look at the sprite functions included in The Useful Community Functions Project. Specifically those in the "Sprites.agc" file.

It sounds to me like you ought to take a look at the following functions:

function moveSprite(spriteID, amount#)
* Move a sprite the specified number of pixels toward the sprites current heading.

function pointSprite(spriteID,x#,y#)
* Points (turns) a sprite at a particular point in space.

Using these together you can easily make a sprite move at a constant velocity toward any point. You call "pointSprite" first, to set the sprite angle (heading), and then call "moveSprite" to make it move in that direction. And of course, you'll have to use #include "Sprites.agc" to pull in the include file into your main module after copying it to your project folder, or just copy/paste the functions from sprites.agc.

(I have a feeling that the "moveSprite" function could be optimized further, since it's currently relying on a lot of trig calculations.)

Cheers,
AgentSam

Login to post a reply

Server time is: 2024-11-27 23:53:07
Your offset time is: 2024-11-27 23:53:07