- Home /
Rect with Z instead of Y or similar alternatives
Hi there! I'm editing this question...
I came to understand that the Rect class has a very valuable Function called Contains, which I'd like to use.
My problem is that the matrix I use to build those Rects has X,Z coordinates, and when I pass them to a Rect my Zs become Ys.
I just need a Class that holds 4 Vector3s as vertices, without meshes or other stuff. I could build up my own one but that Cointains function would require me some extra work ( since my skills are halfway to acceptability).
Is there something else I can use to achieve this goal or maybe a way to build Rects with coordinates I want? Maybe inheriting from Rect/similar and then...? Thanks for the patience!
EDIT + added comments
This is what I've got so far, if you're interested in commenting ( I'd like some suggestions about the code because I'm a self taught "programmer" and I don't know if my way is canon or utterly twisted):
using UnityEngine;
using System.Collections;
[System.Serializable]
public class RectZ{
//Do you like Rects? Would you like them to be X,Z and Y,Z too? Then you'll love this!
public float wMIn; //minimum width... sort of base
public float wMAx; //maximum width
public float hMIn; //minimum height
public float hMAx; //maximum height
public Vector3 center; //the center of this RectZ
public Vector3 size; //the size
public Vector3 extents; //..........
public Vector3 worldCoordinates; //.... pretty straightforward ain't it?
//SO...
//To build a new RectZ from another script, you pass these parameters inside its constructor...
public RectZ (float _UpMinVertex, float _UpMaxVertex, float _DownMinVertex, float _DownMaxVertex, int _1xy_2xz_3yz){
wMIn = _UpMinVertex;
wMAx = _UpMaxVertex;
hMIn = _DownMinVertex;
hMAx = _DownMaxVertex;
//I use an INT as a BOOL to understand which axes you want... this makes the call easier
if(_1xy_2xz_3yz == 1){
size = new Vector3(wMAx-wMIn,hMAx-hMIn,0);
extents = new Vector3(size.x/2,size.y/2,0);
center = new Vector3(size.x-extents.x,size.y-extents.y,0);
worldCoordinates = new Vector3(wMIn+center.x,hMIn+center.y,0);
}
if(_1xy_2xz_3yz == 2){
size = new Vector3(wMAx-wMIn,0,hMAx-hMIn);
extents = new Vector3(size.x/2,0,size.z/2);
center = new Vector3(size.x-extents.x,0,size.z-extents.z);
worldCoordinates = new Vector3(wMIn+center.x,0,hMIn+center.z);
}
if(_1xy_2xz_3yz == 3){
size = new Vector3(0,wMAx-wMIn,hMAx-hMIn);
extents = new Vector3(0,size.y/2,size.z/2);
center = new Vector3(0,size.y-extents.y,size.z-extents.z);
worldCoordinates = new Vector3(0,wMIn+center.y,hMIn+center.z);
}
}
}
To call it I just type things like this:
RectZ cell = new RectZ(vertex.x,vertex.x+tileSize,vertex.z,vertex.z+tileSizeZ,2);
I've skipped other functions of RectZ because I haven't tested them yet, but I'll update them as I progress. This thing, called using my matrix X,Z, works!
Answer by giulio-pierucci · Feb 11, 2015 at 08:07 PM
There are a Bounds object in Unity that you may use if fit your needs:
http://docs.unity3d.com/ScriptReference/Bounds.html
Otherwise, you can write a custom class like this example:
public class foo { public vector3[] vertices;
public bool Contains(vector3 v)
{
..... implement contains logic here
}
}
Hi giulio! I've been coding this class so far.
Since I lack basics I don't know how to use it from other scripts... ideally, I wanted something like all usual calls ( Vector3.x for example, or Physics.Raycast() ).
I'm getting an error about things in this class not being static. To solve the issue I watched this tutorial but I can't grasp the logic.
These are chunks of the custom class I've built:
using UnityEngine;
using System.Collections;
public class RectZ:$$anonymous$$onoBehaviour{
public float w$$anonymous$$In;
public float w$$anonymous$$Ax;
public float h$$anonymous$$In;
public float h$$anonymous$$Ax;
public Vector2 center;
public Vector2 size;
public Vector2 extents;
public RectZ NewRect(float _Up$$anonymous$$inVertex, float _Up$$anonymous$$axVertex, float _Down$$anonymous$$inVertex, float _Down$$anonymous$$axVertex){
w$$anonymous$$In = _Up$$anonymous$$inVertex;
w$$anonymous$$Ax = _Up$$anonymous$$axVertex;
h$$anonymous$$In = _Down$$anonymous$$inVertex;
h$$anonymous$$Ax = _Down$$anonymous$$axVertex;
size = new Vector2(w$$anonymous$$Ax-w$$anonymous$$In,h$$anonymous$$Ax-h$$anonymous$$In);
extents = new Vector2(size.x/2,size.y/2);
center = new Vector2(size.x-extents.x,size.y-extents.y);
}
public bool Is2dPointBetween (float pointCoord1, float pointCoord2, RectZ rect){
}
}
I've skipped the boring stuff. The problem is I can't call NewRect from another script to make a new RectZ, which should be a copycat of Rect struct, except it can work with Z.
by the way, Rect is sealed... that's sad! I wanted to inherit from it!
Hi again! I've fixed the problem stated above!
Thanks for pointing Bounds out though, if you don't $$anonymous$$d, I'd like you to elaborate the differences between using Bounds tweaked to suit my needs and the Class I've built. I'll edit my question to include the new code.
That'd really help me improve my bizarre and unreliable program$$anonymous$$g skills!