Is it possible to cast Vector2 float to int?

For my game I do not need vector2 to use floats, it’s actually making things harder for me so I thought I would force it to use int instead but I got an error saying “Cannot cast from Vector2 to int”. Perhaps I am doing it incorrectly.



Vector2 position = new Vector2();

public Bob(Vector2 position) {
		int pos;
		pos=(int)position;
		this.position = position;
		this.box.width = SIZE;
		this.box.height = SIZE;
		
       }


I would prefer to work round up to the nearest 10 but working to the nearest whole number might be good enough to make things work as intended.

Thank you

Wait, you are actually trying to cast a Vector2, so two numbers, to an integer, one number?

You can do [icode]int posX = (int)position.x;[/icode] and [icode]int posY = (int)position.y;[/icode] if you really want to have the vector as integer variables, but keep in mind that Vector2 means two values.

position instanceof Vector -> true
position instanceof Integer -> false

You cant cast it, you have to cast the elements of the vector. Indeed it doesnt make sense, you should write your own Vector if you really want to use integers

Hi thanks, yes I was aware, both x and y should be ints.

Correct me if I’m wrong but these adjusts should be placed in the setter not the constructor?

Ah feck :frowning:

I’ll have to explore that option then.