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

2014/3/28

Need help to make an object move

kasperk22 kasperk22

2014/3/28

#
Hey guys.. I need to make an object move for my game. It's hard to describe in words what i would like it to do, but basically what i want is a character that is fixiated to the shape of a parabel. Also i have made a picture in order to describe it better. here's the link: http://imgur.com/LydQZwW i hope this is enough information for you guys to understand what i want to make, but i can of course give more information if needed :)
JasonZhu JasonZhu

2014/3/28

#
Try basic trig. Pythagorean theorem should work. x^2 + y^2 = h^2
danpost danpost

2014/3/28

#
Does it have to be parabolic? or can it be a basic arc of a circle? Coding what you want would be much easier, then.
kasperk22 kasperk22

2014/3/28

#
an arc of a circle would be fine too actually.. :)
danpost danpost

2014/3/28

#
Then limit the range of the rotation between -45 and 45 (or whatever) and every act (at any time the angle (or rotation) changes, do the following: (1) turn the actor 90 degrees clockwise (2) set the location to the center-point of the arc (center of the circle that the arc is part of) (3) alter the rotation (limited) by change amount (4) move the actor radius distance (from center) in direction facing (5) turn the actor 90 degrees counter-clockwise
kasperk22 kasperk22

2014/3/29

#
hmm, i don't really get it.. first i turn the actor 90 degrees clockwise and then counter-clockwise. What do i get from that? :)
danpost danpost

2014/3/29

#
This is all done between frames (during one act cycle). If you perform all these actions as listed, your actor will remain on the arc with the proper rotation. The turning of 90 degrees gives it the direction to move along the radius of the circle the arc is a part of. Turning -90 degrees corrects the rotation after that move. Look at this discussion of a very similar issue.
kasperk22 kasperk22

2014/3/29

#
Thank you alot! now i have this working code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
   if(Greenfoot.isKeyDown("up"))    
     {
        setLocation(getWorld().getWidth()/2, getWorld().getHeight()/2); // set center 
    setRotation(angle+1); // set new angle 
    angle = getRotation(); // save new angle 
    move(50); // move from center along radius to circle 
    setRotation(getRotation()); // fix rotation
}
  if(Greenfoot.isKeyDown("down"))    
     {
        setLocation(getWorld().getWidth()/2, getWorld().getHeight()/2); // set center 
    setRotation(angle-1); // set new angle 
    angle = getRotation(); // save new angle 
    move(50); // move from center along radius to circle 
    setRotation(getRotation()); // fix rotation
}
it's not how i imagined it, but it inspired me to do this instead since i like this gamemode better... However i am a little confused about how it works. It's the move function i don't understand fully. I read the description on greenfoot, but i don't understand it. Can you explain how it works to me?
danpost danpost

2014/3/29

#
You could simplify a little using this:
1
2
3
4
5
6
int change = 0;
if (Greenfoot.isKeyDown("up")) change++;
if (Greenfoot.isKeyDown("down")) change--;
turn(change);
setLocation(getWorld().getWidth()/2, getWorld().getHeight()/2);
move(50);
If you are not changing the rotation after the move (which 'setRotation(getRotation())' basically is doing -- or not doing), then the angle does not need to be saved in a field. We can just adjust the angle from what its rotation currently is, as above. The 'move' method moves the actor in the direction the actor is facing (along the path of the angle that 'getRotation' returns) the given distance.
kasperk22 kasperk22

2014/3/29

#
When i put that code in instead, i got an insanely fast moving object. I put in setRotation instead of turn and it worked fine.. However is there any advantages of using the turn method instead?
danpost danpost

2014/3/29

#
You probably did not realize that 'turn(change)' is not 'turn(angle+change)'.
kasperk22 kasperk22

2014/3/29

#
oh, alright.. well thanks, it's working now
danpost danpost

2014/3/29

#
kasperk22 wrote...
is there any advantages of using the turn method instead?
Not much. It just makes it a little easier to code a change in rotation from the current rotation. 'turn' is basically something like this:
1
2
3
4
public void turn(int amount)
{
    setRotation(getRotation()+amount);
}
You do not have to tell 'turn' to add the current rotation to the amount to get the new angle, it automatically does it for you.
You need to login to post a reply.