8 Replies - 1370 Views - Last Post: 22 May 2013 - 06:02 AM Rate Topic: -----

#1 letmegethistraight   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-May 13

Stuck at moving bullets pls help

Posted 21 May 2013 - 06:04 AM

Im a little stuck in moving bullets. here is the link of my demo. Before u test please extract it first on the same folder. If you could run the demo you can see that the bullet is moving base on the cursor but once release i am expecting that the release bullet would not get the angle of my cursor if i moved it to another direction, but its not. The release bullet keeps following the direction of my cursor.


public class VectorSample extends GDX_TEST implements InputProcessor {

    Texture ball,bullet,guider;
    OrthographicCamera camera;
    Vector2 vector = new Vector2();
    Vector2 vectoralt = new Vector2();

    long lastDropTime;
    SpriteBatch batch;
    Rectangle rect = new Rectangle();
    float angle = 0,anglealt = 0;
    private ShapeRenderer renderer;
    TextureRegion tr;
    Sprite sprite,sprite2;


    ShapeRenderer sr;
    Ship ship;

    Vector2 nvec;

    protected Vector2 center = new Vector2();
    Array<Vector2> bullets;
    Array<Rectangle> bulletsRect;

    Boolean fire = false;
    Rectangle tmpRect = new Rectangle();
    float guideAngle = 0;

    double angletry = 0;

    float velocity_x =0;
    float velocity_y = 0;

    Vector2 bulletxy = new Vector2();




    @Override
    public void create() {
        renderer = new ShapeRenderer();
        ball = new Texture(Gdx.files.internal("data/player.png"));

        bullet = new Texture("data/ball_black.png");

        tr = new TextureRegion(ball);

        camera = new OrthographicCamera();
        camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        ship = new Ship(new Vector2(5, 50), 1, 1, 0, 5f);
        sr = new ShapeRenderer();

        batch = new SpriteBatch();
        Gdx.input.setInputProcessor(this);

        rect.width = ball.getWidth();
        rect.height = ball.getHeight();
        vector.add(200,200);


        sprite = new Sprite(ball);

        vectoralt.add(200 + sprite.getWidth(),200);



        bullets = new Array<Vector2>();
        getCenter();


        bulletsRect = new Array<Rectangle>();
        bulletsRect2 = new Array<BallShit>();
        fireatwill2();


    }

