I am currently working on a dungeon game.
Right now I have to generate the rooms of the dunegon. The first step is easy, as it is the placement of the rooms. Now I have to move them, as they are overlapping with each other. I have written a small script in c# that should do the work:
Before
![alt text][1]
and after:
![alt text][2]
Unfortunately the script isn’t doing what I wanted it to do, as it keeps moving the rooms without stopping.
I have read some informations about the [Separating Axis Theorem][3] that I used in the code:
(this is the class for the room)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Stanza {
public Vector2 centro;
public Vector2 max, min, dimensione;
public Stanza (Vector2 c, Vector2 dim) {
centro = c;
dimensione = dim;
max.x = centro.x + dim.x / 2;
max.y = centro.y + dim.y / 2;
min.x = centro.x - dim.x / 2;
min.y = centro.y - dim.y / 2;
}
public void spostaStanza(Vector2 dir) {
centro.x += dir.x;
centro.y += dir.y;
max.x += dir.x;
max.y += dir.y;
min.x += dir.x;
min.y += dir.y;
}
}
then this is the real script that does the work:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Stanze : MonoBehaviour {
public List<Stanza> stanze = new List<Stanza> ();
public List<GameObject> rects = new List<GameObject> ();
public GameObject piano;
public int numStanze = 5;
// Use this for initialization
void Start () {
for (int i=0; i<numStanze; i++) {
stanze.Add(new Stanza(new Vector2(Random.Range(-50,50), Random.Range(-50,50)), new Vector2(Random.Range(3,8),Random.Range(3,8))));
}
Time.timeScale = 0.01f;
}
void Update() {
for (int i=0; i<numStanze; i++) {
Vector2 l1;
l1.x = Mathf.Abs (stanze_.centro.x - stanze*.max.x);*_
l1.y = Mathf.Abs (stanze_.centro.y - stanze*.max.y);*_
* for(int j=1; j<numStanze; j++) {*
* Vector2 d;*
_ d.x = Mathf.Abs (stanze*.centro.x - stanze[j].centro.x);
d.y = Mathf.Abs (stanze.centro.y - stanze[j].centro.y);*_
* Vector2 l2;*
* l2.x = Mathf.Abs (stanze[j].centro.x - stanze[j].max.x);*
* l2.y = Mathf.Abs (stanze[j].centro.y - stanze[j].max.y);*
* Vector2 ltot;*
* ltot.x = l1.x + l2.x;*
* ltot.y = l1.y + l2.y;*
* if(ltot.x > d.x || ltot.y > d.y) {*
* print(“Sovvraposizione!”);*
* Vector2 move;*
* move.x = ltot.x - d.x;*
* move.y = ltot.y - d.y;*
* if(move.x >= move.y) {*
* move.y = 0;*
_ stanze*.spostaStanza(move);*_
* } else {*
* move.x = 0;*
_ stanze*.spostaStanza(move);*_
* }*
* }*
* }*
* }*
* display ();*
* }*
* void display() {*
* if (rects.Count > 0) {*
* for(int i=0; i<numStanze; i++) {*
_ Destroy(rects*);
}*_
* rects.Clear();*
* }*
* for(int i=0; i<numStanze; i++) {*
_ rects.Add(Instantiate (piano, stanze*.centro, Quaternion.Euler(90,0,0)) as GameObject);*_
rects.transform.localScale = new Vector3(stanze_.dimensione.x,1,stanze*.dimensione.y);
}
}*_
}
The public GameObject piano must have a plane (default unity one).
Eventually what I want to end up with should be like [this][4].
Thanks in advice for any help!
*[1]: http://i.imgur.com/5JgbhWY.png*_
[2]: http://i.imgur.com/iK2yy3V.png*_
_[3]: N Tutorial A - Collision Detection and Response
_*[4]: http://tinykeep.com/dungen/*_