Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (537)
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  
  Connecting two points.  (Read 1060 times)
0 Members and 1 Guest are viewing this topic.
Offline lethargic

Junior Newbie





« Posted 2005-12-14 12:12:49 »

Hi.
I don't have much experience with graphics, but my question is pretty straight foward.
This question might go a bit into geometry.

If I have two points on a canvas, and I want to say.. make a sprite travel from one to the other in a straight line, how would I do that?

For example, if I have (23, 43) and (130, 20), how would I best track every coordinate between them?
Like, if I could just get the "direction" to go in I could easily track it myself, is the delta like direction?
How would I get that??
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #1 - Posted 2005-12-14 12:45:33 »

Linear interpolation would be a simple solution. However, getting the speed right would be a tad painful with this approach.

(23, 43) and (130, 20)... that would be (107, -23) as vector, which could be then normalized and after that you can just move the character in that direction x units per frame (a simple multiplication).

LWJGL for example comes with some vector classes. Just get the source and take a look at how that stuff is done.

弾幕 ☆ @mahonnaiseblog
Offline cylab

JGO Ninja


Medals: 38



« Reply #2 - Posted 2005-12-14 13:41:21 »

The delta would be the "raw" form of the direction and corresponds exactly to the vector (107, -23) oNyx pointed out. To normalize a vector means to divide this vector, so that the line you draw along this direction is exactly of the length 1, meaning dividing the x/y delta values by the overall lenght of your "raw" direction. You can calculate this using the formula of pythagoras: double len=Math.sqrt(Math.pow(x,2)+Math.pow(y,2)).

Normalizing the direction vector has the advantage of representing the x and y deltas you need to travel 1 unit in the desired direction.

A decent vector library can take away the details of such operations and offers the benefit of easy transition from 2D graphics to 3D graphics, so I would suggest to get some reading on linear algebra and vector math in detail. I can recommend the book "Essential Mathematics for Games and Interactive Applications"
http://www.amazon.com/gp/product/155860863X/qid=1134564195/sr=8-2/ref=sr_8_xs_ap_i2_xgl14/002-9312530-3104828?n=507846&s=books&v=glance

but it might be a bit much to swollow (700 pages, but it includes nearly all game related mathematics needed)

Mathias - I Know What [you] Did Last Summer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline lethargic

Junior Newbie





« Reply #3 - Posted 2005-12-15 03:06:27 »

Thank you very much, this is a great place to start.
Offline Kommi

Junior Member




All opinions will be lined up and shot!


« Reply #4 - Posted 2005-12-15 21:17:41 »

I have code that does what you want, will post it up when I get home

Kommi
Offline Maverick

Senior Newbie





« Reply #5 - Posted 2005-12-18 00:59:33 »

See if this helps you:
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  
public float toXPos;
    public float toYPos;
    public float vectX;
    public float vectY;
    public int initialYpos;
    public int initialXpos;
    private int dist;
    public float d = 0;
    public Rectangle targetRectangle;

// in constructor
       initialYpos = (int)yPos;
        initialXpos = (int)xPos;
        this.toXPos = (float)(fighterRectangle.getX()+Math.random()*(fighterRectangle.getWidth()));
        this.toYPos = (float)(fighterRectangle.getY()+Math.random()*(fighterRectangle.getHeight()));
        vectY =(toYPos-yPos);
        vectX = (toXPos-xPos);
        dist = (int)(Math.sqrt(vectX*vectX + vectY*vectY))/10; //by changin the 10 you will vary the speed of the object.

//in update
public void update(){
        d++;
        xPos = (int)(d/dist*vectX + initialXpos);
        yPos = (int)(d/dist*vectY+initialYpos);
        rectangle.x = xPos;
        rectangle.y = yPos;
    }

 
pd: did not try it. but i beleive it should work.
forgot to tell you, this will pick a random point of a rectangle to shoot to, if you want to always shoot straight just assign toXPos and toYPos an exact value.
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.

CogWheelz (16 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (18 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (40 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

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

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

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

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

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
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!