Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8032145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:18:28+00:00 2026-06-05T01:18:28+00:00

I’m trying to make a camera for an android app that moves by dragging

  • 0

I’m trying to make a camera for an android app that moves by dragging on the touch screen to drag the camera across. I’m using the Cocos2D engine for my development.

The problem is, whenever you moved your finger on the screen, everything on the screen just disappears instead of moving.

My code is below, I hope someone can help me with this 🙂 Thanks for your time.

@Override
public boolean ccTouchesMoved(MotionEvent event)
        {
            CGPoint location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.getY()));
            CGPoint movement = CGPoint.ccpSub(location, previousLocation);
            previousLocation = location;
            //Update the camera
            float[] x = new float[1];
            float[] y = new float[1];
            float[] z = new float[1];
            this.getCamera().getCenter(x, y, z);
            CameraPos.x = x[0];
            CameraPos.y = y[0];
            this.getCamera().getEye(x, y, z);
            movement.x = 2 * movement.x * (1 + (z[0]/832));
            movement.y = 2 * movement.y * (1 + (z[0]/832));
            CameraPos.x = CameraPos.x - Math.round(movement.x);
            CameraPos.y = CameraPos.y - Math.round(movement.y);
            this.getCamera().setCenter(CameraPos.x, CameraPos.y, 0);
            this.getCamera().setEye(CameraPos.x, CameraPos.y, z[0]);
            return true;
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:18:29+00:00Added an answer on June 5, 2026 at 1:18 am

    Its fine I got it working nevertheless.

    For any who want to know this, I created a class called CameraControls that controls the basic functions of the camera. It isnt finished yet (I’ll probably update the code as I make changes such as zoom functionality) but this one allows me to track the touch input perfectly.

    import org.cocos2d.nodes.CCDirector;
    import org.cocos2d.types.CGPoint;
    import org.cocos2d.types.CGSize;
    
    public class CameraControls {
    
    CGSize winSize = CCDirector.sharedDirector().displaySize();
    CGPoint CameraPos = CGPoint.ccp(winSize.width, winSize.height);
    CGPoint previousLocation;
    double minX;
    double maxX;
    double minY;
    double maxY;
    
    public CameraControls(World world)
    {
        this.loadCamera(world);
    }
    
    public void setCameraLimit(float minX, float maxX, float minY, float maxY)
    {
        this.minX = minX;
        this.maxX = maxX;
        this.minY = minY;
        this.maxY = maxY;
    }
    
    public void loadCamera(World world)
    {
        float[] x = new float[1];
        float[] y = new float[1];
        float[] z = new float[1];
        world.getCamera().getCenter(x, y, z);
        CameraPos.x = x[0];
        CameraPos.y = y[0]; 
    }
    
    public void trackTouchMovement(CGPoint location, World world)
    {
        if(previousLocation == null)
        {
            previousLocation = location;
        }
        CGPoint movement = CGPoint.ccpSub(previousLocation, location);
        previousLocation = location;
        float[] x1 = new float[1];
        float[] y1 = new float[1];
        float[] z1 = new float[1];
        world.getCamera().getEye(x1, y1, z1);
        CameraPos.x = CameraPos.x + movement.x;
        CameraPos.y = CameraPos.y + movement.y;
    
        try
        {
            if(CameraPos.x >= maxX || CameraPos.x <= minX || CameraPos.y >=  maxY || CameraPos.y <= minY)
            {
                CameraPos = CGPoint.ccpSub(CameraPos, movement);
            }
        }
        catch (NullPointerException e)
        {
            System.out.println("Invalid values for camera Limits. No Limits applied.");
        }
    
        world.getCamera().setCenter(CameraPos.x, CameraPos.y, 0);
        world.getCamera().setEye(CameraPos.x, CameraPos.y, z1[0]);
    }
    
    public void storePositionAsPrevious(CGPoint pos)
    {
        previousLocation = pos;
    }
    
    public void resetPrevious()
    {
        previousLocation = null;
    }
    }
    

    Now that I have a class, I simply create an instance of CameraControls in my class and then do the necessary configuration.

    CameraControls camera = new CameraControls(this);
    

    In this case, I want the total area my camera can view to be 3 x the width of the camera, and 3 times the height of the camera, so I set the limits of the camera as the negative width of the camera, the width of the camera, the negative height of the camera and the height of the camera, as the camera starts at (0, 0).

        camera.setCameraLimit(-winSize.width, winSize.width, -winSize.height, winSize.height);
    

    Finally, I just add the necessary method calling in ccTouchesBegan, ccTouchesMoved and ccTouchesEnded

    @Override
    public boolean ccTouchesMoved(MotionEvent event)
            {
                CGPoint location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.getY()));
                camera.trackTouchMovement(location, this);
                return true;
            }
    @Override
    public boolean ccTouchesEnded(MotionEvent event)
    {
                camera.resetPrevious();
                return true;
    }
    @Override
    public boolean ccTouchesBegan(MotionEvent event)
    {
                CGPoint location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.getY()));
                camera.storePositionAsPrevious(location);
                return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to create an if statement in PHP that prevents a single post
I am using Paperclip to handle profile photo uploads in my app. They upload
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.