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;
}
Related
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
I'm currently working on RPG graphically very similar to Runescape (old school), but with my own game system. I am not sure what is the best way to have interchangeable armor system. I am going to create low poly models, as i said, similar to runescape models like here, and want to have possibility to have random variations of armor like here.
There are two ways as I understand, first is just to change mesh of my character model, the second one is to create model of my character and use the bones weights to armor models. I know there is whole topic about the first option here (couldn't make 3rd link) forum.unity3d.com/threads/stitch-multiple-body-parts-into-one-character.16485/, but really? Every qeustion about something similar leads to this topic. But its like 8 years old and for Unity 2.0...Is there simplier way to achieve same results but don't study masterprompt's exhausting explanation which I'm not gonna be able to implement anyway. And for the second way I can't find any hints, except manual and script on unity web for bone weight, which is way too general for my use and my skills. I have spent looking for some solutions (tutorials) for hours, but google keeps popping the same topics, which are 4-8 years old...
Any advice how to choose the best way for changing armors and how to gain any knowledge about these issues? Thank You.
So what I'm doing in my projects is I duplicate my base character .blend file (rig, model) and I remove most of the mesh except for the body part that I want to make the clothing for (chest/legs). I pull it out a bit so the vertices doesn't overlap with base mesh and just save it.
Then in Unity I create a new prefab out of the model I just saved. The hierarchy of a prefab looks like this: parent game object with BoneReplacer.cs script -> child game object with skinned mesh renderer of my clothing.
Then all I have to do is just drag this prefab onto my character prefab so the target variable in this script points to my character's skinned mesh renderer.
This probably isn't the best way to do this but it works for me, maybe it will help you get your head around it and create some better solution.
Heres the BoneReplacer.cs script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoneReplacer : MonoBehaviour
{
public GameObject target;
SkinnedMeshRenderer myRenderer;
Transform[] newBones;
SkinnedMeshRenderer targetRenderer;
Dictionary<string, Transform> boneMap = new Dictionary<string, Transform>();
void Start()
{
target = transform.parent.parent.gameObject;
targetRenderer = target.GetComponentInChildren<SkinnedMeshRenderer>();
foreach (Transform _bone in targetRenderer.bones)
{
boneMap[_bone.gameObject.name] = _bone;
}
myRenderer = gameObject.GetComponent<SkinnedMeshRenderer>();
newBones = new Transform[myRenderer.bones.Length];
for (int i = 0; i < myRenderer.bones.Length; i++)
{
GameObject _bone = myRenderer.bones[i].gameObject;
if (!boneMap.TryGetValue(_bone.name, out newBones[i]))
{
Debug.Log("Unable to map bone ~" + _bone.name + "~ to target skeleton!");
break;
}
}
myRenderer.bones = newBones;
myRenderer.rootBone = targetRenderer.rootBone;
}
}
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
}
}
}
I have an entity called Shop which has a DBGeorgpraphy column called Position
A sample shop in the database has a Position value of POINT (145.034242 -37.825519)
I am trying to retrieve all shops that fall within a polygon.
var polygon = DbGeography.PolygonFromText(#"POLYGON((145.2898592378906 -37.66376896413059,
145.2898592378906 -37.93504877166811,
144.7075838472656 -37.93504877166811,
144.7075838472656 -37.66376896413059,
145.2898592378906 -37.66376896413059))",
4326);
var shops = db.Shops.Where(p => p.Position.Intersects(polygon));
I would expect the sample shop to be included in the results but it doesn't. Can anyone enlighten me?
The answer is to construct the polygon in the opposite direction, i.e. anti-clockwise.
var polygon = DbGeography.PolygonFromText(#"POLYGON((145.2898592378906 -37.66376896413059,
144.7075838472656 -37.66376896413059,
144.7075838472656 -37.93504877166811,
145.2898592378906 -37.93504877166811,
145.2898592378906 -37.66376896413059))",
4326);
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