• Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at \$59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.

Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!

# P0jahn

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

### Coding a chain for a 2D Game

09 November 2014 - 03:58 PM

I want to code a chain for my game and am not sure how to do it so I am hoping to get some suggestions that get me started.

Details:

You create a chain object and specify two points: start and end point.

The image of the chain object is one piece of the chain. Example:

So it should look something like this ingame:

Ideas?

### Formula to calculate when a point is reached

14 October 2014 - 07:51 PM

I am developing a 2D game where x is increasing rightwards and y downwards. Thus, the top-left corner is 0,0.

I have an enemy that moves towards a set of waypoints. When one is reached, it will start moving towards the next waypoint in the list.

I use the following code to move towards a point:

```	Vector2 norP = normalize(this.x, this.y, target.x, target.y);

float accelx = thrust * -norP.x - drag * vx;
float accely = thrust * -norP.y - drag * vy;

vx = vx + delta * accelx;
vy = vy + delta * accely;

this.x += delta * vx;
this.y += delta * vy;
```

That piece works fine. The problem is detecting when a point has been reached.

Right now, I use the following:

```if(distance(this.x, this.y, target.x, target.y) < 25)
{
...
```

The value 25 is a fixed value and works sometimes. As you alter thrust and drag properties, the value 25 stops being effective.

So I need a formula that calculates this value that works good no matter what thrust and drag is(taking them into consideration).

### Calculate points of rotated shape

17 September 2014 - 06:07 PM

I have a shape(width and height always the same), looking like this:

I then rotate it by 45 degrees and want to calculate the edge of each pillar.

The top-left corner of the shape is x:0 y:0.

### [2D]Make a camera follow multiple characters.

24 August 2014 - 03:47 PM

I am developing a 2D game(platformer) where to top-left corner is x:0,y:0. As you move rightwards, the x coordinate increase and y increasing when moving downwards(in other words, standard coordinate system for 2D games).

As in every platformer, the camera is focused on the main character, following her/him. Some games however have more than one main character. In these games, the camera would zoom out if the characters are to far away from each other and at the same time "follow" them.

I need to implement this in my game but not sure how. It need to support 1-4 characters.

The variables translateX, translateY and scale(where 1.0 is the normal size of the world) need to be set.

Language can be either Java, C#, C, C++ or anything that look alike.

### Rotate at a point

10 April 2014 - 12:41 PM

I am in process of writing my own laser drawing function and I have ran into a math problem.

Have a look at the following image:

The laser is set to fire at a certain point(the top-left corner of the red guy), which it actually does. The top-left, or the bottom-left(not sure which one) corner of the laser beam is exactly at the target coordinate.

What I want to do is to have the lasers middle point at the target point, not the corner.

Here is my code, I am using the following method to draw the laser:

```SpriteBatch.draw(TextureRegion region,
float x,
float y,
float originX,
float originY,
float width,
float height,
float scaleX,
float scaleY,
float rotation)

```

And here is my code:

```float angle = MathStuff.getAngle(srcX, srcY, destX, destY); //Calculate the angle between the source and destination point.

batch.draw(beamImage,
srcX,
srcY,
0,
0,
MathStuff.distance(srcX, srcY, destX, destY),
beamImage.getHeight(),
1,
1,
angle);
```

PARTNERS