Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
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 971 times)
0 Members and 1 Guest are viewing this topic.
Offline orange451

JGO Kernel


Medals: 438
Projects: 7
Exp: 7 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: 438
Projects: 7
Exp: 7 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: 438
Projects: 7
Exp: 7 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  
 
 

 
EgonOlsen (1644 views)
2018-06-10 19:43:48

EgonOlsen (1705 views)
2018-06-10 19:43:44

EgonOlsen (1155 views)
2018-06-10 19:43:20

DesertCoockie (1583 views)
2018-05-13 18:23:11

nelsongames (1183 views)
2018-04-24 18:15:36

nelsongames (1707 views)
2018-04-24 18:14:32

ivj94 (2515 views)
2018-03-24 14:47:39

ivj94 (1732 views)
2018-03-24 14:46:31

ivj94 (2813 views)
2018-03-24 14:43:53

Solater (953 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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!