    @Override
    public void render() {



        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        camera.update();
        batch.setProjectionMatrix(camera.combined);


        batch.begin();
        {

            batch.draw(sprite,vector.x,vector.y,sprite.getWidth()/2,sprite.getHeight()/2,sprite.getWidth(),sprite.getHeight(),1,1,angle);


        }

        batch.end();
        if(fire == true){


            for(Rectangle raindrop: bulletsRect) {
                batch.begin();
                batch.draw(bullet,raindrop.x,raindrop.y);
                batch.end();
            }
        }

        iterateshit(fire);

        if(Gdx.input.isKeyPressed(Input.Keys.D)){
            vector.add(1,0);
            if(vector.x > Gdx.graphics.getWidth()){
                Gdx.app.log("x","x");
            }
        }
        if(Gdx.input.isKeyPressed(Input.Keys.A)){
            vector.add(-1,0);
            if(vector.x < 0){

            }
        }

        float w = camera.frustum.planePoints[1].x - camera.frustum.planePoints[0].x;
        float distance =   w - (vector.x + sprite.getWidth());
        if(distance <=0){
            vector.x = w - sprite.getWidth();
        }

        if(distance >= Gdx.graphics.getWidth() - sprite.getWidth()) {
            vector.x = 0;
        }

        if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
            fire = true;
            guideAngle = getAngle();
            if(TimeUtils.nanoTime() - lastDropTime > 100000000) fireatwill2();

        }

    }

    private float getAngle(){
        float angle = findAngle(Gdx.input.getX(),
                Gdx.graphics.getHeight() - Gdx.input.getY());


       return angle;
    }

    public void iterateshit(boolean release){

        if(release == true){
            Iterator<Rectangle> iter = bulletsRect.iterator();

            while(iter.hasNext()) {
                Rectangle raindrop = iter.next();

                raindrop.x += velocity_x;
                raindrop.y += velocity_y;

                Gdx.app.log("voila","voila");
                /*if(raindrop.y  < 0) iter.remove();
                if(raindrop.y  > Gdx.graphics.getHeight() || raindrop.x  > Gdx.graphics.getWidth()) iter.remove();*/
            }

        }
    }


    @Override
    public boolean mouseMoved(int screenX, int screenY) {

        //getting the angle
        float angle = findAngle(Gdx.input.getX() ,
                Gdx.graphics.getHeight() - Gdx.input.getY());

        this.angle = angle % 360;


        return  true;
    }

    private void fireatwill2() {


        Rectangle raindrop = new Rectangle();


        raindrop.x =   vector.x + sprite.getWidth() / 2 - (bullet.getWidth() / 2);
        raindrop.y =    vector.y + sprite.getHeight() / 2 - (bullet.getHeight() / 2);



        angletry = guideAngle * MathUtils.degreesToRadians;
        float speed = 5;
        float scale_x = (float)Math.cos(angletry);
        float scale_y = (float)Math.sin(angletry);
        velocity_x = (speed* scale_x);
        velocity_y = (speed* scale_y);
        bulletxy.add(velocity_x,velocity_y);

        bulletsRect.add(raindrop);

        lastDropTime = TimeUtils.nanoTime();

    }

    public Vector2 getCenter() {
        center.x = vector.x + sprite.getWidth() / 2;
        center.y = vector.y + sprite.getHeight() / 2;
        return center.cpy();
    }


    @Override
    public void pause() {
        super.pause();    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public void resume() {
        super.resume();    //To change body of overridden methods use File | Settings | File Templates.
    }

    public float findAngle(float x1, float y1) {
        Vector2 center = getCenter();
        float x0 = center.x;
        float y0 = center.y;

        float a = MathUtils.atan2(y1 - y0, x1 - x0);

        return a * MathUtils.radiansToDegrees;
    }
}


Although I am using a game framework named LIBGDX, I believe the logic is the same. Pls help. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Stuck at moving bullets pls help

#2 Kainyu   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 14-March 13

Re: Stuck at moving bullets pls help

Posted 21 May 2013 - 06:46 AM

Comments would be nice.. :dontgetit:
I cant really figure out what you want and I kinda gave up reading once I saw "iterateshit" as a method :sweatdrop: do you want the bullet to fire at an angle depending on the position of the mouse or not?

Quote

i am expecting that the release bullet would not get the angle of my cursor if i moved it to another direction, but its not


Can you rephrase please? And maybe add comments next time you post code its really really helpful for you and other people :)
Was This Post Helpful? 0
  • +
  • -

#3 letmegethistraight   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-May 13

Re: Stuck at moving bullets pls help

Posted 21 May 2013 - 07:43 AM

View PostKainyu, on 21 May 2013 - 06:46 AM, said:

Comments would be nice.. :dontgetit:/>
I cant really figure out what you want and I kinda gave up reading once I saw "iterateshit" as a method :sweatdrop:/> do you want the bullet to fire at an angle depending on the position of the mouse or not?

Quote

i am expecting that the release bullet would not get the angle of my cursor if i moved it to another direction, but its not


Can you rephrase please? And maybe add comments next time you post code its really really helpful for you and other people :)/>


Really sorry about that. Im really frustrated because i cant get it right. Im not that good in English so sorry in advance. Here's what i think. When i press the button left, lets say my tank would fire a bullet base on where my tank is directed at. The direction of my tank is base on where my cursor is. Once the bullet is release lets say this is bullet_1, i change my direction and fired again so the release bullet is bullet_2. The problem is that bullet_1 followed the direction of bullet_2 instead of keeping its direction straight. Another way to look at it is the bullet_1 follows the direction of bullet_2 or you can say the direction in which tank is pointed at when i fired the bullet_2. Ive been finding a solution for days but cant get it right. I hope this clarifies everything. Really sorry for my bad english.
Was This Post Helpful? 0
  • +
  • -

#4 letmegethistraight   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-May 13

