2
\$\begingroup\$

Does Java have a built-in Vector class suitable for handling collision detection / response? It should have methods like subtract(Vector v), normalize(), dotProduct(Vector v), ...

It seems logical to use java.awt.Rectangle and java.awt.Polygon to calculate collisions. Would I be right to use these classes for this purpose?

I understand collision detection; I'm only wondering what approach to it is idiomatic in Java. I'm new to the language and to application development in general.

\$\endgroup\$
1
  • \$\begingroup\$ Just implement it yourself... That way you can only have methods that you need :) \$\endgroup\$
    – Savlon
    Commented Jun 4, 2014 at 10:39

5 Answers 5

5
\$\begingroup\$

There is no built in math Vector class. You'll have to implement your own or use a library like JScience - its project page is here.

EDIT: As comment below suggested, JScience isn't really designed for gaming application is missing some optimizations. A more game oriented vector math API can be found in libgdx. I imagine it should not be too difficult to extract just the math classes from libgdx.

\$\endgroup\$
1
  • \$\begingroup\$ JScience's Vector classes are inefficient for game applications where vectors are constantly modified; they don't allow in-place modification or cycle-saving operations like lengthSquared. \$\endgroup\$
    – Anko
    Commented Feb 24, 2015 at 13:48
4
\$\begingroup\$

The vecmath-package from Java 3D can be used separately from the rest of the project. It allows in-place modification of vectors and has various methods that are convenient and efficient for games (such as lengthSquared, which avoids a √-operation).

vecmath.jar is 312K and works well separately from the rest of Java3D. It may be included under javax.vecmath in some Java distributions, but you can download it at the below link if not.

Downloads | Documentation

\$\endgroup\$
1
\$\begingroup\$

There is also a Vector3D class in Apache Commons Math.

\$\endgroup\$
2
  • \$\begingroup\$ I've found the Vector classes in Apache Commons Math to be lacking for game applications: They don't allow in-place modification (instead returning new instances, which performs badly) and they have no methods to get length (inconvenient) or length-squared (convenience and performance penalty for having to undo the sqrt). \$\endgroup\$
    – Anko
    Commented Feb 24, 2015 at 13:19
  • \$\begingroup\$ You can get the length (or length-squared) of the vector by calling getNorm() (or getNormSqrt()). \$\endgroup\$ Commented Feb 28, 2015 at 22:39
1
\$\begingroup\$

I use the Vector2 and Vector3 class from LibGDX (java gamedev library). It has some odd behaviour and you have to be careful when you do calculations with references, but I guess this is the fault of java not the vector implementation. On the other side, the vector class from XNA had some odd behaviour as well when it did come to normalization and I replaced it with another one from another library :)

LibGDX Vector2 Class

\$\endgroup\$
1
\$\begingroup\$

I've just starting using Vectorz, and it's great. Fast, in-place transformations, readily available from Maven Central, well documented.

From the README:

Fast double-precision vector and matrix maths library for Java, based around the concept of N-dimensional arrays.

This library is designed for use in games, simulations, raytracers, machine learning etc. where fast vector maths is important.

Some highlights:

  • Vectorz can do over 1 billion 3D vector operations per second on a single thread.
  • Specialised matrix types for efficient optimised operations (identity, diagonal, sparse etc.).
  • Support for arbitrary n-dimensional numerical arrays.
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .