Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


P0jahn

Member Since 21 Nov 2012
Offline Last Active Nov 10 2014 04:36 PM

Posts I've Made

In Topic: Coding a chain for a 2D Game

10 November 2014 - 11:40 AM

Never mind the gaps. I dont want to calculate the number of links, it should be a fixed value like Randy Gaul suggested.

 

From the code posted above, I switched line from:

Vector2 linkPos = endPoint1.lerp(endPoint2, (float)i/(float)links);

to:

Vector2 linkPos = endPoint1.lerp(endPoint2, (float)i/(float)(links-1));

 

And I got the results I wanted: One chain always on the start point and an other always at the end point.

 

The problem is that the distance between the chains are really odd.

Attached File  inconsistensy.png   6.57KB   0 downloads

 

Why do the links at the left clot but not those at right?

 

Edit: Never mind. It was an typo. Works great :)


In Topic: Coding a chain for a 2D Game

10 November 2014 - 10:16 AM

unbird: Thanks, I will try to complete those tomorrow. 

 

As for the original question, I did kinda what you suggested, but the distance between the chains are not consistent.

 

		for(int i = 0; i < links; i++)
		{
			Vector2 linkPos = endPoint1.lerp(endPoint2, (float)i/(float)links);
			batch.draw(chainImage, linkPos.x, linkPos.y);
		}

I also dont see why I need to subtract one link?


In Topic: Coding a chain for a 2D Game

09 November 2014 - 05:16 PM

Thanks.

 

How do I calculate the number of links(the position of the points can change at any time)? Also, if I want gap between the links?


In Topic: Detecting tile collisions in a 2D platformer

29 September 2014 - 02:31 PM

Like most people already explained, use an two dimensional array with either an object type or byte.

public static final byte SOLID = 0;
public static final byte HOLLOW = 1;

byte[][] worldTiles = new byte[stageHeight][stageWidth];

And when to check for collision, the fastest way is to treat all entities as rectangles(this is often enough). You then check if the border of the rectangle is colliding with solid tile.

	//Checks if the specified GameObject collides with the given tile
	public boolean collidesWithTile(byte tileType, GameObject go)
	{
		int 	x  = (int) go.x,
			y  = (int) go.y,
			x2 = (int) (go.x + go.width),
		    	y2 = (int) (go.y + go.height);
		
		for(int lx = x; lx < x2; lx++)
		{
			if(worldTiles[y][lx] == tileType || worldTiles[y2][lx] == tileType)
				return true;
		}
		for(int ly = y; ly < y2; ly++)
		{
			if(worldTiles[ly][x] == tileType || worldTiles[ly][l2] == tileType)
				return true;
		}
		return false;
	}

go.x++;
boolean canGoRight = collidesWithTile(HOLLOW, go);
go.x--;

The great thing about byte array is that they take so little space in the ram. An map with 25 million tiles wont take more than 100mb ram.

You could also use a bunch of references instead of byte, which should take less space than byte.


In Topic: Calculate points of rotated shape

18 September 2014 - 05:18 PM

The example below returns 11.72, 11.72:

		double rot = Math.toRadians(45);
		float unitX = 40;
		float unitY = 0;
		
		float x = unitX - unitX;
		float y = unitY - unitX;
		
		y = Math.abs(y);
		
		float resX = (float) (x * Math.cos(rot) - y * Math.sin(rot));
		float resY = (float) (y * Math.cos(rot) + x * Math.sin(rot));
		
		resY = -resY;
		resX += unitX;
		resY += unitX;
		
		System.out.println(resX + " " + resY);

But when used in the game, the results are a bit off.

Ingame, replacing 40 and 0 with the entity´s position in the world gave wrong results. 
Furthermore, tried replacing them with width and height, and at the end, add the entity´s position. Gave better results, but was about 10 pixels to much:

	private Point2D.Float getSpawnPos(int rotation)
	{
		double rot = Math.toRadians(rotation);
		float unitX = width; //Entity´s width
		float unitY = height; //Entity´s height
		
		float x = unitX - unitX;
		float y = unitY - unitX;
		
		y = Math.abs(y);
		
		float resX = (float) (x * Math.cos(rot) - y * Math.sin(rot));
		float resY = (float) (y * Math.cos(rot) + x * Math.sin(rot));
		
		resY = -resY;
		resX += unitX;
		resY += unitX;
		
		resX += currX;//Entity´s position(x)
		resY += currY;//Entity´s position(y)
		
		return new Point2D.Float(resX, resY);
	}

PARTNERS