Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8072637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:04:58+00:00 2026-06-05T14:04:58+00:00

I am using the jMonkeyEngine (Java Game Engine) in Scala, which works out pretty

  • 0

I am using the jMonkeyEngine (Java Game Engine) in Scala, which works out pretty well so far, but now I am asking myself whether there is an elegant way of overloading operators for the Vector3f (and similar) classes. My first idea was to inherit from Vector3f and overload the operators, but Vector3f is final, so that is not an option. Next I thought maybe I could have a singleton/scala object with static methods that operate on Vector3f, like below, but that does not work either:

object VectorMath {
    def *(factor: Float, vector: Vector3f) = vector.mult(factor)
}

//and then somewhere
import VectorMath._
var v = new Vector3f(1,2,3);
var u = 1.2f * v; //this does not work, because Float does not have * overloaded for Vector3f
var u = VectorMath.*(1.2f, v); //this does work, but defeats the purpose

So all I can think of now is to wrap the Vector3f in a new Scala class and delegate the operator calls to the appropriate Java methods. This has, however, three downsides:

  1. I will have to do to a lot of back and forth conversions (a toVector3f and a fromVector3f method or something like that).
  2. This problem becomes even worse when I have arrays of Vector3f/ScalaVectors. Since the classes are not related (except by composition), I would have to manually cast every element of the array every time I call a method in jME.
  3. Even then I know of no way to overload an operator for my new ScalaVector class in such a way that I could have the factor in front, i.e. 1.2f * v.

My question: Can someone think of a way to make this more natural/elegant? How do you tackle similar problems? Or is there maybe a Scala syntax that I don know of to do these kinds of things? In C++ I would make a global operator that takes float and Vector3f as arguments and possibly friend it. Whats the Scala way to do this, or is that simply not possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:04:59+00:00Added an answer on June 5, 2026 at 2:04 pm

    Or is there maybe a Scala syntax that I don’t know of to do these kinds of things?

    Yeah, there are implicits:

    class VectorMath(f: Float) { def * (v: Vector3f) = v mult f }
    implicit def VectorMath(f: Float) = new VectorMath(f)
    
    val v = new Vector3f(1,2,3)
    1.2F * v
    // treated as: VectorMath(1.2F).*(v)
    

    Since Scala 2.10 the implicit conversion can also be written as:

    implicit class VectorMath(f: Float) { def * (v: Vector3f) = v mult f }
    

    Since 2.10 there are also value classes which are optimized by the compiler to get better performance:

    implicit class VectorMath(val f: Float) extends AnyVal { def * (v: Vector3f) = v mult f }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am planning to write an simple 3d(isometric view) game in Java using jMonkeyEngine
Using the C# Facebook SDK 5.0.3 everything works fine whit the client.Get(/me). But when
Using Java,I have to fetch multiple sets of values from an XML file to
Using MVVM. I have a DataTemplate which I am using to display an expander
using chrome and ff so far have made a nasty glitch when using scrollTo
Using the navigator.geolocation object in JavaScript. Trying to establish accurate ranges, but wondering exactly
Using android 2.3.3, I have a background Service which has a socket connection. There's
Using jQuery, one can easily find out whether a particular element is visible using
Using NSDateComponents I know how to get the day component, but this gives me
Using MVC2 I have an AJAX form which is posting to a bound model.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.