-
Help!~
I'm not sure if i'm posting this thread in the correct VB Section.
What i have to produce on a program is this:
I have to use a map, which has the routes between states on it. Whatever it is, I have to make a dot, or any other object, move along those routes. The thing is, how do I do this in VB?
-
easy enough.
1.For starters you need a picture of a map, save it as a bmp and use this as your background pic.
2. Hard code the dimentions of the pic using scalemode pixels and scalewidth and scaleheight.
3.Code an Array of index's for x and y values that correspond to the different states and/or cities.
4.Code it so that on mousedown or mousemove the x,y values are compared to the vals in the array and produce a shape,pic,or pixel color at that position.
5.Remove(visible=false) or move (move method) or biltblt the shape,pic,or pixelcolor when not over anymore.
pset(x,y) will get you a dot
circle (x,y),r will do a circle
not gonna touch blitting here
a simple method might be using formName(or picname).cls before each shape or whatever is moved. Be prepared for a lot of flickering if your not gonna dive deep into bltblit or Memory DC's though. If you go the blt and DC route come back when you understand those for more help.
-
i don't get step number2! thanx for that guide by the way...
-
you have a certain width for your map.
instead of doing the calculations yourself how many pixels represent what on your pic you just set the scalemode to user and the scalewidth to you width. let's say you got a map in your task that is supposed to be 200 meters than you just put in 200 into scalewidth, do the same for the hight and follow the guidelines posted by joey
-
Well intrpreted /\/\issinthrop! This should get you there fkheng.
-
MY NAME IS MISANTHROP
why can't anyone spell my name right???
you even bothered to write /\/\ instead of M.
Misanthrop(e).
-
I spelled it right the first time *proud* :D
Anyways. The ScaleWidth thing is a nice way to do it, just make sure you don't set the ScaleWidth to represent for example kilometers when you need to set the dots per meter (we don't use miles here :p), as that would make it difficult to position them...
-
true
and if the program is to draw those dots and stuff more regularly you better figure out your own scale thing and use the API (setPixel) because it is way faster!
-
there are so many dots on the form, how do i determine the coordinate of each dot?
-
I thought you would draw them yourself... whenever you draw one you pass the coordiantes...
-
teh lines are already on the graphic itself, the thing is now, how do i proceed from there?
-
so you need to find lines in a picture file?
that is gonna be a little more challenging...
is the picture clean? are the lines in a different color than anything else?
-
yeap, the color is different from the other elements on the picture...
-
so wat if the color is differnet, how can it be achieved?
also, er........how do I find out the X, Y coordinates on a form at design-time? there are many small dots on the form....how do i make the coordinates out?
how do i make a picture appear at a specified coordinatE?
-
well since the color is different all you have to do is go through every pixel and look if it's the color you are looking for, otherwise you would have needed to recognize shapes what would have been much harder.
what do you mean by the X,Y coordinates on a form?
and if you load the picture into a picturebox than you can just set the left and top properties. If you blt the picture onto a dc you have to give the coordinates anywayws...
-
I have two destination in the map. Lets say from destination A to destination B takes 6 hours.
How do i set that the particular circle is in the middle of the two destination after 3 hours ???
I thought of using timer, but timer only can set it in terms of seconds for the interval but not hours.
How can i solved this problem ???
-
you want the program to actually take 6 hours for a task that would take 6hours in reality? If so you will probably run into some trouble, because noramally I would tell you to use GetTickcount, but that gets resetted after a day or two (other people here will give you more info on that) and timers are just sucky.
But if you really would want to you could measure any time using a timer (even though timers remain inaccurate) by just setting it's interval to a minute and than count how many times the event occured... after 60 times it would be one hour and so on...
what I think would be way better would be to make a 'in program time' which runs faster than the real time so you can speed things up! (and make programming easier or at least cleaner)
-
I think that's an example winsonlee is giving us.
Goes like this:
You need to know how far along the journey you are. Therefore:
Code:
k = timetotal / timenow
i = (x1 - x2) / k
j = (y1 - y2) / k
if j < 0 then j = -j
if i < 0 then i = -i
makecircle(i, j)
Sorry about the psuedo-code.
That gets the totaltime, and divides it by the time now (in your case, equalling 2, because 6/3 = 2/1 = 2)
Then, it gets the point in the middle of the line (assuming the line is from x1, y1 to x2, y2), and stores the points at i, j.
The little if statements make sure i and j are not negative (x1-x2 sometimes does that)
Then it makes a circle and i, j.
if you want to do that after 3 horus, just use the timer (1000 = 1 second, so 60000 = I minute, so 3600000 = 1 hour, so 10800000 = 3 hours), or use GetTickCount, and put in 10800000.
If you don't know how to use GetTickCount, sorry, I have to go now. :)
Good luck.
-
that should work theoretically, while I am not sure if you can put numbers that high into the timer control!
that is why I propsed to have it count minutes....
for gettickcount just open the API viewer copy the declaration and paste it into you project than you can use it like that:
lngTime = gettickcount 'LngTime now has the value of how many milliseconds the system is up...
now you could calculate how much time has passed since you assigne the ticks to lngTime by
(gettickcount-lgntime)/1000 'would give you the seconds that have passed for example....
-
is there any code which allows me then to find out the difference between 2 given times?
i plan to find the difference between 2 times and then take the difference and assign it to timer1.interval. Shouldn't this be possible?
-
i am actually at a stage now, where i have followed joey's step 3 which was mentioned earlier in this thread (I did not follow the other steps, but used that idea).
SO now if i can fin dthe difference in time, then the next challenge would be "How do I spread the coordinates for the picture to move upon over timer1.interval?"
-
well basically I am sure that you can get the difference between two times. in which format do you get them?
I am not exactly sure what you want to do now.
what are those two time representing?
basically if you move from one point to another you should get the slope m and the offset c and than you can just go there in a loop
y=m*x+c
so you increase x depend to the time and you will get from the first point to the second one.
-
oh no no, i do not intend to use formulas here...
one is a time which i have stored in a database, and another is captured real-time from the system...
what i need to know is that if there is any code or method available which already does the subtraction between 2 given times?
I have assigned the coordinates along a curved line to an array, therefore I have the coordinates for a given route.
So if i want to move a picture from a source to its destination, how do I do that over a given time? Moreover, how do I spread it out evenly accross the amount of coordinates i have?
-
well if you have all the coordiantes where you want the picture to be at at a certain time you can just have a static var increase by one every time the timer event occurs and than move the pic like:
pic.x = coordinates(theIncreasingVar).x
pic.y = coordinates(theIncreasingVar).y
did that make any sense?
-
nope, it did not, not in my case
you're assuming that the line i'm using is a straight line
i can't increment the value of the variable by one each time as i'm using a curve line, which can bend anywhere....
any other way?
-
well that would still work.
It gets better the more points you have but unless you don't have a y coordinate per pixel it is best to connect those you have by lines even if it makes a curve
you could try that pretty fast on a sine wave and see that it gives you quite good appearance.
-
ok, but wat if i do have an x and a y coordinate per pixel?
i have stored all the coordinates of the curve and saved them into an array...
how can i manipulate them now?
-
what do you want to manipulate?
-
manipulate the coordinates, so that i know how to move a picture (which is actually a dot), over them with respect to time...
-
hm sorry I still don't get you right.
do you have all the coordiantes where the point should be sometimes or where it should be all over it's way there?
well maybe someone else knows what you want to do now. I really don't understand...
-
I haev the coordinates. Among al tehse coordinates, some are assigned to special arrays, which signify that each represents a state.
What I am doing is loading the source and destination from a database, for any given record, matching the source and destination with the appropriate coordinates, and then make the bus moev along those coordinates from source to destination accross a given time....
get it?
-
so you know the time the whole trip takes and now you want to know where the bus has to be in between?
-
yeap
i know the trip duration, now i want to know, wat if the user opens the form in the middle of the trip duration, i'll haev to calculate the position of the bus at that time, and determine which coordinate it has to start from...
and from there onwards, it has to move according to the remaining trip duration till it reaches its destination...
-
hm as of right now I can't tell you of any great way....
basically you need the avarage velocity.. to get that you need the distance... you could get the distance by doing a for loop and adding up the distance from point i and i+1... than you can get the average velocity... than you could put marks in some array how far every point is... than you could calculate how far you are at the time the user opens the form... than you first go to the point it needs to be
from there on you need to get the way from this point to the next one as a function which would be easy... well I guess you know what I mean and you could have just come up with it yourself...
I really can't think of any more elegant way right now...
-
er........is there any way you could elaborate on wat u just said?
-
haha tomorrow ok? it's 11:30 pm and I have to get up early...
good luck working that out...
-
Good grief DM!
I've just read this through and it seems to have got somewhat complicated along the way.
Just to confirm fkheng...
You have a map.
You know the start point of this bus and the end point.
You know the route in between.
You know how long it takes to make the trip.
You know the start time.
What you want to do is draw is how far the bus is along the route, based on the current time?
If you confirm/correct the above then you'll be pleased to know what I think you're trying to do is childishly simple... :)
On the other hand, if the above is completely wrong - I'll get my coat... :rolleyes:
-
hi well when my alarm woke me up this morning I suddenly had an easier Idea... gonna post it here right after school...
-
/\/\isanThr0p, would lvoe to have u share that idea with us....
by the way, just curious, what does ur username stand for?
arbiter, u r absolutely right, that's wat i would like to do...
well, if that's easy, i'd like to tkae it a step further...
after finding out the current position of the bus at that current time, i would like the bus to move on till it reaches its destination...
any ideas?
-
hey arbiter, is that signature of urs a verse from the bible?
-
well I assume that:
you have the total distance (if not that you can calculate it)
the average velocity
and points (in an array) that should be connected with a straight line to make the whole route.
the current time.
-
am I right with that?? and... is it enough to display the bus at the point that is closest to where the bus is at or does it need to be the exact position of the bus?
-
fkheng,
A misanthrope is someone who dislikes people in general, doesn't trust anyone etc
With regard to your problem, the easiest way of doing it is to set a list of co-ordinates that the bus follows on its route.
The more co-ordinates you specify the more accurate your program will be.
The first co-ordinate will be the start, the last will be the end and all the ones in between will be the route.
Then, you put all these co-ordinates in an array.
In order to calculate where the bus is, you calculate the % of the way through the bus is based on the journey time. For example, if the bus set off 30 mins ago and the journey takes 1 hour then you know it's 50% of the way through the journey.
Now this is where the array of co-ordinates come in. You select the co-ordinate that's 50% (or whatever) of the way through the array and that will tell you where the bus should be.
Then you simply draw the bus's representative picture (a blob, or even a drawing of a bus, whatever) at the co-ordinates you pulled from the array.
For ease of coding, routes could be given exactly 100 co-ordinates (one for each % of the journey) . It makes your job easier, but if you want more or less (to make the program more or less accurate) then you can do this but it will require extra coding.
By doing it this way, it also provides you with an easy way of changing the bus route (by simply changing the list of co-ordinates) or to support multiple buses.
Make sense?
-
Cool, in the time it took me write all that ,Misanthrop posted. Twice!
And the quote isn't from the bible (although the bible is very quotable), it's from T.S. Eliot's - 'The Waste Land'.
-
hey arbiter well I was working on some code but I did not post it yet...
well what you said with the % works fine if the distance between each point is the same if not you need to calculate it first (I would do that with phythagoras and than just find an equation for the line and move it along...)
-
Alternatively the array could hold the distance between itself and the next point. Then you'd need to know the total length of the route and the distance between each points. He's not made any mention of knowing that so I gave the best solution I could think of at the time.
If he wants to know exactly where the bus is, he's going to have to fit a GPS to it....
-
you are right. I hope he is going for your advice (implying that the points have a fixed distance) because otherwise that could mean some wierd coding... I tried to put it into some pseudo code and I think it would need like 6 or 8 variables just for that little task...
-
sorry for the late reply, yeap, actually Arbiter is going along the same line as i am, and the formula sounds fine with me, i also thought about something like that.
I am assuming that the distance between each coordinate is the same.
I am also storing each coordinate of the route into an array.
The challenge now is from the point I position the bus on the screen, I'll have to make the bus move from that point onwards till it reaches its destination accross time.
Another hassle would be that though using a route, there maybe many trips. The route works fine, but on my map, I have routes which branch out.
The problem just gets bigger. Was just wondering if you ppl could offer some ideas. I hope I this is clear. I'd be glad to clarify as many time as possible if what I have said is 'blurry'.
-
Well, I think to make it easier for Misanthrop and myself, you might want to post your map and any code you have at the moment.
If we can see where you're up to, we can provide pointers for you.
-
true well the fragment of code that I was working on kind of would do the job... it's not done though and I got to work for school...
so I could post it after doing a little morfe to it... but it would be no real code just vb like code you could get the idea from...
but didn't you say every point the bus should ever be at is stored in the array? in that case you could just go like
Code:
sub movebus(start as long)
for i= start to end
bus.x = array(i).x
bus.y = array(i).y
next
(you could put some code to slow it down in between, so it wont just fly through though....)
-
You'd probably use a timer event to check the time every user defined interval and then calculate which image to draw.
It'd be more accurate than looping through the array with a delay.
-
1 Attachment(s)
hey thanx ppl, i really appreciate all the help...
yeap, i do have all the coordinates i need for the route, stored in various arrays...
ok, i'll post the map, and see if you ppl understand it, it might be confusing...
for now, its disfunctional, can't work properly...not sure wat i'm doing...
misanthrope's idea is good, just that i can't use a loop to make the bus move, becoz i onli require the bus to move to the next coordinate according to thee next given time... that is why i was wondering if there was a way to find out the interval between 2 coordinates...
arbiter, the timer event, hm...........yeah, but i'm really talking about possibly long times, coujld be measured in hours, but still, the timer is the easiest way rite? so wat do u think?
attached below is the map...
-
1 Attachment(s)
-
I'll try and look at these this evening or tomorrow for you.
And for the timer event, I didn't mean set the timer gap to the gap between updates, I meant have the timer check the time every second/10 secs/minute and compare to the start time.
e.g. Every second, the timer gets the time from the system using the now() function.
It compares that the start time of the bus and calculates as follows.
time running = Now - start time
journey complete percent = (total time of journey / 100) * time running
Then you get the co-ordinate which corresponds with the percentage complete and draw the bus.
Easy... :)
-
ok, but how do i know or calculate which coordinate lies at which position or percentage?
-
er.........wat is the purpose of the offset?
and have u taken a look at my code?
-
The offset is to select the co-ordinate from the percentage if you have anything other than 100 co-ordinates.
Do you understand the theory that I've posted?
I've not had chance to look at your code yet, I've been busy with other projects.
You should have everything you need in the posts above to code the solution to your problem.