Jump to content

  • Log In with Google      Sign In   
  • Create Account


PixelPrime11

Member Since 08 Mar 2011
Offline Last Active Nov 13 2012 01:45 AM

Topics I've Started

Static self referencing classes, a good idea or not?

10 November 2012 - 05:53 AM

This is a general OOP question, as I didn't know if this is an 'accepted' practice in programming!

I have some classes in my application that only ever get instantiated once - for example, a class that defines application settings and properties. However, when I want to reference this class instance from another class, I'm kinda stumped because I don't have a visible reference to my class instance.

Sure, I could pass the class instance reference into the functions that need it, but that seems ugly.

Instead, I've added a static reference to the class instance, and can therefore refer to that instance from any other class.

Example:

[source lang="java"]public class Settings{ // static reference to a settings object public static Settings ref; public Settings() { // apply a self-reference settings = self; } private void doSomething() { ... }}[/source]

So, from another class file entirely, I can refer to my single Settings class instance by using the in-built static reference, giving me access to its private methods:

[source lang="java"]Settings.ref.doSomething();[/source]

Is this an acceptible method of doing this? Or is this type of self-referencing frowned upon? Should I just make all required 'public' members and methods in the class static instead?

[source lang="java"]public class Settings{ public Settings() { // do nothing } public static void doSomething() { ... }}[/source]

I'm looking forward to some insight on this!

Thanks all!

Matrix operation order causing positioning problems!

09 November 2012 - 11:07 AM

Hi All,

I'm currently having some positioning problems using OpenGL and LWJGL, but figured this would be generic enough for the OGL forum!

I wanted to know whether my method for positioning objects in the world is correct, as I'm not sure my matrix operations are in the right order.

For every object that's placed in the world, I'm doing the following:
  • Load the Identity Matrix (reset everything)
  • Translate to the camera's X, Y, Z position
  • Rotate the camera around the X and Y axes (I'm not doing Z rolling)
  • Translate to the object's X, Y, Z position
  • Rotate the object around the X, Y, Z axes.


I keep tweaking things and somehow getting it to work, but I'd like to know the de-facto method for accomplishing this. I'm aware that you're normally supposed to rotate before a translation, but I think it works different in LWJGL? I might be wrong on this, though.

Every object has its own X, Y, Z position in the world, as has the camera. But the whole interation between them has confused me to no end now!

Thanks for any assistance anyone can offer : )

Matrix operations in LWJGL, odd coordinate problems

09 November 2012 - 09:01 AM

Hi fellow coders,

I'm having real issues with identifying the 'player' position in 3D space in relation to other objects in the world.

In the render pass, this happens:

[source lang="java"]public void render(){ float halfBlock = BLOCKSIZE / 2f; float originX = (position.x * BLOCKSIZE) - halfBlock; float originY = (position.y * BLOCKSIZE) - halfBlock; float originZ = (position.z * BLOCKSIZE) - halfBlock; glPushMatrix(); // bind the sides texture texForSides.bind(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glBegin(GL_QUADS); // back face glColor3f(lightBack, lightBack, lightBack); glTexCoord2d(1, 0); glVertex3f(originX, originY + BLOCKSIZE, originZ); glTexCoord2d(0, 0); glVertex3f(originX + BLOCKSIZE, originY + BLOCKSIZE, originZ); glTexCoord2d(0, 1); glVertex3f(originX + BLOCKSIZE, originY, originZ); glTexCoord2d(1, 1); glVertex3f(originX, originY, originZ); // front face glColor3f(lightFront, lightFront, lightFront); glTexCoord2d(0, 1); glVertex3f(originX, originY, originZ + BLOCKSIZE); glTexCoord2d(1, 1); glVertex3f(originX + BLOCKSIZE, originY, originZ + BLOCKSIZE); glTexCoord2d(1, 0); glVertex3f(originX + BLOCKSIZE, originY + BLOCKSIZE, originZ + BLOCKSIZE); glTexCoord2d(0, 0); glVertex3f(originX, originY + BLOCKSIZE, originZ + BLOCKSIZE); // left face glColor3f(lightLeft, lightLeft, lightLeft); glTexCoord2d(1, 1); glVertex3f(originX, originY, originZ + BLOCKSIZE); glTexCoord2d(1, 0); glVertex3f(originX, originY + BLOCKSIZE, originZ + BLOCKSIZE); glTexCoord2d(0, 0); glVertex3f(originX, originY + BLOCKSIZE, originZ); glTexCoord2d(0, 1); glVertex3f(originX, originY, originZ); // right face glColor3f(lightRight, lightRight, lightRight); glTexCoord2d(1, 1); glVertex3f(originX + BLOCKSIZE, originY, originZ); glTexCoord2d(1, 0); glVertex3f(originX + BLOCKSIZE, originY + BLOCKSIZE, originZ); glTexCoord2d(0, 0); glVertex3f(originX + BLOCKSIZE, originY + BLOCKSIZE, originZ + BLOCKSIZE); glTexCoord2d(0, 1); glVertex3f(originX + BLOCKSIZE, originY, originZ + BLOCKSIZE); glEnd(); // bind the top texture texForBottom.bind(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glBegin(GL_QUADS); // bottom face glColor3f(lightBottom, lightBottom, lightBottom); glTexCoord2d(0, 0); glVertex3f(originX + BLOCKSIZE, originY, originZ); glTexCoord2d(1, 0); glVertex3f(originX + BLOCKSIZE, originY, originZ + BLOCKSIZE); glTexCoord2d(1, 1); glVertex3f(originX, originY, originZ + BLOCKSIZE); glTexCoord2d(0, 1); glVertex3f(originX, originY, originZ); glEnd(); // bind the bottom texture texForTop.bind(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glBegin(GL_QUADS); // top face glColor3f(lightTop, lightTop, lightTop); glTexCoord2d(0, 0); glVertex3f(originX, originY + BLOCKSIZE, originZ); glTexCoord2d(1, 0); glVertex3f(originX, originY + BLOCKSIZE, originZ + BLOCKSIZE); glTexCoord2d(1, 1); glVertex3f(originX + BLOCKSIZE, originY + BLOCKSIZE, originZ + BLOCKSIZE); glTexCoord2d(0, 1); glVertex3f(originX + BLOCKSIZE, originY + BLOCKSIZE, originZ); glEnd(); glPopMatrix();}[/source]

The following shows the code that renders a block into the world:

[source lang="java"]public void render(){ float halfBlock = BLOCKSIZE / 2f; float originX = (position.x * BLOCKSIZE) - halfBlock; float originY = (position.y * BLOCKSIZE) - halfBlock; float originZ = (position.z * BLOCKSIZE) - halfBlock; glPushMatrix(); // bind the sides texture texForSides.bind(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glBegin(GL_QUADS); // back face glColor3f(lightBack, lightBack, lightBack); glTexCoord2d(1, 0); glVertex3f(originX, originY + BLOCKSIZE, originZ); glTexCoord2d(0, 0); glVertex3f(originX + BLOCKSIZE, originY + BLOCKSIZE, originZ); glTexCoord2d(0, 1); glVertex3f(originX + BLOCKSIZE, originY, originZ); glTexCoord2d(1, 1); glVertex3f(originX, originY, originZ); // front face glColor3f(lightFront, lightFront, lightFront); glTexCoord2d(0, 1); glVertex3f(originX, originY, originZ + BLOCKSIZE); glTexCoord2d(1, 1); glVertex3f(originX + BLOCKSIZE, originY, originZ + BLOCKSIZE); glTexCoord2d(1, 0); glVertex3f(originX + BLOCKSIZE, originY + BLOCKSIZE, originZ + BLOCKSIZE); glTexCoord2d(0, 0); glVertex3f(originX, originY + BLOCKSIZE, originZ + BLOCKSIZE); // left face glColor3f(lightLeft, lightLeft, lightLeft); glTexCoord2d(1, 1); glVertex3f(originX, originY, originZ + BLOCKSIZE); glTexCoord2d(1, 0); glVertex3f(originX, originY + BLOCKSIZE, originZ + BLOCKSIZE); glTexCoord2d(0, 0); glVertex3f(originX, originY + BLOCKSIZE, originZ); glTexCoord2d(0, 1); glVertex3f(originX, originY, originZ); // right face glColor3f(lightRight, lightRight, lightRight); glTexCoord2d(1, 1); glVertex3f(originX + BLOCKSIZE, originY, originZ); glTexCoord2d(1, 0); glVertex3f(originX + BLOCKSIZE, originY + BLOCKSIZE, originZ); glTexCoord2d(0, 0); glVertex3f(originX + BLOCKSIZE, originY + BLOCKSIZE, originZ + BLOCKSIZE); glTexCoord2d(0, 1); glVertex3f(originX + BLOCKSIZE, originY, originZ + BLOCKSIZE); glEnd(); // bind the top texture texForBottom.bind(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glBegin(GL_QUADS); // bottom face glColor3f(lightBottom, lightBottom, lightBottom); glTexCoord2d(0, 0); glVertex3f(originX + BLOCKSIZE, originY, originZ); glTexCoord2d(1, 0); glVertex3f(originX + BLOCKSIZE, originY, originZ + BLOCKSIZE); glTexCoord2d(1, 1); glVertex3f(originX, originY, originZ + BLOCKSIZE); glTexCoord2d(0, 1); glVertex3f(originX, originY, originZ); glEnd(); // bind the bottom texture texForTop.bind(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glBegin(GL_QUADS); // top face glColor3f(lightTop, lightTop, lightTop); glTexCoord2d(0, 0); glVertex3f(originX, originY + BLOCKSIZE, originZ); glTexCoord2d(1, 0); glVertex3f(originX, originY + BLOCKSIZE, originZ + BLOCKSIZE); glTexCoord2d(1, 1); glVertex3f(originX + BLOCKSIZE, originY + BLOCKSIZE, originZ + BLOCKSIZE); glTexCoord2d(0, 1); glVertex3f(originX + BLOCKSIZE, originY + BLOCKSIZE, originZ); glEnd(); glPopMatrix();}[/source]

Everything seems to render fine, and I'm able to navigate around the world by manipulating the camera position.

I have a method inside of my Block class (shown below) that checks to see whether a given Vector3f point in space is within the block's volume. The block is drawn from the middle, so 0,0,0 would be at the center of the cube.

For example, a block created at grid location (0, 0, -8) reports the following data when the player is 'inside' the cube:

Camera position: [-0.03],[-0.14],[-0.03]
Block: [-0.5 > 0.5],[-0.5 > 0.5],[-8.5 > -7.5]

N.B. The 'Block' line above shows the bounds for each axis (X min = -0.5, X max = 0.5, etc.).

I can't figure out why the camera isn't showing similar coords to the block Posted Image

Here's the Block class method for determining if a point in space is within its own volume:

[source lang="java"]public boolean isPointInside(Vector3f point){ float minX = position.x - (BLOCKSIZE / 2); float maxX = position.x + (BLOCKSIZE / 2); float minY = position.y - (BLOCKSIZE / 2); float maxY = position.y + (BLOCKSIZE / 2); float minZ = position.z - (BLOCKSIZE / 2); float maxZ = position.z + (BLOCKSIZE / 2); System.out.println("Block: [" + minX + " > " + maxX + "],[" + minY + " > " + maxY + "],[" + minZ + " > " + maxZ + "]"); if (point.x > minX && point.y > minY && point.z > minZ) { if (point.x < maxX && point.y < maxY && point.z < maxZ) { return true; } } return false;}[/source]

Any assistance with this would be greatly appreciated.

P.S. I know there are likely to be some heinous mistakes or irregularities with my code. I'm new to 3D Java programming (and LWJGL), so I'm likely to be making mistakes at the moment!

2D Platform edge collisions

17 October 2011 - 08:29 AM

Hi All, I'm wondering if someone can help offer some insight into a very frustrating problem I'm having.

I've currently written a small platform test demo, but some of the collision detection is causing strange movement problems (teleporting to the tops of blocks, sticking to the sides etc.).

The player is moved every frame using inertia / velocity values, as such:

Player.x += speedX;
Player.y += speedY;

Before the player movement is updated, however, I run a collision check against all blocks in the level. Naturally, in the final version I'll be limiting the number of these checks to only nearby objects, but I need the theory to work first!

When testing collison, I first run a quick exclusion text to see if the player and block are going to intersect:

function WillIntersect(block)
{
var newX = Player.x + speedX;
var newY = Player.y + speedY;

if (Player.x > block.x + block.width || Player.y > block.y + block.height || Player.x + Player.width < clip.x || Player,y + Player.height < clip.y)
{
// no intersection, thus no collision
return false;
}
else
{
// the two object bounding boxes intersect in some way
return true;
}
}

Then, as a result of this check, I then move into a collision detection method that attempts to resolve the collision that occured.

The following rules persist for movement around the game level:

  • If no intersection was detected with any object, the speedX and speedY values will remain unmodified.
  • If the player falls onto the top of a block, the speedY value is zeroed-out and they are repositioned on top of the block with pixel precision
  • If the player hits the underside of a block, the speedY value is zeroed-out and they are repositioned directly underneath the block
  • If the player hits the left, or right side of a block, the speedX value is zeroed-out and they are repositioned to the relevant edge.
The problem I have is when a combination of these factors occur. When the player jumps sideways against a wall (or the edge of a platform), the player will 'stick' to the vertical surface because the code generating the 'push to top' or 'push to bottom' check is running and canceling out any other checks.

Video of the effect in question:
http://www.youtube.c...h?v=nFusvi8U_-A

Finally, here's the code I'm currently using for the collisions. I've been tearing my hair out over this for over a week now, and just need some fresh viewpoints - you know what it's like when you've been staring at the same problem for days on end!

function DoCollisions()
{
	txtOutput.text = "";
	
	var newX = Player.x + speedX;
	var newY = Player.y + speedY;
	
	for (var i = 0; i < Level.numChildren; i++)
	{
		var clip = Level.getChildAt(i);
		
		if (getQualifiedClassName(clip) == "BLOCK")
		{
			if (WillIntersect(clip))
			{
				if (speedX > 0)
				{
					speedX = 0;
					Player.x = clip.x - Player.width;
				}
				else if (speedX < 0)
				{
					speedX = 0;
					Player.x = clip.x + clip.width;
				}
				else { }
				
				if (speedY > 0)
				{
					speedY = 0;
					Player.y = clip.y - Player.height;
				}
				else if (speedY < 0)
				{
					speedY = 0;
					Player.y = clip.y + clip.height;
				}
				else { }
			}
		}
	}
}

Thanks to anyone who can offer some advice here.

PARTNERS