0

I am trying to develop a game as my major project using Java.

I have to move a coin like object on a board (it's a board game), I want to know how to move that object in any direction and how to implement 2-D collision on that objects (I have implemented collision detection.)

Until now objects move in straight lines with slope=1, pathFinder(int x1, int y1, int slope); (the method to move objects) will have current position(x1, y1) and slope as input, so how to move object in any direction and at any slope? Moreover, how to implement velocity vectors in java, probably using a class Vector and defining all operations in it?

Igor
  • 33,276
  • 14
  • 79
  • 112
H S
  • 179
  • 4
  • 12
  • Do you know any trigonometry (sin, cos, tan)? – Justin Nov 16 '11 at 13:51
  • 3
    Sorry, but in which way is this java related? Java by itself has no tools to help with game development or moving objects, so basically you will need to do the same code as if you did it in any other language. – bezmax Nov 16 '11 at 13:54
  • Maybe this will help: http://stackoverflow.com/questions/345838/ball-to-ball-collision-detection-and-handling – Bogdan Nov 16 '11 at 14:21
  • @Bogdan i just told it to give you clarity about what i am doing. Did i commit crime? and thankyou for helping me sir. – H S Nov 16 '11 at 14:42
  • @Justin yes sir, i know i am a mathematics student, i know physics too,concept of 2-D collision. i am facing problem in converting these concepts into code according to my need. – H S Nov 16 '11 at 14:44

1 Answers1

0

For collision between a round objects - they collide when distance between their centers is less than the sum of their radii. If you want movement at every angle you will need to keep object coordinates in floating point numbers and use trigonometry to calculate new positions using sin/cos/tan functions. Then you convert floating point numbers to integers before drawing on screen.

Vectors can be implemented in several ways. (x,y) float pair, normalized - size 1 - flat pair (x,y) and size float variable. Once you have a vector then changing position is trivial. Just take a sum of velocity vector and old position as new position. Normalized vector can be calculated from angle of movement like so: x = cos(ang) and y = sin(ang), with angle in radians. Normalized vector is then multiplied by size variable to get a full velocity vector which is then summed with position to get new position.

RokL
  • 2,663
  • 3
  • 22
  • 26