• Post Reply Bookmark Topic Watch Topic
  • New Topic

variable type Point subtraction not working  RSS feed

 
Cuda Ped
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public Point posi(){
Point currp = new Point(this.x(), this.y());
return currp;
}

public Point nposi(){ //next possition
Point nextXp = new Point(SPACE, 0);
return nextXp;
}

public Point objposi(Objects object) {
Point obp = new Point(object.x(), object.y());
return obp;
}

public Point subtract( Point p1, Point p2) {
Point p3;
p3 = p1 - p2; ///// this is not working could someone explain why? and how I could fix this
return p3;

}


public int isHorizonatalCollision(Objects object) {
int tru = 0;
int leftColli = 1;
Point eqp;
eqp = subtract(posi(), nposi());

if (eqp == objposi(object) )
{
return leftColli;
}
return tru;
}
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How, in your mind, is Point minus Point defined? Perhaps then we could help you with an implementation of your definition.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't work because Java has no clue how to subtract one object from another - especially ones that you have created.

Your subtract method will need to explicitly write how what needs to be done. And before you can code that, you have to be able to explain in English (or some other natural language) what EXACTLY it means to subtract them.

Pretend you are trying to teach a 10 yr old child, who has never seen a "Point" object. What should they do?
 
Cuda Ped
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what I'm trying to achieve with the subtract method

public Point subtract( Point p1, Point p2) {
Point p3;
p3 = p1 - p2;
return p3;

}

is say P1 = (32, 0) and P2 = (32, 0) I want the value of P3 to then be (0, 0).

My thinking is that if I can do that, I can then use that to create an algorithm for object collisions.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you want to create a new Point object referred to by the variable p3. And it's going to be the result of calling that subtract() method. Then before you return it, you should set its internal state to be whatever your calculations say it should be. To do that you might have to call a suitable Point constructor to create it, or alternatively you might want to call some setter methods after you create it. Have a look at the Point class and see which would be more suitable.
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cuda Ped wrote: ... My thinking is that if I can do that, I can then use that to create an algorithm for object collisions.


You have an algorithm for object collision if you have point objects, but that is rarely the case. If on the other hand, you look at Java's Shape Interface, and the Classes that implement it, you will see that they have what you are looking to do.

In the case you have an object and you want to see if it will collide with another object, and you want to program it yourself, you can do vector distances--are the centers of your objects within a certain distance from each other. Most of the time in 2D and 3D games it becomes a problem of does the square or cube containing the object intersect the other object's square or cube--I believe that is Java's implementation. I prefer the vector approach:

You get P2 - P1 = (dx, dy) as you are looking at doing:

P2.x - P1.x = dx and
P2.y - P1.y = dy there you have positional difference. If you take the square root of the sum of the squares-- sqrt(dx^2 + dy^2) you get the distance between P1 and P2--or in this case the center of the objects. The critical distance is the sum of half of the diameters of your objects, so if your objects are 6 units and 4 units in diameter, then you have half of each distance to work with, which is 5. So if your distance if greater than 5, there is not any collision.

BTW: it works in 3D also, but you have to add a Z axis position--sqrt(dx^2 + dy^2 + dz^2)

Here is a practical note: if you are implementing the vector distance approach, then you can also assign damage factors according to how bad the collision is--in this example a 5 is barely grazing each other and 0 would be dead on.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!