How to get Coordinates of each vehicle in VEINS? - omnet++

I am using Veins 4.6, Sumo 0.25 and Omnet++ 5.2. I need to get the coordinates of two vehicles (nodes) at a given time, to calculate the distance between them.
I have tried to modify the TraCIDemo11p.cc file in the function handlePositionUpdate(). The Problem is when the veh0 returns its coordinate at the same time there is coordinate sent by veh1 which is very small.
How can I get the position of both the vehicles at the given time and find the distance between them?
void TraCIDemo11p :: handlePositionUpdate(cObject* obj) {
BaseWaveApplLayer::handlePositionUpdate(obj);
// Get vehicle ID
std::string vehID = mobility->getExternalId().c_str();
// Get coordinates of first vehicle
if (vehID == "veh0"){
firstVehX = mobility->getCurrentPosition().x;
firstVehY = mobility->getCurrentPosition().y;
firstVehZ = mobility->getCurrentPosition().z;
calculateDistance(vehID, firstVehX, firstVehY,firstVehZ);
}
//Get coordinates of second vehicle
if (vehID == "veh1"){
secondVehX = mobility->getCurrentPosition().x;
secondVehY = mobility->getCurrentPosition().y;
secondVehZ = mobility->getCurrentPosition().z;
calculateDistance(vehID, secondVehX, secondVehY, secondVehZ);
}
}

As far as I understood, you want to calculate the distance from the vehicle this code is running on to some other car. However, I am not sure what this other vehicle is. Is it firstVeh for instance?
If this is the case, with this code you can not have achieve what you want (as you figured out already). This code runs on every vehicle in the simulation but is independent from all other vehicles. Therefore, mobility points only to the mobility module of the current vehicle this code is running on. Thus, mobility->getCurrentPosition() always gives you only the position of exactly this vehicle.
For calculating the distance to firstVeh for example, you need its coordinates. Usually, however, you do not have any knowledge about arbitrary other vehicles in the simulation, unless you receive a message from them which includes its position (see Calculating distance between cars nodes VEINS).
If you really need to calculate the distance to an other, arbitrary vehicle (i.e. not an aforementioned sender of a message), you could get a pointer to that vehicle from the TraCIScenarioManager (see How to get count of cars in specific range). This, however, is bad practice in my opinion, since in reality you would not be aware of any other cars in the scenario, other than some sender of a message, either.

On sink node you can get list of all modules in simulation, access their coordinates and then find the distance between them using following snippet in handlePositionUpdate method of TraCIDemo11p.cc:
//Get current position of the node which is going to send message
Coord senderPosition = mobility->getCurrentPosition();
//Get all available nodes in simulation
std::map<std::string, cModule*> availableCars = mobility->getManager()->getManagedHosts();
//Iterate through collection and find distance,
std::map<std::string, cModule*>::iterator it;
for(it = availableCars.begin(); it != availableCars.end(); it++)
{
TraCIMobility* mobility1 = TraCIMobilityAccess().get(it->second);
Coord receiverPosition = mobility1->getCurrentPosition();
//returns distance in meters
senderPosition.distance(receiverPosition)
}
Ref: How to get count of cars in specific range

Related

Count of nodes in an specific range

I'm using veins 5 and I am trying to obtain the position of nodes around in a certain distance from a node. More specifically, I'm trying to get a mapping position of all nodes in an instant to work with the positions of the nodes.
I read this question How to get count of cars in specific range, however it centered in veins 4.6 and also I didn't get how to use the method suggested by Christoph Sommer. Is it possible to get the mapping in veins 5? How can I work this?
I'll appreciate any help
Thanks!
Finally I got the position of all nodes in the simulation and calculate their distance from a tagged car.
To do this I "translate" the answer of Ahmad Ahsan in the question How to get Coordinates of each vehicle in VEINS? to veins 5.0 in the following way:
void MessageGenerator::getMapping(){
// Get my position on the scenario
veins::Coord myPosition = mobility->getPositionAt(simTime());
EV << myPosition << endl;
// Get all available nodes in simulation
std::map<std::string, cModule*> allNodes = mobility->getManager()->getManagedHosts();
// Iterate through collection and find distance,
std::map<std::string, cModule*>::iterator it;
for(it = allNodes.begin(); it != allNodes.end(); it++)
{
TraCIMobility* auxMobility = TraCIMobilityAccess().get(it->second);
veins::Coord receiverPosition = auxMobility->getPositionAt(simTime());
//returns distance in meters
double dist = myPosition.distance(receiverPosition);
EV << "Distance: " << dist << endl;
}
}
The key change is in the getPositionAt(simTime()) method.
One additional comment, as discussed in this post the method of obtain all vehicles in simulation is a bad practice if this data is used for realistic simulation purposes, because it is rarely possible to obtain all positions in one time step. In my case, I used it for only evaluation purposes.

Keep the correct lane on road

