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.

Newcomers DBPro Corner / Move circle from one point to another.

Author
Message
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 31st Jan 2013 17:03 Edited at: 31st Jan 2013 17:05
I want to use circles to represent people and i want those people to pick a point somewhere and then go to it (preferably in a straight line)
Now i got this far:



But this doesnt really do anything. Just defines a person and puts them somewhere on "the map". I cant think how to make them choose a point to go to (individually different for each person) then make them slowly "walk" that way to that point.

Basically trying to replicate a busy town so i will no doubt need collision to consider later so they dont go through buildings or each other but for now just flying round screen picking a point and going to it is good enough. Its a top down view.
Phaelax
DBPro Master
20
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 31st Jan 2013 17:31
The command you want to use is atanfull()


Try this example:



"You're not going crazy. You're going sane in a crazy world!" ~Tick
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 31st Jan 2013 20:33
This is just perfect Phaelax, im just wondering exactly how atanfull works? How does it know to go in a straight line? is it like 1 across for every 1 down for example? Ive never come across a tutorial (and ive done most of them) that explained cos sin and atan etc good enough for my slightly feeble brain. Im trying to break the code down but struggling just a little.

What im trying to achieve in the end is a sense of random walking about the place (or on roads) for idle "peasants" and actual moving resources around for non idle peasants in a controlled way to make them deliberately go to certain nodes or "buildings" exactly like settlers.
zeroSlave
14
Years of Service
User Offline
Joined: 13th Jun 2009
Location: Springfield
Posted: 31st Jan 2013 21:35 Edited at: 2nd Feb 2013 02:58
ATANFULL will get you the angle between two points in 2d space.


SIN and COS will get you a position in 2d space based on an angle.


Once you have the angle (using atan) you can then plot the course for movement by using this formula:


This will move the object in the amount of distance# in the direction of angle# per sync. Phaelax's example shows this nicely.

You may also want to look into pathfinding AI for your peasants/workers/peons.
A* pathfinding is good to know, but you may want to look into Potential Fields for an RTS.

Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid.
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 1st Feb 2013 10:29
Thank you all for this. I am going to look into it in great detail. I think i am going to just try and build what i want to build (the simple simple version) and keep posting up problems i need to get through. Its probably the best way for me to learn. My Hands on book does explain cos and sin but not in as great a depth as i would like and it doesnt include ATANFULL at all so once again, this is great.

Thank you.
Phaelax
DBPro Master
20
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 1st Feb 2013 14:03
If you wanna know the math behind atanfull() then read my old snippet:

http://forum.thegamecreators.com/?m=forum_view&t=27159&b=6

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 2nd Feb 2013 13:53 Edited at: 2nd Feb 2013 14:15
I'm going to pop back through my book to get a better grasp on Cos and Sin as i did understand it when i first read it. Then ill have a bit more of a play through that code to see whats happening.

That article on potential fields is just amazing and the first few paragraphs made total sense (if im reading it right) and its a very good way to solve some problems but like A* pathfinding i have no real world way to implement it into darkbasic so even if i do understand it, im not sure i could write a program demonstrating it, let alone expand it to incorporate it into a game. I would like it demonstrated with DB code if i can find some to help me visualise it better (i am a very visual learner, text and numbers make less sense which is why books are not as helpful as they are to others).
I may make a new post about this (if search fails to come up with a viable solution) when i come to that part. Its all very interesting stuff though and a good read. Well the first parts of it are then it starts becoming a mess but once you fully understand a small bit you just move on from there. Hopefully
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 2nd Feb 2013 16:30
I just had a play around and a brush up on Sin and Cos etc and have learned and refreshed a lot. I have a slightly better understanding but some things are still puzzling me regarding this bit.

Note: Deliberately quoting code not codeboxing for ease of reading.
Quote: "
if ((person(i).xpos-person(i).targetX)^2 + (person(i).ypos-person(i).targetY)^2) > speed*speed
"

Im not too sure how exactly this works. I know what its doing as if its not at that position then (do rest of code which moves it). Particularly not sure why the > speed*speed thing.

Quote: "
a# = atanfull(person(i).targetX - person(i).xpos, person(i).targetY - person(i).ypos)
"

Now this line is just making the next ones work because it saves typing that code again, instead you just put a# but even though i have read how atanfull works i still dont understand it and therefore dont know exactly what this statement is saying.

Quote: "
person(i).xpos = person(i).xpos + sin(a#)*speed
person(i).ypos = person(i).ypos + cos(a#)*speed
"

Again now i think i know what this is doing, its the part that updates the x and y pos and moves it using speed but because i dont understand why the angle*speed moves it or how the angle is worked out using atanfull then adding that to sin or cos im a bit stuck. Now what i am also missing from this is why sin and cos are the other way round. I though cos was for xpos and sin would be for ypos yet person(i).xpos + sin(a#) ? not cos(a#) i dont know.

Now dont get me wrong, using this im pretty sure i could make something where a "building" is placed down which automatically has a co-ordinate where its "entrance" is. Then make a "human" walk from its designated point to the entrance of that building and in fact pretty much wherever i like, be it a random place, a specified place or a mouseclick etc. I would just like to try and understand it deeper if anyone can help please.
Phaelax
DBPro Master
20
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 3rd Feb 2013 23:22
Code question #1:

It's a distance formula, more specifically Pythagorean theorem.
A^2 + B^2 = C^2

A and B are the legs of a right triangle, C is the hypotenuse. With two points, you can draw lines from each one until they intersect, forming the right 90 degree corner. Finding the difference between the X and Y coordinates of both points will give you the legs. And rather than compute the square root of that to get an exact distance, we're simply comparing to a known distance and therefore its computationally quicker to square the hypotenuse C than calculating the square root.

Basically, it's checking the player's distance from the target is less than the speed amount. Checking for a smaller distance closer to the target could lead to a potential loop of never reaching the target, but it keeps over stepping back and forth.


Code question #2:
atanfull() will calculate the angle between two points, so we know which direction to move towards the other. To understand how that function calculates the angle you can look at the broken down code I linked to in my other post. You should know "SOHCAHTOA" so you can understand how triangles are used here and then you'll see how tangent plays into finding the answer.





Code question #3:
Again, it's all about triangles. Sine and cosine, when given an angle, will return a decimal between -1 and 1. The X and Y axis (in 2D) are the legs of a triangle. The direction we move (from starting to position to ending position after the next movement step) is the hypotenuse. The values returned from sine/cosine basically tell how far along the legs you move to get to your destination. Speed is the maximum distance you can move in a single step, and by multiplying that by the sine values (think of it as a percentage) you'll get your targeted movement point.


It's easier to explain with more pictures, but hopefully I was clear enough to help out a little.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 4th Feb 2013 09:16
Yes im quite a visual person and no matter how rough a picture is it usually does wonders for me over written text (dont know why, its not like im artistic in any way, i cant even draw stick figures, i just prefer pictures for explanations accompanying text.)
Anyway, that diagram has helped as has going through the books again and experimenting with code im slowly but surely piecing together certain bits. I made one of those circle guys to to the entrance of a building (an offset sprite) i clicked whenever it was placed down so its getting there

Login to post a reply

Server time is: 2024-03-29 05:20:07
Your offset time is: 2024-03-29 05:20:07