• Advertisement
Sign in to follow this  

How to read/adapt coding style that look like this

This topic is 1652 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I am watching this video of Notch coding Minecraft.

 

His coding style is bizarre to me. I seen other open source game projects that have

straightforward code.

 

I will type a sample code from the video for clarity.

 

Timecode for the video for this code is 1:05.

 

Vec3.newTemp(target.x + target.xd * time, target.y + target.yd*time,target.z + target.zd * time);

 

He does not have comments in his code so I am not sure how the developers would follow the code.

 

Source:

 

Edited by warnexus

Share this post


Link to post
Share on other sites
Advertisement

Looks like a change in new 3D position over based on time. Probably called every update or so.

 

Vec3: 3 valued vector.

 

Target.*: Target's current coordinates

 

Target.*d: Delta change for each value

 

time: elapsed time since last update

Edited by Dragonsoulj

Share this post


Link to post
Share on other sites

Looks like a change in new 3D position over based on time. Probably called every update or so.

 

Vec3: 3 valued vector.

 

Target.*: Target's current coordinates

 

Target.*d: Delta change for each value

 

time: elapsed time since last update

Thanks DragonSoulj! It makes a lot of sense now!

Share this post


Link to post
Share on other sites
It makes sense that the code he's actively working on doesn't have comments yet. He's adding an experimental feature and the code is changing a lot.

From what I saw of the few frames where the code is in focus, his coding style seems fine to me. Edited by Nypyren

Share this post


Link to post
Share on other sites


Thanks DragonSoulj! It makes a lot of sense now!

 

wink.png  Helps to know X, Y, and Z are 3D planes (X and Y for 2D), and a point in the 3D space needs a value of each. A change of something is a delta, hence the D.

Share this post


Link to post
Share on other sites

Thanks DragonSoulj! It makes a lot of sense now!

 
wink.png  Helps to know X, Y, and Z are 3D planes (X and Y for 2D), and a point in the 3D space needs a value of each. A change of something is a delta, hence the D.


The word is "coordinates", not "planes".

Share this post


Link to post
Share on other sites
I guess it depends on experience? I looked at that line and it made perfect sense to me. "Vec3" tells me to expect x,y and z coordinates. A "d" in the variable name tells me it is a delta variable. He's constructing a new vector from an old vector, while translating it by delta values scaled by time. Without looking at the video, I would say this is part of a 3D animation system. Edited by MarkS

Share this post


Link to post
Share on other sites

I guess it depends on experience? I looked at that line and it made perfect sense to me. "Vec3" tells me to expect x,y and z coordinates. A "d" in the variable name tells me it is a delta variable. He's constructing a new vector from an old vector, while translating it by delta values scaled by time. Without looking at the video, I would say this is part of a 3D animation system.

 

Where is the new vector going? It's not assigning a return value to anything. I wonder, is in unfinished code, or does Minecraft have global / thread local temp variables to avoid allocations?

Share this post


Link to post
Share on other sites


Actually, I believe those are coordinate planes as I am referring to the whole. His .x, .y, and .z are coordinates on those planes.

A 'coordinate plane' is the plane formed by each *pair* of axis.

 

Individually, the components on a vector are referred to as 'coordinates'.

Share this post


Link to post
Share on other sites
[edit]
This post was completely wrong and helped no one. Just not my day today.
[/edit] Edited by MarkS

Share this post


Link to post
Share on other sites

This is a C++ constructor:

Vec3.newTemp(target.x + target.xd * time, target.y + target.yd*time,target.z + target.zd * time);


It is equivalent to:
Vec3 newTemp;
newTemp.x = target.x + target.xd * time;
newTemp.y = target.y + target.yd * time;
newTemp.z = target.z + target.zd * time;

Except that it is Java, and it isn't equivalent.

warnexus appears to be correct, in that the result is never assigned to a variable.

Share this post


Link to post
Share on other sites

Except that it is Java, and it isn't equivalent.


Ah. I missed that part (probably should watch the video now, huh?). I'm not familiar with Java, other than it is loosely based on C++.

Share this post


Link to post
Share on other sites

 

Except that it is Java, and it isn't equivalent.


Ah. I missed that part (probably should watch the video now, huh?). I'm not familiar with Java, other than it is loosely based on C++.

 

I'd say its more based on plain C while trying to come up with the OO aspect on its own rather than basing it on C++ (hence no :: or -> operator, abstract instead of virtual, interfaces and so on).

 

At first I thought that it was a Vec3 from LWJGL default types but it isnt. I have no idea what that Vec3.nevTemp does (it looks like "nev" instead of "new" to me).

 

If it returns a new Vec3, then it isn't getting assigned to anything. And if it doesn't returns a new Vec3, it looks like a global static method that does something on other static stuff. Which kinda looks ugly since if other thread dares to touch the same Vec3 static, bad things could happen.

 

 

 

I guess it depends on experience? I looked at that line and it made perfect sense to me. "Vec3" tells me to expect x,y and z coordinates. A "d" in the variable name tells me it is a delta variable. He's constructing a new vector from an old vector, while translating it by delta values scaled by time. Without looking at the video, I would say this is part of a 3D animation system.

 