I am trying to make a Traffic Simulator for my bachelor thesis.
I created a map with OpenStreetMap osm and now I am trying to put some cars on roads.
I created a graph for the roads. The graph is like this:
A->B,C,D
C->A,B,E
B->A,C
D->A
E->C
and I generate random the cars on random points A,B,C,D,E,F. After this I use function getShortestPath(start,end) to get the shortest path for every car and I put the cars to move:
foreach(Car c in allCars){
Move(c,path)
}
But, I have a problem, I don't know how to keep the correct lane of road for every car, all my cars are on the same lane of road.
I am thinking about the direction between two points and the car will be at the right side of the direction.
But I have no idea how to do this....
Thank you!
Here is my answer to update your movement and car code, taking into account our conversation in the comments.
Add a variable to your car class:
Class Car {
// your other variables
Vector3 progressPosition; // This will only be set to the from nodes position whenever you get a new path.
}
Your update code will be like this:
void UpdateCarMethod()
{
car.car.progressPosition = Vector3.MoveTowards(car.car.progressPosition, car.path[car.index].point + lane, Time.deltaTime * speed);
car.car.transform.position = car.car.progressPosition + car.car.transform.right;
}

Unable to get the location bound of Sketchup model using Sketchup Ruby API

I have a Sketchup 3d model that is geo-located. I can get the geo-location of the model as follows :-
latitude = Sketchup.active_model.attribute_dictionaries["GeoReference"]["Latitude"]
longitude = Sketchup.active_model.attribute_dictionaries["GeoReference"]["Longitude"]
Now i want to render this model on a 3D globe. So i need the location bounds of the 3d model.
Basically i need bounding box of the model on 2d map.
Right now i am extracting the same from the corners of a model(8 corner).
// This will return left-front-bottom corner.
lowerCorner = Sketchup.active_model.bounds.corner(0)
// This will return right-back-top corner.
upperCorner = Skectup.active_model.bounds.corner(6)
But it returns simple geometrical points in meters, inches depending upon the model.
For example i uploaded this model in sketchup. Following are the values of geo-location, lowerCorner and upperCorner respectively that i'm getting by using the above code for the above model.
geoLocation : 25.141407985864, 55.18563969191 //lat,long
lowerCorner : (-9483.01089", -6412.376053", -162.609524") // In inches
upperCorner : (-9483.01089", 6479.387909", 12882.651999") // In inches
So my first question is what i'm doing is correct or not ?
Second question is If yes for the first how can i get the values of lowerCorner and upperCorner in lat long format.
But it returns simple geometrical points in meters, inches depending upon the model.
Geom::BoundingBox.corner returns a Geom::Point3d. The x, y and z members of that is a Length. That is always returning the internal value of SketchUp which is inches.
However, when you use Length.to_s it will use the current model's unit settings and format the values into that. When you call Geom::Point3d.to_s it will use Length.to_s. On the other hand, if you call Geom::Point3d.inspect it will print the internal units (inches) without formatting.
Instead of tapping into the attributes of the model directly like that I recommend you use the API methods of geo-location: Sketchup::Model.georeferenced?
By the sound of it you might find Sketchup::Model.point_to_latlong useful.
Example - I geolocated a SketchUp model to the town square of Trondheim, Norway (Geolocation: 63°25′47″N 10°23′36″E):
model = Sketchup.active_model
bounds = model.bounds
# Get the base of the boundingbox. No need to get the top - as the
# result doesn't contain altiture information.
(0..3).each { |i|
pt = bounds.corner(i)
latlong = model.point_to_latlong(pt)
latitude = latlong.x.to_f
longitude = latlong.y.to_f
puts "#{pt.inspect} => #{longitude}, #{latitude}"
}

Passing list as a parameter: player vs the world

I'm creating a top-down 2D game.
Classes in question are Level and Entity.
In Level I have a Map, but let's just think of that class as a list of items like walls and other stuff you can imagine will have a body, which is actually a XNA's Rectangle.
Now, in Level's Update function, I'll be putting Entity's function that will resolve all collisions (walls, pickups, other entities, traps, etc), and I am questioning the best way of letting each Entity know what's around him. By doing that I want to achieve the freedom of creating different Entityes such as different players, enemies, bosses... Goal is a good parent class for everything that should feel alive to the player playing the game.
So, I want to get a good performance for this function (that'll resolve collisions), because I plan on making maps that'll have around 1000 different items and many entities, and can't decide how to go about it.
The structure is something like this: Level has a list of items (walls, pickups, traps...) and a list of entities (players, enemies, bosses, pets, mounts, whatever...), and in each update of level I loop trough entities and do Update for each.
Question Do I:
1) send whole list of items to each entity('s update method) and calculate distances from each item and resolve collision
2) calculate closest items to the entity in level's update method and then send smaller list of items to the entity, which will then resolve collision
3) (this I just thought of) pass list of XNA's Rectangles and type of item (that could mean I must make a new class or new function for map class that will return 2 lists: rectangles and item types)
4) pass the whole map, but in a matix (because map will be a grid, not items nor player's rectangles will rotate) of integers that'll have something like "1 for wall, 0 for nothing, 2 for trap, 3 for health-pack, etc). But this will resolve only the map, because other entities won't all be in grid coordinates (wall's positions will be like 32x32, 0x64, etc, and all will be 32x32, but entities won't. They'll be 32x16, and their position may vary. That also crossed my mind just now.
Note that both entities and items will most likely have much more than Rectangle for body. There will be textures, animations, and other basic types of variables for different purpose, and most of them won't be needed for entities collision resolution with that item. When colliding with a wall, entity won't need that items image to resolve collision, but it will need items (lets say) X value if that item is a healt potion.
How would you resolve this so the code is efficient, or how did you resolve this in your game?
I am looking for simpicity and efficiency, and that's what I will be looking for in your answer.
I haven't tested this code at all, so if it doesn't work, thats why.
Given that Entity is a struct with the following code,
struct Entity{
Rectangle pos;
Texture2D tex;
bool hasAgency;
public Entity(Rectangle pos, Texture2D tex, bool hasAgency){
this.pos = pos;
this.tex = tex;
this.hasAgency = hasAgency;
}
}
then this should work.
List<Entity> entityList = new List<Entity>//your entities are here
for(int testIndex=0; testIndex<entityList.Count; testIndex++){
for(int entityBeingTested = 0; entityBeingTested<entityList.Count; entityBeingTested++){
if(testIndex!=entityBeingTested && entityList[testIndex].hasAgency)
{
if(entityList[testIndex].pos.Intersects(entityList[entityBeingTested.pos]))
//hit logic here
}
}
}

Random World Generation

I'm starting to go into random world generating, I have an idea on how random number generating works (Actually pseudorandom numbers), but I don't have a good idea of how to make the world look "nice", in other words not just place blocks based on a random x, y that it gives me, but make it look smooth.
This will be a 1 time generation per world. So everything is created at start.
I was thinking of an algorithm a few moments ago, but the problem is that it would just use be an endless amount of nested if loops, which would probably take a more than the necessary time. I was thinking of the following:
Choose a random location on the map and place the spawn point in that location.
Start building the street based on the spawn location, like if the spawn location is 16
spaces near the edge of the world build a house, otherwise start building a street.
Based on the previously generated street's place structures around.
Place misc.
Conceptualizing the algorithm isn't much of a problem, what I'm having difficulty with is starting the actual code from step 2 and below. Based on the above algorithm or an algorithm you think of, how would you start the code? I'm not asking for the actual code to be made, just an idea of how it would look.
I know this question isn't precise and can have multiple answers, but I've seen many questions similar to this one having a strange approach.
hmm looks like planar(or cubic) map filling. from my point of view firstly you need some databases
struct _object
{
string name,help,info; // texts for later use
int siz[3]; // grid size of object
int pos[3]; // placement pos (center or what ever)
// other stuff like: mesh-id,color,hit points,...
};
struct _dependency
{
int objid
List<int> near; // can be near this objects (can add probability)
List<int> far; // cannot be near this objects (can add probability,min distance)
};
List<_object> object; // DBS of all object types
List<_dependency> depend; // DBS of object dependency
Then you need to initialize this DBS from ini files or whatever. After that you need to create world map. For simplicity let it by only a single squared town and single layer(no sub-terrain), size and placement can be random.
List<_object> obj; // DBS of placed objects, could be lesser derivate of previous _object to conserve memory requirements
const int N=100;
int[N][N] map; // placement map, containing placed obj id, or -1 for empty space
so now you need some town generating function that fills map[N][N]:
void genere()
{
int i,j,x,y,xx,yy,objid,objix;
int _min_complexity=N/10; // this can also be random
int _max_complexity=N; // this can also be random
// clear map
for (i=0;i<N;i++)
for (j=0;j<N;j++)
map[i][j]=-1;
int complexity=_min_complexity+random(_max_complexity-_min_complexity);
for (i=0;i<complexity;)
{
// random placenet position
x=random(N);
y=random(N);
// random object, should take in mind object[].near and closest objects in map[y][x]
objid=random(object.num);
if (check if map[][] is empty enough to place object[objid] to x,y,z)
if (check if near x,y position is not bad type of object already object[].far)
{
// add new object to list
objix=obj.add(object[objid]);
// add new object to map
int *siz=obj[objix].siz
int *pos=obj[objix].pos
x+=pos[0];
y+=pos[1];
for (yy=0;yy<siz[1];yy++)
for (xx=0;xx<siz[0];xx++)
map[y+yy][x+xx]=objid;
i++;
}
}
}
also the position can be double[3] + orientation matrix and map coordinates will than be aligned to grid. There are many ways to tweak this code, its just an starting template. Hope it helps a little.
P.S.
List<type> l;
l.num - number of items in list
l[i] - i item from list
i=l.add(a) - add new item a to list and returns its index

Resources