Jump to content

  • Log In with Google      Sign In   
  • Create Account


Johnell

Member Since 12 Apr 2012
Offline Last Active Sep 11 2013 02:57 AM
-----

Posts I've Made

In Topic: Building C/C++ project without IDE

30 June 2013 - 06:43 PM

How is it done if I want to link my project to some 3rd party library/framework? If I'm using SDL for instance.


In Topic: 2D scrolling game

16 March 2013 - 05:30 AM

Because your cameraX is the top-right (or bottom-right) of the camera, and not the center of the camera.

 

You're mixing two different formulas into a single location:

A) You're calculating the center of the camera from the camera's position and the screen size.

B) You're calculating the player's on-screen position from his absolute (or 'world') position and the (now centered) camera.

 

I suggest you keep your camera position as the center point of the camera, I just find it easier that way. Then your function would just look like:

 

player.draw(playerX - cameraX, playerY - cameraY);

 

Further, holding a 2D 'position' is a very very common game task. It adds extra visual code clutter and extra opportunities for mistakes if for every 'position' you have two variables (one for x and one for y).

 

Instead, a position is a prime candidate for its own struct or class.

 

A 'Point' class, that contains an 'x' and 'y', and has built-in addition and subtraction functions. Instead of every function taking an 'x' and a 'y', now you just take a 'point'.

 

This further reduces and simplifies your code to:

 

player.draw(playerPos - cameraPos);

 

It has the extra same code costs, but alot less visual clutter, and alot less potential for bugs, and alot easier maintenance, and alot easier readability.

Here's the interface for my point class: http://ideone.com/lH9X3B

Here's the source file: http://ideone.com/G5aAst

 

Many people (and different libraries) have their own custom point class. It's a very useful thing to have.

I suggest you make yourself a Point, a Size, and a Rect (which contains a Point and a Size).

 

"I suggest you keep your camera position as the center point of the camera." Hmm, not sure I follow. Isn't the camera view camera.x + camera.width and camera.y + camera.height? Also I'm not sure I entirely understand the difference between world position and screen position. 

 

Say object A and B is at (50, 50) 

 

If I draw them like this:

 

A.draw(50, 50);
B.draw(50 - camera.x, 50 - camera.y);

 

A will be at (50, 50) within the screen all the time. B will be drawn at (50, 50) world position. Is the coordinates within the screen always the same?


In Topic: 2D scrolling game

15 March 2013 - 07:30 PM

I am currently centering the player this way:

 

player.draw(playerX - cameraX + (screen.width/2), playerY - cameraY + 240);

 

 

This do indeed work, although i'm confused as to why it does. Since playerX = cameraX I am just drawing it at screen.width/2. Say screen.width/2 is 400px. If cameraX is at 800 the player will still be drawn at 400px, yet still be centered in the screen. Why is that?


In Topic: 2D scrolling game

15 March 2013 - 09:51 AM

So instead of moving the player I should move the camera and just center the player in the camera?


In Topic: 2D scrolling game

15 March 2013 - 05:43 AM

I think you're going to need to explain what it is that you're not getting more precisely. Maybe if you start by explaining to us your process so far and then stop at the point where you're stuck and explain why you don't know what to do.

 

I wanna know how I can keep the player centered on the screen while moving him. I need the camera to "follow" the player so the camera's position is updated, but I dont know how I can accomplish this.


PARTNERS