Get a points coordinate

Started by
14 comments, last by P0jahn 11 years, 3 months ago

Yes, 30.0 is the width and 20.0 is the height (as in the examples in the first post). I think of the ray as being a particle starting at the red point, moving at constant velocity so that at time 1 it is at the green point. I use vertical_intersection_time and horizontal_intersection_time to see when that particle would hit the edges of the map. Whichever one happens first is where the black dot will be.

std::min does what you expect: Return the minimum of two values. I don't know how you say that in Java.

Advertisement

Hmmmm. I did not get it to work for some reason.

Watch the result:

[attachment=12972:error.png]

The position is wrong as well.

The code:


public static Point findEdgePoint(GameObject observer, GameObject target) //TODO:
{
 float vertical_intersection_time = 1.0e20f;


 if (target.posX > observer.posX) vertical_intersection_time = (observer.stage.width-observer.posX)/(target.posX-observer.posX);
 else if (target.posX < observer.posX) vertical_intersection_time = (0.0f-observer.posX)/(target.posX-observer.posX);


 float horizontal_intersection_time = 1.0e20f;


 if (target.posY > observer.posY) horizontal_intersection_time = (observer.stage.height-observer.posY)/(target.posY-observer.posY);
 else if (target.posY < observer.posY) horizontal_intersection_time = (0.0f-observer.posY)/(target.posY-observer.posY);


 float time = Math.min(horizontal_intersection_time, vertical_intersection_time);


 return new Point((int)(observer.posY + time * (target.posY-observer.posY)), (int)(observer.posY + time * (target.posY-observer.posY)));
}

Where did it go wrong?

Whoa, I just realized we've got some div-by-zero time-bombs hangin' out in here. Better factor those out.

Meanwhile, look closely at the args you've passed to 'new Point()'.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Ops, my bad. Fixed it. Works perfect now, almost...

There seems to be some inconsistency when the black circle is at the bottom. The circle is not at the bottom-most position, instead, it is about 40 pixels above it.

The problem is probably because the provided method use float as coordinates while my game only use ints as coordinates.

Is there a way around this?

Ops, my bad. Fixed it. Works perfect now, almost...
There seems to be some inconsistency when the black circle is at the bottom. The circle is not at the bottom-most position, instead, it is about 40 pixels above it.
The problem is probably because the provided method use float as coordinates while my game only use ints as coordinates.
Is there a way around this?

That doesn't sound like a int-vs-float problem at all. Pick a particular example where the code does the wrong thing and follow it step by step, using a debugger. You should be able to see at what point the computation departs from what you expect.

I fixed it somehow. I think that it... Actually I have no idea how I fixed it. But the problem is solved, big thanks to all the help.

I am going to test this ingame, as see how it works. I will report back if there is anything that do not work well.

This topic is closed to new replies.

Advertisement