This site requires JavaScript, please enable it in your browser!
Greenfoot back
GreenGoo
GreenGoo wrote ...

2014/8/17

Using setLocation() or some variant with doubles?

GreenGoo GreenGoo

2014/8/17

#
I'm trying to create faithful space acceleration, but with int values it gets ridiculous. I want to set the location of my object using doubles so that it doesn't zoom off forever.
lordhershey lordhershey

2014/8/17

#
Could you not just cast it to an int? setLocation((int)doubleA,(int)doubleB);
GreenGoo GreenGoo

2014/8/18

#
It doesn't give the accuracy I'm looking for. When I only increase the velocity by .01 or so per frame, the ship accelerates in lurches.
davmac davmac

2014/8/18

#
It doesn't give the accuracy I'm looking for. When I only increase the velocity by .01 or so per frame, the ship accelerates in lurches.
If something doesn't seem to be working and you want help with it then we need to see the code! ;)
lordhershey lordhershey

2014/8/18

#
Are you using getX and getY to get and reset position?
GreenGoo GreenGoo

2014/8/18

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
double xVelocity = 0, yVelocity = 0;
    double xAcceleration = 0, yAcceleration = 0;
    double thrust = 0.01;
 
    public Starship(int rotation)
    {
        setRotation(rotation);
    }
 
    public void act()
    {
        checkKeys();
        moveStars();
        checkHit();
        weaponFireDelay++;
        xVelocity = xVelocity + xAcceleration;
        yVelocity = yVelocity + yAcceleration;
        this.setLocation(getX() + (int) xVelocity, getY() + (int) yVelocity);
    }
GreenGoo GreenGoo

2014/8/18

#
Oh, and this:
1
2
3
4
5
if (Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("w"))
        {
            xAcceleration = ((90 - this.getRotation())/90) * thrust;
            yAcceleration = (this.getRotation()/90) * thrust;
        }
davmac davmac

2014/8/18

#
Here:
1
this.setLocation(getX() + (int) xVelocity, getY() + (int) yVelocity); 
You're using the integer positions from getX() and getY(), and casting the velocity values back to integer as well, which is what is causing the issue. The easiest solution is to import and use the SmoothMover class (via the menu, Edit - Import class). If you don't want to use it you can always look at the code to see how it works.
GreenGoo GreenGoo

2014/8/18

#
I'll give it a go thanks!
GreenGoo GreenGoo

2014/8/18

#
I can't find it on the Edit tab
davmac davmac

2014/8/18

#
It's in the edit menu. I presume you're running a recent - preferably the most recent - version of Greenfoot?
danpost danpost

2014/8/18

#
Two things concern me here. (1) without the current x and y coordinates tracked as double values, you are losing any attempt to create the smoother, more accurate, movement wanted; (2) I think you need to re-work how the acceleration values are to change in your last post. EDIT: I guess I missed the last few minutes of posting trying to figure out how to explain what I thought.
GreenGoo GreenGoo

2014/8/18

#
Okay thanks for the help, I've found it. Yeah that code was very very rough, I just wanted to get some idea of what I should do and see how it looked. Thanks again!
You need to login to post a reply.