Re: Stuck at moving bullets pls help

Posted 21 May 2013 - 08:02 AM

View PostKainyu, on 21 May 2013 - 06:46 AM, said:

Comments would be nice.. :dontgetit:/>
I cant really figure out what you want and I kinda gave up reading once I saw "iterateshit" as a method :sweatdrop:/> do you want the bullet to fire at an angle depending on the position of the mouse or not?

Quote

i am expecting that the release bullet would not get the angle of my cursor if i moved it to another direction, but its not


Can you rephrase please? And maybe add comments next time you post code its really really helpful for you and other people :)/>


ahm here i will gave u the source code, the attach file here is not working so i use mediafire.
Was This Post Helpful? 0
  • +
  • -

#5 anonymous26   User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Stuck at moving bullets pls help

Posted 21 May 2013 - 03:48 PM

iterateshit()?! lmao

That is really good.
Was This Post Helpful? 0
  • +
  • -

#6 letmegethistraight   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-May 13

Re: Stuck at moving bullets pls help

Posted 21 May 2013 - 07:30 PM

View PostButchDean, on 21 May 2013 - 03:48 PM, said:

iterateshit()?! lmao

That is really good.


Sorry :oops: can you help me about my problem?
Was This Post Helpful? 0
  • +
  • -

#7 Kainyu   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 14-March 13

Re: Stuck at moving bullets pls help

Posted 22 May 2013 - 01:46 AM

I cant actually download or view the game as i'm at work and I cant understand your code (sorry) but it sounds to me like you either haven't kept track of your mouse at the time of creation (when bullets are shot) or you've redefined it somewhere.

I have also read your other post and seen you are new to programming games I think it might be smart for you to follow some tutorial instead of diving in head first I haven't used LIBGDX before but don't you think using eclipse would be better if your just starting off if you know java?
There are hundreds of guides and videos floating around the place advanced and simple. Maybe starting off with something on your own is a bit much.

RealTutsGML and bucky are the two names that come to mind when I think of a good place to start if your looking for a video.

If your more of a reader:
http://www.dreaminco...toolsresources/
http://www.dreaminco...ming-tutorials/

If you don't wish to do that and stay with it then i'm sure there is some tutorials on the libgdx forum?
http://www.badlogicgames.com/forum/ (think that's it)

I hope you fix your problem.
Was This Post Helpful? 1
  • +
  • -

#8 letmegethistraight   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-May 13

Re: Stuck at moving bullets pls help

Posted 22 May 2013 - 05:55 AM

View PostKainyu, on 22 May 2013 - 01:46 AM, said:

I cant actually download or view the game as i'm at work and I cant understand your code (sorry) but it sounds to me like you either haven't kept track of your mouse at the time of creation (when bullets are shot) or you've redefined it somewhere.

I have also read your other post and seen you are new to programming games I think it might be smart for you to follow some tutorial instead of diving in head first I haven't used LIBGDX before but don't you think using eclipse would be better if your just starting off if you know java?
There are hundreds of guides and videos floating around the place advanced and simple. Maybe starting off with something on your own is a bit much.

RealTutsGML and bucky are the two names that come to mind when I think of a good place to start if your looking for a video.

If your more of a reader:
http://www.dreaminco...toolsresources/
http://www.dreaminco...ming-tutorials/

If you don't wish to do that and stay with it then i'm sure there is some tutorials on the libgdx forum?
http://www.badlogicgames.com/forum/ (think that's it)

I hope you fix your problem.


I see. I guest i have to abandon my project temporarily and find some tutorials and have a better understanding on game development. I just thought diving in to game development with a little knowledge will help me better understand it though. But thanks anyway. Maybe ill take bucky's game development on java. :rockon:
Was This Post Helpful? 0
  • +
  • -

#9 letmegethistraight   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-May 13

Re: Stuck at moving bullets pls help

Posted 22 May 2013 - 06:02 AM

View PostKainyu, on 22 May 2013 - 01:46 AM, said:

but don't you think using eclipse would be better if your just starting off if you know java?


I am much comfortable in intellij idea. Much faster and its like a VS to me :sorcerer:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1