Where is the new vector going? It's not assigning a return value to anything. I wonder, is in unfinished code, or does Minecraft have global / thread local temp variables to avoid allocations?

 

As far as I know, in Java, if something is static, its static to that class in that applicaton, so no "thread globals" (unless I misunderstood what you're saying).

Edited by TheChubu

Share this post


Link to post
Share on other sites

A 'coordinate plane' is the plane formed by each *pair* of axis.



Individually, the components on a vector are referred to as 'coordinates'.
 

 

Coordinate Planes*. My mistake.

 

Nevermind...

Edited by Dragonsoulj

Share this post


Link to post
Share on other sites

I guess it depends on experience? I looked at that line and it made perfect sense to me. "Vec3" tells me to expect x,y and z coordinates. A "d" in the variable name tells me it is a delta variable. He's constructing a new vector from an old vector, while translating it by delta values scaled by time. Without looking at the video, I would say this is part of a 3D animation system.

Yeah it would depend on experience. I'm still a CS undergraduate in my senior year. Never been exposed to the production environment.

Share this post


Link to post
Share on other sites


This is a C++ constructor:

 

But he is coding in Java not C++.

 

Based on coding convention, newTemp looks more like a static method with 3 parameters of his Vec3 class.

 

Right?

Share this post


Link to post
Share on other sites
Yeah, ignore me today. I don't know why, but I'm not at my best today. I was off am nearly every count.

Share this post


Link to post
Share on other sites

As far as I know, in Java, if something is static, its static to that class in that applicaton, so no "thread globals" (unless I misunderstood what you're saying).

 

 

This what I meant by thread local (not to imply I have any idea how Vec3.newTemp is really implemented):

public class Vec3 {
 
public Vec3() {
    x = y = z = 0.0f;
}
 
private float x, y, z;
 
private static final ThreadLocal<Vec3> temp = new ThreadLocal<Vec3>() {
    @Override protected Vec3 initialValue() {
        return new Vec3();
    }
};
 
public static void newTemp(float x, float y, float z)
{
    Vec3 v = temp.get();
    v.x = x;
    v.y = y;
    v.z = z;
}
}
 

Share this post


Link to post
Share on other sites

Yeah, ignore me today. I don't know why, but I'm not at my best today. I was off am nearly every count.

your initial post was good. I appreciate everyone's feedback!

Share this post


Link to post
Share on other sites


If it returns a new Vec3, then it isn't getting assigned to anything. And if it doesn't returns a new Vec3, it looks like a global static method that does something on other static stuff. Which kinda looks ugly since if other thread dares to touch the same Vec3 static, bad things could happen.

 

It's common practice among Java game developers to keep an internal temporary vector in some classes to avoid newing them all over the place. Given the number of them often needed in a single frame, it can put a bit too much pressure on the GC in some cases. I've seen different approaches to how they're implemented. Most commonly its as a normal class member in the class that needs it. I assume that Marcus has implemented one in this case as a static member of his Vec3 class. Of course it isn't thread-safe, but I seriously doubt that's an issue in for him. Static members and methods that mutate state are common in Java, so any Java programmer worth his salt knows when to use them and when not to. If he needed multi-threaded access, he would have chosen a different approach.

Share this post


Link to post
Share on other sites

 

As far as I know, in Java, if something is static, its static to that class in that applicaton, so no "thread globals" (unless I misunderstood what you're saying).

 

 

This what I meant by thread local (not to imply I have any idea how Vec3.newTemp is really implemented):

public class Vec3 {
 
public Vec3() {
    x = y = z = 0.0f;
}
 
private float x, y, z;
 
private static final ThreadLocal<Vec3> temp = new ThreadLocal<Vec3>() {
    @Override protected Vec3 initialValue() {
        return new Vec3();
    }
};
 
public static void newTemp(float x, float y, float z)
{
    Vec3 v = temp.get();
    v.x = x;
    v.y = y;
    v.z = z;
}
}
 

 

I never knew such thing existed (its no surprise since I never delved beyond the "extend Thread or implement Runnable" in Java). Looking at the Javadocs, it looks like a pretty nice feature.

 

 

It's common practice among Java game developers to keep an internal temporary vector in some classes to avoid newing them all over the place. Given the number of them often needed in a single frame, it can put a bit too much pressure on the GC in some cases. I've seen different approaches to how they're implemented. Most commonly its as a normal class member in the class that needs it. I assume that Marcus has implemented one in this case as a static member of his Vec3 class. Of course it isn't thread-safe, but I seriously doubt that's an issue in for him. Static members and methods that mutate state are common in Java, so any Java programmer worth his salt knows when to use them and when not to. If he needed multi-threaded access, he would have chosen a different approach.

I know, probably the most used static around is a Random instance. But it sounds weird to place it directly on the Vec3 class instead of, as you said, the class that needs it.

 

I mentioned threading because from what i've read around, in voxel engines its pretty common to do multi threaded stuff (ie, generating chunks in a secondary thread), I have no idea if Minecraft follows such practices though.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement