Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Canvas: How do you make the Canvas as a chase camera for moving objects?  (Read 1600 times)
0 Members and 1 Guest are viewing this topic.
Offline tom_mai78101
« Posted 2012-08-28 16:17:37 »

Here's Java-Gaming.org pastebin of my currently managed code.

StoryTime!  Roll Eyes

I have successfully managed to create a simple chase camera. The camera itself is the Canvas. The region of the camera is the region of the Canvas. The camera (Canvas) follows a stationary unit, while other objects move in opposite directions, creating the illusion that the stationary unit is moving and the camera is "chasing" it.

But, a friend of mine points that my simplistic design is a bad design if I were to expand on this. For example, if I were to add lots of objects to my game, I would have to calculate each and every single object. The friend thinks that the Canvas could just follow around the unit itself, while the unit moves around stationary obstacles, making the game more flexible and more easier to code. So I did a rewrite, albeit unsuccessful.

It's like this diagram shown here:



The trouble I'm having is that, due to some technical difficulties, I can only obtain the center position, (x,y), of the Canvas. The map itself is loaded from a binary file, with everything from trees, to guns, to doors, etc. all fully created in the file. If I were to just load the file, my unit (the purple dot in the diagram) would spawn in at the top left corner.

The main problem, thus the question, is how do I map the Canvas's center position onto the purple dot all the time?

The hint I'm getting is that, if I can obtain the unit's position coordinates, then do something I have no clue on, I can then take the results from there, and offset all of the objects. But, wouldn't that defeat the purpose of what my friend is thinking of?  Clueless Thanks in advance.
Offline loom_weaver

JGO Coder


Medals: 17



« Reply #1 - Posted 2012-08-28 16:37:40 »

Is your code what you have now or what you had before your changes?

In either case you'll need to process most of your objects each render pass (until you add viewport culling).

I would recommend storing the camera position e.g.:
1  
2  
int cameraX;
int cameraY;


Then your main render pass would appear as such:
1  
2  
3  
4  
5  
6  
for (entity <- all entities) {
  int xOffset = entity.x - cameraX;
  int yOffset = entity.y - cameraY;

  draw entity on your Canvas at (Display.width/2 + xOffset, Display.height/2 + yOffset)
}


Now you can either keep your camera stationary (fixed values) or update its position to be the location of your tracked entity each frame.
Offline tom_mai78101
« Reply #2 - Posted 2012-08-29 06:40:41 »

Very tricky to pull it off, but I managed to successfully implement a working prototype. Thanks!  Grin Credit goes to loom_weaver.

Code for Curious:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
public void tick(RenderView view) {
      //Accelerometer static class from API
      //(x,y) = Object Position Coordinates
      //(vx,vy) = Vector of Object Velocity
      //RenderView is a subclass of Canvas. Variables "translateX" and "translateY" are
      //camera position.
      vx += Accelero.X * 0.1;
      vy += Accelero.Y * 0.1;
      x += vx;
      y += vy;
      view.translateX = x;
      view.translateY = y;
      vx *= 0.1;
      vy *= 0.1;
   }

   public void render(Canvas c, float translateX, float translateY, final float centerWidth, final float centerHeight) {
      if (bitmap == null || c == null)
         return;
      float xOffset = x - translateX;
      float yOffset = y - translateY;
      move(centerWidth + xOffset, centerHeight + yOffset);
      c.drawBitmap(bitmap, srcRect, dstRect, null);
   }
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!