movement in 2D

Hi,

i would like to develop a program to make a playbook for american football. I haven't run across a free application that does this so i want to try it myself.

The view would be 2D and all players would have an x, y coordinate, velocity, acceleration and agility for starters.

You would plot out a course for a player by clicking on the field and thus creating a list of x,y points the player would have to run too.

But i'm not sure how i can calculate the next position given the player his current x,y coordinate and speed. Or if i count in the agility, how fast a player can turn. Are their any tutorials (java or c++) that show how to do this?

Thanks

[696 byte] By [wsbenedictv] at [2007-9-27 21:08:33]
# 1

Try having a direction in degrees or radians. Then, all you need to do to generate the next point is to do cos(direction)*speed for the x coordinate and sin(direction)*speed for the y coordinate. If you need to find the direction from one point to another then if p1x and p1y are the coordinates for the first point and p2x and p2y are the coordinates for the second point then you can use atan2(p2x-p1x,p2y-p1y).

JTeen at 2007-7-7 2:51:48 > top of Java-index,Other Topics,Java Game Development...
# 2
Thanks. That seems to do the trick.Any pages you know where there's more of this handy stuff :-)I'm rusty on the math. I used to have a lot of math but since i barely used it, i become very rusty at it :-)
wsbenedictv at 2007-7-7 2:51:48 > top of Java-index,Other Topics,Java Game Development...
# 3

Hello,

I have read your msgs. and i have a question to ask.

I am try to do a very basic thing it seems to me.

just an oval shape that moves to where the (x,y) point where i click.

I am using a JApplet Graphics object

the problem is...

the drawOval() method doesn't take as parameters but int data type.

round()*** the values generated from they way you mentioned earlier will only result in loss of precision and will end up with the ball moving to a totally different area.

do you have any ideas?

aymannour at 2007-7-7 2:51:48 > top of Java-index,Other Topics,Java Game Development...
# 4

I ususally use something like this..

public class Vector2D {

double xmag,ymag,totalMag;

double angle; // in radians

public MotionVector(double xmag, double ymag){

this.xmag = xmag;

this.ymag = ymag;

// Arc Tangent of y over x yields angle

angle = Math.atan((double)(y/x));

// Pythagrean (spelling?) theorem

totalMag = Math.sqrt((double)((xmag*xmag)+(ymag*ymag)));

}

/*

* getters & setters

*/

}

And when I need to translate my object to draw I just get the x and y magnitudes in units per second, multiply them by the elapsed time since the last redraw ((Units / Seconds)*Elapsed Seconds = Units). If, for instance the player needs to change direction, just set the variables for its cooresponding Vector2D object and wait for the next redraw. If you want to add acceleration, just tack on another vector of which you multiply by the time squared to get its unit-change.

// Keep in mind that this is untested, but to my knowledge it should work given you create the addVectors(Vector2D []) method.

import java.awt.geom.*;

public class MotionObject{

Vector2D [] velocities;

Vector2D [] accelerations;

public MotionObject(Vector2D [] velocities, Vector2D [] accelerations){

this.velocities = velocities;

this.accelerations = accelerations;

}

public Point2D.Double getResultantMagnitudes(double time){

Vector2D vsum = addVectors(velocities);// you'll need to create this method, to add vectors together

Vector2D asum = addVectors(accelerations);

double deltax = (vsum.xmag * time) + (asum.xmag * (time*time));

double deltay = (vsum.ymag * time) + (asum.ymag * (time*time));

return new Point2D.Double(deltax,deltay);

}

}

To make use of an object like that, you would create your Vector2D arrays, then pop them into a new MotionVector object. And when it came time to find out where your player/object should be on screen, just call getResultantMagnitudes(double time) with the elapsed time and add the x and y values of the returned Point2D.Double instance to the current x and y locations of your onscreen object.

JasonFeinstein at 2007-7-7 2:51:48 > top of Java-index,Other Topics,Java Game Development...
# 5

Hi, i was going to suggest spline curves, but a person does not move at a constant speed though the points.

After a way point the velocity would equal the last velocity - (change in direction) + agility.

or int v1 = v0 - (d0 - d1) + agility

depending on how you represent direction, and agility.

Have a look at http://www.gamedev.net/reference/

Harley.

harleyrana at 2007-7-7 2:51:48 > top of Java-index,Other Topics,Java Game Development...
# 6
Again, very useful information. Thanks guys!
wsbenedictv at 2007-7-7 2:51:48 > top of Java-index,Other Topics,Java Game Development...