Determining position on screen.
Hey guys,
How would you determine the difference of two actors on scene.
Say, you had a race. One person is further ahead whilst the other lags behind. Racer 1's X position on screen is greater than the Racer 2's x position on screen. To find the difference in the distance between Racer 1 and Racer 2 you would simply minus Racer 2's position X from Racer 1's Position X. The result would determine the distance between Racer 1 and Racer 2.
Racer 1 has finished the race. Yay! Now that Racer 1 is gone we want to know the difference in distance between Racer 2 and Racer 3's position X. The race will continue indefinitely as there are an infinite number of racers.
The question is how would you calculate the difference between the leading Racer's X and the Racer behind them's X?
Example,
Racer 2 would finish then we would need the difference between Racer 3 and Racer 4.
Racer 3 would finish then we would need the difference between Racer 4 and Racer 5.
Racer 4 would finish then we would need the difference between Racer 5 and Racer 6.
Racer 5 would finish then we would need the difference between Racer 6 and Racer 7.
Racer 6 would finish then we would need the difference between Racer 7 and Racer 8.
I attempted to resolve this issue by using a rule,
If (512 > self.position X < 480), change primary racer's x to self position x
If (480 > self.position X < 460), change secondary racer's x to self position x
(Primary racer's x) - (Secondary racer's x) = distance
Comments
Ah, you edited your question...
Updating...
That partially solves the issue. There is still the issue of telling the engine who is the leading racer and who is the racer directly after them. Also, what is the difference between them.
If I was wanting to just calculate only the first two racer's distances this would be a piece of cake. Now that I am trying to switch out which racer is closest to the finish line and who is directly behind them all while finding the distance between them, thing are getting sketchy.
@RabidParrot How about this:
Two game attributes: firstPosition, secondPosition
In each racer actor (also a boolean self attribute called leadingRacer):
If my.Position.X > game.firstPosition
change self.leadingRacer to true
else:
change self.leadingRacer to false
Second rule:
If self.leadingRacer is false & my.Position.X > game.secondPosition
Third rule:
If my.Position.X > game.finishLineX
You would also need to either destroy the actor after the finish line, or add another rule checking that a finished actor doesn't overwrite the game.firstPosition and game.secondPosition attributes anymore.
i am starting to think using tables and max() function.
leader_position = max(racer1.x,racer2.x,racer3.x, ....) would give you the leader's positon
then when a racer finishes the race just set their x to something small so they're no longer the leader.
to find the distance between leader and currently standing second place, use min()
min(
(leader_position-racer1.x)/(leader_position-racer1.x) * (leader_position-racer1.x),
(leader_position-racer2.x)/(leader_position-racer2.x) * (leader_position-racer2.x),
(leader_position-racer3.x)/(leader_position-racer3.x) * (leader_position-racer3.x),
.
.
.
)
the above min function will return a smallest non-zero difference.
since if you're looking at racer1.x, and racer1 is leading, (leader_position - racer1.x) will give a zero which when divided by zero will give invalid expression. and will not be returned as min() result.
So only the one closest distance to the current leader will be returned.
To figure out who's leader just look at their x if it's equal to leader's position then they are the leader.
To figure out who's currently standing second look at their (leader's postion-x) if it is the min() above than it's second.
I was thinking of a very similar solution, but what made me reconsider was
That causes a problem for using min and max, because the range of numbers you are comparing is either infinite, or is constantly changing, with new racers dropping in as other finish.
hehehe infinite? i don't think you could ever have infinite...
I was assuming there were some sort of limit of actors on screen at one time like if one racer finishes, another racer enters the scene or something like that or it could even be that same racer that just finished the race that reenters the scene.
I wouldn't know how to min() or max() some group if the group keeps changing constantly like if actors were destroyed and created at random. unless you had the ability to tell it to min() or max() a table column or row.
I'm probably just going to estimate the distance based on the time it takes for the leading racer to hit the second checkpoint and the second racer to hit the first checkpoint. Thanks for all your help!
I hope your racers travel at constant speed or constant acceleration and have no break pedals.
Did you try playing with my method? It isn't that complicated to implement.
I think i like your method better, as you wouldn't have to define so many game.variables or changing a bunch of table values.
@pHghost
Yes, but I couldn't pull the distance data before the next racer had overwritten the data. Simultaneously I'm trying to constrain a "distance bar" to the distance of the leading racer and the runner up. Again, in a perfect scenario there would only be two racers, but because there are constantly racers added to the game it throws everything off. The distance bar doesn't need to be accurate, only appear so. With no data listed in the scene the player won't have a clue if the distance is slightly off.
PS, it isn't a racing game. Although the circumstances are parallel.
This looks fun,
I thought I'd give this a try combining both methods, using max() in some logics
and abs(diference from finishline)/(difference from finishline) to give me a number to excluded finished players.
racers are drag-able by touch or mouse i guess.
they start off at different locations with random speeds.
1st place is colored red
and 2nd place is colored blue and shows pixels difference from first place.
Wow, your actors must be moving bloody fast. I can imagine the 'overwriting war' at the beginning of the race, but once you get some distance, at a decent framerate you should only ever have the correct racers make changes to their respective attributes, all racers beyond #2 should be ignored.
I figured.
I'll take a look when I get home. Thanks!
I just did it for fun, and try to play around with constrains to set booleans and positions.
I don't know how efficient it is though (compared to pHghost's method).
But maybe my abs(difference from finishline)/(difference from finishline) was a complete hack..as I just found out you can do compare
(conditions) and (true value) or (false value)
to get a true value when condition is true (in Lua).