Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  A co-ordinate problem.  (Read 3209 times)
0 Members and 1 Guest are viewing this topic.
Offline westloar

Junior Member





« Posted 2012-05-16 09:49:46 »

OK so this may be a long a complicated problem, I have made a Boomerang class (yes it's that guy) that determines the boomerangs start point by getting the X,Y position of the player and then modifying these co-ords so the boomerang will appear from the centre of the player.

First problem: It doesn't, it appears significantly higher and to the left of the player.

The class then creates the path of the boomerang and stores it in a point array and every time the sprite is updated moves it to the next point in the array.

Second problem: Although it does move in a straight, diagonal line, it moves vertically down from the start point, even though the path is calculated by adding X,Y positions to the last "node" of the path.

I will post the class and see if you can make head or tails of where the problem is coming from, part of me thinks that it may be using points to store the X,Y co-ords, as these use Java's co-ordinate system and points aren't used throughout the rest of the code. I wanted someone else's opinion before I rewrite the class or the rest of the engine to use or not to use points.

Here's the class:

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  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
public class Boomerang extends Sprite
{
    private static double DURATION = 0.5;  // secs
   // total time to cycle through all the images
   private int period;    // in ms; the game's animation period
   private BricksManager brickMan;
   
    private int speed; //used to calculate how many nodes to create along the
                      //boomerangs path.
   protected int locx, locy;
    private Boolean isVisible;
    private JumperSprite rat;
    private Point startPoint = new Point();
    private Point nextPoint = new Point();
    private int nodeLength;
    private int nodes = 0;
    private Point[] boomPath;
   
public Boomerang (int x, int y, BricksManager bm, ImagesLoader imsLd, int p, String name, JumperSprite js)
{
super(x, y, 30, 32, imsLd, "Boomerang");
 
locx = x;
locy = y;
super.setImage(name);
speed = 3;
period = p;
isVisible = false;
rat = js;
boomPath = new Point[20];
    }

public void fire(){
    int x;
    int y;
    int ratx = rat.getXPosn();
    int raty = rat.getYPosn();
    System.out.println("ratx:" + ratx);
    System.out.println("raty:" + raty);
   
    Point endp = new Point();
    int midx = rat.getWidth()/2; //get the mid point of rat' sprite
   //System.out.println("midx:" + midx);
   int midy = rat.getHeight()/2;
    //System.out.println("midy:" + midy);
   startPoint.setLocation((ratx + midx), (raty - midy));
    //set the start of boomPath to the mid
   //positions.
   //System.out.print("startPoint original" +startPoint);
   boomPath[0] = startPoint;
   
    PointerInfo MousePosn = MouseInfo.getPointerInfo();
   
    endp = MousePosn.getLocation();
   
    nodeLength = (int) (startPoint.distance(endp)/(20-speed));
    //System.out.println();
   //System.out.println("nodeLength:" + nodeLength);
   //cut the distance into equal segments to increment boomPath
   
    for(int i = 0; i<(20- speed); i++){   //add points to boomPoint using nodeLength increments
       //System.out.println();
       //System.out.println("boomPath[0].x:" + boomPath[0].x);
       //System.out.println();
       //System.out.println(i + 1);
       
        nextPoint.setLocation((boomPath[i].x - nodeLength), (boomPath[i].y - nodeLength));
       
        boomPath[i+1] = nextPoint;
       
       
        //System.out.println();
       //System.out.println("boompath[" + i + "]:" + boomPath[i]);    
       
    } //end of for loop
   
    isVisible = true;
   
           
  }//end of fire()

public boolean isVisible(){
    return this.isVisible;
}

public void update() {
   if(isVisible){
       if(nodes < boomPath.length){
       locx = boomPath[nodes].x;
       
       //System.out.println("boomPath[" + nodes + "]" + "locx:" + locx);
      locy = boomPath[nodes].y;
       //System.out.println("boomPath[" + nodes + "]" + "locx:" + locy);
      nodes ++;
        }
       }
    }//end of update()
 


}//end of Boomerang


Cheers for any help you can give me Smiley
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #1 - Posted 2012-05-16 09:59:27 »

Have you tried drawing all of your points to see where they actually are?

There should be nothing wrong with using Point, but I'm not sure from your description what your actual bug is. An image might help?

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline 65K
« Reply #2 - Posted 2012-05-16 10:01:32 »

Every path node gets the same point object.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline westloar

Junior Member





« Reply #3 - Posted 2012-05-16 14:40:07 »

how so?
Offline 65K
« Reply #4 - Posted 2012-05-16 14:51:03 »

Because of this:
boomPath[i+1] = nextPoint;

Every node is nextPoint.

Offline westloar

Junior Member





« Reply #5 - Posted 2012-05-17 23:08:04 »

Yes but before

1  
boomPath[i+1] = nextPoint;


nextPoint is calculated for every iteration:

1  
nextPoint.setLocation((boomPath[i].x - nodeLength), (boomPath[i].y - nodeLength));
Offline westloar

Junior Member





« Reply #6 - Posted 2012-05-17 23:15:48 »

Ok so Orangy Tang here's a rough approximation of what currently happens when you throw the boomerang:



The green line is the path that the boomerang takes, the red cross is the mouse pointer position at time of throwing the boomerang.

I have tried subtracting the nodeLength from the X and Y co-ord (line 67) as I having read up on Java's co-ord system, it seems to use a system by which 0,0 is the top left of the screen. Confusingly whether I add or subtract nodeLength the boomerang still takes a downwards diagonal path.
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #7 - Posted 2012-05-17 23:20:28 »

65K is right, you've got an array where all the indexes refer to the same Point object. You want something like:

1  
boomPath[i+1] = new Point((boomPath[i].x - nodeLength), (boomPath[i].y - nodeLength));


Of course then it probably still won't work as your usage of nodeLength is pretty weird. What do you think you're doing there?

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline westloar

Junior Member





« Reply #8 - Posted 2012-05-17 23:26:30 »

nodeLength is essentially the distance between each node, may be confusingly named, but I know what I mean and ultimately that's all that matters as this is a one man project Tongue

what I think I'm doing is:

The next node is at the X position of the current node - the nodeLength and at the Y postion...etc.
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #9 - Posted 2012-05-17 23:34:47 »

But the nodeLength is only the length between nodes *along the line*. The actual x distance between two nodes is different from the actual y distance between two nodes, but you're trying to use one value to step both.

By now hopefully you'll see that you need separate xNodeLength and yNodeLength.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline westloar

Junior Member





« Reply #10 - Posted 2012-05-17 23:49:52 »

If I want to calculate a path with a 1:1 ratio i.e:



(I realise that triangle does not have a 1:1 ratio)

surely the X and Y would need to be the same.

Also, why does my boomerang travel on a path diagonally downwards whether I add or subtract nodeLength, surely, even if my game logic, so to speak, is flawed, changing whether the value is added or subtracted should change the vertical direction of the path?
Offline westloar

Junior Member





« Reply #11 - Posted 2012-05-18 01:12:00 »

you know when you're really tired and you say things that are silly because you are really tired and say silly things that are silly.

Yes, I have done that, of course I need a different Y value worked out from the angle of the mouse pointer relative to the X perpendicular (also still tired so that may sound like crap, but again I know what I mean Tongue)

I'm glad you've brought that to my attention, but the main problem still remains, what the hell is going on!?
Offline 65K
« Reply #12 - Posted 2012-05-18 06:11:17 »

I'm glad you've brought that to my attention, but the main problem still remains, what the hell is going on!?
So, do create new point objects now like Orangy Tang suggested ?

Offline westloar

Junior Member





« Reply #13 - Posted 2012-05-18 10:26:03 »

Ok, so I've added the code you suggested, I'll code in the calculation of the Y path when I've fixed the problem that my boomerang travels diagonally downwards, any suggestions especially as changing the operator like so:

From:

1  
boomPath[i+1] = new Point((boomPath[i].x - nodeLength), (boomPath[i].y - nodeLength));


To:

1  
boomPath[i+1] = new Point((boomPath[i].x + nodeLength), (boomPath[i].y + nodeLength));


makes no difference whatsoever!?
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #14 - Posted 2012-05-18 17:38:47 »

To explain why you can't recycle the same Point object:

All objects in Java are actually pointers to objects, everywhere. That means that setting nextPoint for 50 different objects, even if it has different values on each setting, will always end up changing it for all 50 objects. If you know how pointers work, all Objects in Java are pointers to a spot in memory where the data is. So all 50 objects have different pointers, but all the pointers are pointing to the same data.

Creating new Points every time means you have distinct data for each (as desired), so you're all good.

Primitive values (int, float, boolean, etc.), however, are pass by value. That means every time you change the value of one or put one as a parameter it is copied and is its own distinct value. So if instead of using a Point you were using two floats (x and y), then you would actually have no issues.

Make sense?

See my work:
OTC Software
Offline westloar

Junior Member





« Reply #15 - Posted 2012-05-18 17:54:13 »

Yer, I totally get that, I am a noob at Java, but I have read a couple of books and while that was a while ago I have retained some of the knowledge, I just can't get my head around the behaviour of the boomerang and how using floats as opposed to Points will change that?
Offline 65K
« Reply #16 - Posted 2012-05-18 18:09:08 »

I think I don't understand what you are actually trying to do...
Shall the boomerang, well, fly like a boomerang, or in a diagonal line ?

Offline westloar

Junior Member





« Reply #17 - Posted 2012-05-18 18:14:26 »

well for now just a diagonal line, at the moment a lot of the code is place holder while I get everything together, the issue is the code isn't working properly.

The boomerang is supposed to calculate its path from the centre of the player (I think I know why it doesn't do that) towards the position of the mouse pointer when boomerang.fire() is called.

If you check the diagram (if you can call it that Tongue) above it shows that when mouse pointer is up and to the right of the player not only does the boomerang start above and to the left of the player (wrong) it then travels diagonally downwards(wrong), whereas it is supposed to travel diagonally upwards to the right i.e. towards the position of the mouse pointer.
Online matheus23

JGO Kernel


Medals: 107
Projects: 3


You think about my Avatar right now!


« Reply #18 - Posted 2012-05-18 18:17:30 »

Are you drawing things in OpenGL with LWJGL?

Maybe your xy(0|0) Coord is at the top-left, and so LWJGL's Mouse.getY() is flipped.

Then you would need to get the Mouse position with Display.getHeight()-Mouse.getY().

Hope, that helps Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline 65K
« Reply #19 - Posted 2012-05-18 19:24:00 »

So, from a starting and a target point you want to calculate a set of intermediate curve points.
As input and output are clearly defined, I recommend to first develop a separate method just for this. No sprites, no mouse, no image required, thus no further interactions and no further possible error causes.

List<Point> calculateFlightCurve(Point start, Point target)


Offline kappa
« League of Dukes »

JGO Kernel


Medals: 77
Projects: 15


★★★★★


« Reply #20 - Posted 2012-05-18 19:38:29 »

Are you drawing things in OpenGL with LWJGL?

Maybe your xy(0|0) Coord is at the top-left, and so LWJGL's Mouse.getY() is flipped.

Then you would need to get the Mouse position with Display.getHeight()-Mouse.getY().

Hope, that helps Smiley

Your code is off by one pixel, it should be
1  
(Display.getHeight() - 1) - Mouse.getY();
Offline sproingie

JGO Kernel


Medals: 202



« Reply #21 - Posted 2012-05-18 19:43:57 »

All objects in Java are passed by reference, everywhere.

Java is pass by value, period, end of story.  Pass by Reference means you can write this, which you cannot do in Java:
1  
 public void swap(Object a, Object b)


All non-primitives are accessed by references, but those references are passed by value.  "Pass by reference" and "passing references" are two different things.  I'm not just nitpicking here; when you explain this stuff to people coming from C++ or C# or PHP which actually does have pass-by-reference, it gets confusing when they get told Java passes by reference when it does not.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #22 - Posted 2012-05-18 19:46:51 »

All objects in Java are passed by reference, everywhere.

Java is pass by value, period, end of story.  Pass by Reference means you can write this, which you cannot do in Java:
1  
 public void swap(Object a, Object b)


All non-primitives are accessed by references, but those references are passed by value.  "Pass by reference" and "passing references" are two different things.  I'm not just nitpicking here; when you explain this stuff to people coming from C++ or C# or PHP which actually does have pass-by-reference, it gets confusing when they get told Java passes by reference when it does not.

Yes, I will edit my post.

See my work:
OTC Software
Online matheus23

JGO Kernel


Medals: 107
Projects: 3


You think about my Avatar right now!


« Reply #23 - Posted 2012-05-19 08:01:58 »

Are you drawing things in OpenGL with LWJGL?

Maybe your xy(0|0) Coord is at the top-left, and so LWJGL's Mouse.getY() is flipped.

Then you would need to get the Mouse position with Display.getHeight()-Mouse.getY().

Hope, that helps Smiley

Your code is off by one pixel, it should be
1  
(Display.getHeight() - 1) - Mouse.getY();


Ugh... Have to change that in my Project. Thanks kappa Cheesy

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline H3rnst

Senior Newbie


Medals: 1



« Reply #24 - Posted 2012-05-19 13:23:11 »

well for now just a diagonal line, at the moment a lot of the code is place holder while I get everything together, the issue is the code isn't working properly.

The boomerang is supposed to calculate its path from the centre of the player (I think I know why it doesn't do that) towards the position of the mouse pointer when boomerang.fire() is called.

Remember the formula of the distance traveled by a body (I really don't know how you guys call this, I'm not a native english speaker. I'm talking about accelerated movement in a vertical shot):
1  
distance = initialSpeed*elapsedTime + (1/2)*gravityAcceleration*(elapsedTime^2)


From the way I see it, this movement resembles a lot to the one of a boomerang (Only if you throw it with a 90º angle. If you click above the head of the player)
If you already have a way to calculate the angle between the player and the position where the user clicked...


...then I'm pretty sure there is a way to "shift" the trajectory of the boomerang by multiplying it for the sine or cosine of the angle. I'm afraid I can't help you with that because I suck at math, even worse than how I suck at speaking english. But I think someone smarter should have no problem.

I hope I didn't misunderstood the problem and this could actually help.

Offline westloar

Junior Member





« Reply #25 - Posted 2012-05-20 21:58:33 »

thanks, that's really helpful for when I figure out what the hell is going on with the boomerang inexplicably starting from the wrong position and travelling in the wrong direction.

I'm going to try using floats for my co-ordinates and see if that makes any difference.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 800
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #26 - Posted 2012-05-22 18:20:43 »

All non-primitives are accessed by references, but those references are passed by value.  "Pass by reference" and "passing references" are two different things.  I'm not just nitpicking here; when you explain this stuff to people coming from C++ or C# or PHP which actually does have pass-by-reference, it gets confusing when they get told Java passes by reference when it does not.
IMHO there is no need to distinguish primitives and non-primitives. In both cases a 'copy' is pushed on the stack of the call site, which is popped  by the target - whether that is a primitive or a reference doesn't matter.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline sproingie

JGO Kernel


Medals: 202



« Reply #27 - Posted 2012-05-22 19:14:17 »

Sure, but it's a copy of the reference, not the contents of the object.  Most people who've learned Java first have just internalized this stuff, and others who have learned a bunch of languages before are aware of all the different conventions, so it's really just people who only knew maybe one or two other languages beforehand who get confused.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Grunnt (16 views)
2014-09-23 14:38:19

radar3301 (14 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (29 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (75 views)
2014-09-10 13:57:51

TehJavaDev (105 views)
2014-09-10 06:39:09
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!