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

2014/7/16

Follow Cursor

buildoman buildoman

2014/7/16

#
One of my friends made a bullet that whenever you shot it all of the bullets would orbit around the cursor. He said he tried to make the bullets shoot where the cursor was but the bullets would just orbit around the cursor. He no longer has the code that let the bullets orbit and I want to know if anyone can code it for me as im a noob at this. Thanks!
danpost danpost

2014/7/16

#
That type of movement would be like that which was implemented in my Orbital Effects scenario. Run the scenario to view the code.
buildoman buildoman

2014/7/16

#
I cant run the scenario for some reason, Can you paste the code here or in a pastebin?
danpost danpost

2014/7/16

#
Basically, you would write the code to follow the cursor; but, between 'turnTowards' and 'move', you add an extra 'turn' (up to plus or minus 89 degrees).
buildoman buildoman

2014/7/16

#
Whats the code for all of that? Like I said I am not good at greenfoot at all.
lordhershey lordhershey

2014/7/16

#
1
2
3
4
5
6
7
8
9
10
11
12
13
MouseInfo mouse = Greenfoot.getMouseInfo();
        if(null != mouse)
        {
            turnTowards(mouse.getX(),mouse.getY());
            if(mouse.getClickCount() > 0 ||
                mouse.getButton() > 0 ||
                Greenfoot.isKeyDown("space"))
            {
                Bullet b = new Bullet();
                b.setRotation(getRotation());
                getWorld().addObject(b,getX(),getY());
            }
        }
Something like this, makes the actor point at the mouse, create a bullet, add it to the world, then point the bullet in the same direction that this actor is pointing; which happens to be the cursor.
danpost danpost

2014/7/16

#
@lordhershey, it is not the code for firing and setting initial rotation that was asked for. @buildoman, for the act method of the bullet, use something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void act()
{
    if (Greenfoot.mouseMoved(null) || Greenfoot.mouseDragged(null))
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if (mouse != null)
        {
            turnTowards(mouse.getX(), mouse.getY());
            turn(270);
        }
    }
    turn(2); // vary the value as desired
    move(5); // vary the value as desired
}
lordhershey lordhershey

2014/7/16

#
Wait a second, I think I remember this conversation, there was a furious debate of how to get something to orbit the mouse pointer. Here was what I had made Obits the Mouse though the solution may be sub optimal. Just look at the code for the Orbiter.
danpost danpost

2014/7/16

#
lordhershey wrote...
Here was what I had made Obits the Mouse though the solution may be sub optimal. Just look at the code for the Orbiter.
It would be easier to look at the code if it was made available (by publishing the source code).
lordhershey lordhershey

2014/7/17

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.lang.*;
 
/**
 * Write a description of class Orbiter here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Orbiter extends Actor
{
    /**
     * Act - do whatever the Orbiter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        int cx = getWorld().getWidth()/2;
        int cy = getWorld().getHeight()/2;
         
        MouseInfo mi = Greenfoot.getMouseInfo();
        if(null != mi)
        {
            cx = mi.getX();
            cy = mi.getY();
        }
         
        
        int speed= 10;
        move (speed);
         
         
        turnTowards(cx,cy);
         
        int xSq = (cx - getX())*(cx - getX());
        int ySq = (cy - getY())*(cy - getY());
         
        int dist =(int)Math.sqrt(xSq+ySq);
         
        int rotation = getRotation();
        setRotation(rotation + 45);
        if(dist < 150)
        {
             
            if(dist < 100)
            {
                setRotation(rotation + 91);
            }
            else
            {
                setRotation(rotation + 88);
            }
        }
         
    }   
}
danpost danpost

2014/7/17

#
lordhershey wrote...
Wait a second, I think I remember this conversation, there was a furious debate of how to get something to orbit the mouse pointer.
Out of curiosity -- where was this 'furious debate'? I cannot seem to find it on this site. Could it have been somewhere else?
lordhershey lordhershey

2014/7/17

#
Not really a furious debate, we just had 2 different approaches to this same problem, the first code snippit I put out there you had pointed out that the actor would drift away to the edge of the screen, which it did. so I put that distance check in there to adjust the angle more in or out depending on the distance from the target. I had not posted the code, not sure why, I can easily be distracted.
You need to login to post a reply.