Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LuaJ, creating a vector class  (Read 309 times)
0 Members and 1 Guest are viewing this topic.
Offline orange451

JGO Kernel


Medals: 346
Projects: 6
Exp: 6 years


Your face. Your ass. What's the difference?


« Posted 2017-10-18 03:27:59 »

Hiah. I am trying to implement luaj into my project, and I was hoping to get some help!

I want to implement a Vector class similar to this structure:
http://wiki.roblox.com/index.php?title=API:Vector3

The problem is I can't seem to find proper documentation on a few things that are necessary. Firstly, I like the .new() convention for constructors, but I am not sure how that would translate to java, as "new" is a protected keyword.

Secondly, how would I go about operator overloading? i.e. Vector3 * Vector3, ect.

Thirdly, I don't know how to go about creating a property that automatically updates as other properties are changed. For example, in the Vector class I linked: X, Y, Z, Unit, and Magnitude are all fields of a Vector. If X, Y, or Z changes then the Unit and Magnitude variable automatically change. There are no setters or getters.

Ideally I would have most of the functionality in java, and just expose some methods to the user via lua.

First Recon. A java made online first person shooter!
Offline orange451

JGO Kernel


Medals: 346
Projects: 6
Exp: 6 years


Your face. Your ass. What's the difference?


« Reply #1 - Posted 2017-10-18 04:14:06 »

Here is my current implementation:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
package math;

import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.ThreeArgFunction;
import org.luaj.vm2.lib.jse.CoerceJavaToLua;

public class Vector3Lua {
   public float X;
   public float Y;
   public float Z;
   
   public Vector3Lua unit;
   
   public Vector3Lua() {
      // Empty
   }
   
   // Java constructor
    public Vector3Lua(float X, float Y, float Z) {
       this.X = X;
       this.Y = Y;
       this.Z = Z;
       
       this.unit = new Vector3Lua(); // TODO Make this automatically calculate
   }
   
    // Lua constructor
    static public class New extends ThreeArgFunction {

      @Override
      public LuaValue call(LuaValue arg0, LuaValue arg1, LuaValue arg2) {
            return CoerceJavaToLua.coerce(new Vector3Lua(arg0.tofloat(), arg1.tofloat(), arg2.tofloat()));
      }
    }
   
   // Lua Function - Dot Product
    public float Dot(Vector3Lua other) {
       if ( other == null ) {
          return 0;
       }
       
        return X * other.X + Y * other.Y + Z * other.Z;
    }
   
    // Lua Function - Cross Product
    public LuaValue Cross(Vector3Lua other) {
       Vector3Lua result = new Vector3Lua( Y * other.Z - Z * other.Y,
                                  Z * other.X - X * other.Z,
                                  X * other.Y - Y * other.X );
        return CoerceJavaToLua.coerce(result);
    }
}


Testable example:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
package math;

import org.luaj.vm2.*;
import org.luaj.vm2.lib.*;
import org.luaj.vm2.lib.jse.*;

public class luaj {
   public static void main(String[] args) {
      // Normal Lua defaults
      Globals globals = JsePlatform.standardGlobals();
     
      // Setup Vector class
      LuaValue vectorClass = CoerceJavaToLua.coerce(Vector3Lua.class);
      globals.set("Vector3", vectorClass);

      // Bind .new() to constructor function
      LuaTable t = new LuaTable();
      t.set("new", new Vector3Lua.New());
      t.set("__index", t);
      vectorClass.setmetatable(t);

      // Test it
      LuaValue chunk = globals.load("local test1 = Vector3.new(9, 8, 7); local test2 = Vector3.new(1, 2, 3); print(test1.Z); print(test1:Dot(test2)); print(Vector3:func())");
      chunk.call();
   }
}

First Recon. A java made online first person shooter!
Offline orange451

JGO Kernel


Medals: 346
Projects: 6
Exp: 6 years


Your face. Your ass. What's the difference?


« Reply #2 - Posted 2017-10-18 22:10:45 »

I've found an interesting problem. Not sure if it's due to my own incompetence or an issue with LuaJ...

If I define a class with a metatable such as this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
// Setup Vector class
LuaValue vectorClass = CoerceJavaToLua.coerce(Vector3Lua.class);
globals.set("Vector3", vectorClass);

// Metatable stuff for Vector3
LuaTable t = new LuaTable();
t.set("__add", new TwoArgFunction() {
   public LuaValue call(LuaValue x, LuaValue y) {
      System.out.println("TEST1: " + x);
      System.out.println("TEST2: " + y);
      return x;
   }
});
t.set("__index", t);
vectorClass.setmetatable(t);


And then in lua I create an instance of "Vector3Lua":
1  
2  
3  
print( "Vector3 Metatable:", getmetatable(Vector3) );
local test1 = Vector3.new(9, 8, 7);
print("Test1 Metatable:", getmetatable(test1));


Vector3 contains a metatable, but Test1 does not. So what seems to be happening is that metatables do not pass from parents to their children from the .new() lua function. But I've seen this done before in other game engines. Hmm.

First Recon. A java made online first person shooter!
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (108 views)
2017-09-30 02:57:34

theagentd (135 views)
2017-09-26 18:23:31

cybrmynd (245 views)
2017-08-02 12:28:51

cybrmynd (240 views)
2017-08-02 12:19:43

cybrmynd (238 views)
2017-08-02 12:18:09

Sralse (252 views)
2017-07-25 17:13:48

Archive (864 views)
2017-04-27 17:45:51

buddyBro (1007 views)
2017-04-05 03:38:00

CopyableCougar4 (1566 views)
2017-03-24 15:39:42

theagentd (1373 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!