Rendering a image with Graphics2D based on 4 points

103 Replies
JavaBot
JavaBotโ€ข2y ago
โŒ› This post has been reserved for your question.
Hey @Hunter! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Kyo-chan
Kyo-chanโ€ข2y ago
And, erm, you have trouble finding a method from Graphics2D that might possibly do exactly that? Ah wait no, 4 points. Okay. I'm not sure either
JavaBot
JavaBotโ€ข2y ago
๐Ÿ’ค Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
๐Ÿ’ค Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Defective
Defectiveโ€ข2y ago
graphics2d.drawImage(image, x1, y1, x2-x1, y2-y1, null); ? this method takes width and height as the 3rd and 4th argument, so you need to calculate them yourself
Kyo-chan
Kyo-chanโ€ข2y ago
From 4 points there is no reason to believe the points make out a rectangle, yet that method assumes so
Defective
Defectiveโ€ข2y ago
oh right My bad
tjoener
tjoenerโ€ข2y ago
https://docs.oracle.com/javase/8/docs/api/java/awt/geom/AffineTransform.html This is what you need Have not used the API in forever though Plus starting from points makes it a lot harder
Kyo-chan
Kyo-chanโ€ข2y ago
I suspect that OP would have needed to give more information about their problem. One AffineTransform can't solve the issue for any arbitrary set of points
tjoener
tjoenerโ€ข2y ago
yeah no, but, and I'm going on a limb here, he's trying to draw textures to fake cubes as in some sort of minecraft clone ๐Ÿ™‚ But afaik, affinetransform and the corresponding graphics2d.drawimage is the only standard thing to get anywhere close to where they want to go. And if it is game related, there should be an affinetransform available already of some sort
dan1st
dan1stโ€ข2y ago
Do these 4 points form a rectangle or are they completely arbitrary?
dan1st
dan1stโ€ข2y ago
If it is (like the black image), you can draw the image starting on the bottom left corner like the red image. For the dx and dy lengths, they are equal to the lengths of the dides of the original rectangle (the blue lengths are equal and the pink lengths are equal). Then, you'd have to rotate it.
No description
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
I don't think the image is the problem The problem is more that we don't know whate xactly you want to do
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
the proportion being different/stretching wouldn't be much of a problem I think but if the points don't represent a rectangle, it's more difficult
dan1st
dan1stโ€ข2y ago
e.g. can the points be arranged like this?
No description
dan1st
dan1stโ€ข2y ago
or in other words: Is the 4th point created such that the whole thing assembles a rectangle if it'S a parallelogram, you can probably also apply some transformations
The Typhothanian (@always)
@hundo I just figured this out for my game over the weekend, so I got you covered. Pic is my work. On the top is a quad, and the inner quad is representing a pixel. This pixel, if the quad was a 20x20 square, would be from 10, 5 to 15, 13. Since the square is 20x20, we need to divide each dimension by 20. So, we have these vars: startX = 0.5 endX = 0.75 startY = 0.25 endY = 0.65 Then, we use a lerp function (a + t * (b - a)) to find the coords on each line. We should have eight. Then, we connect the lines from #0 to #5, #1 to #4, etc. and where the lines intersect is our projected pixel. Now, this isn't an especially efficient method for games, but if you start skipping pixels as the quad gets smaller, it is good enough. However, if you want/are okay with smoothing, then check out LWJGL. There is a method literally for this, but you need some buffer stuff and triangles. Hope this helps.
No description
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
The Typhothanian (@always)
oops @Hunter btw, that took me about a month to figure out (I was messing around with distancing before) so yw
Kyo-chan
Kyo-chanโ€ข2y ago
So the explanation and example don't use 4 points, and when we ask what it is you need you answer you need to adapt to 4 points, but now that you get this you like it.
dan1st
dan1stโ€ข2y ago
if it is that case, you could try https://stackoverflow.com/a/16796687/10871900
Stack Overflow
Draw an Image in arbitrary corners
So i have a normal image. I defined Click- and Drag-Listeners on each corner of the rectangular image. I want to freely transform each of the corners and paint it on the screen. The AffineTransform
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
Why?
dan1st
dan1stโ€ข2y ago
tbh I wouldn't be so sure about that
JavaBot
JavaBotโ€ข2y ago
๐Ÿ’ค Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
reduce it to a minimal reproducable example
dan1st
dan1stโ€ข2y ago
(so that it isn't problematic in any way)
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
No I won't run code from random people on the internet and I also don't recommend others to do that what exactly is happening?
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
Are you possibly blocking the UI thread?
JavaBot
JavaBotโ€ข2y ago
๐Ÿ’ค Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
Is your code actually executed delayed or does your code execute in time and it's just the rendering?
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
How much behind?
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
px?
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
not milliseconds? I thought behind meant behind in time
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
with behind, do you mean it's below than where it should be?
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
JavaBot
JavaBotโ€ข2y ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
dan1st
dan1stโ€ข2y ago
Can you check whether the tick method is actually executed 100 times per second? i.e. you can add something like
//before the loop
long lastTime = System.currentTimeMillis();
//in loop
long newTime = System.currentTimeMillis();
System.out.println(newTime-lastTime);
lastTime = newTime;
//before the loop
long lastTime = System.currentTimeMillis();
//in loop
long newTime = System.currentTimeMillis();
System.out.println(newTime-lastTime);
lastTime = newTime;
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
Is it just the image? What values does that typically print? How much delay is there with rendering?
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
looks like you are doing a lot of work compared to the number of ticks How does it change when sleeping for 50ms instead of 10? Does the problem of the drawing being delayed stay the same, does it get worse or does it get better? Also you might want to try using a swing timer instead of Thread.sleep and how much is the drawing delayed?
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
you mean the screen is black?
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
oh nvm it didn't show properly
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
ok you have a square and a line
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
what's the issue about that?
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
For testing, if you change the Thread.sleep from 10 to 1000 which means you should have a bit less than 1 frame per second Is it always 1 frame behind?
tjoener
tjoenerโ€ข2y ago
Also adjust the dt then ๐Ÿ™‚
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
well ig my goal is to make everything run slower
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
tjoener
tjoenerโ€ข2y ago
do you do move calculation in a separate thread or something? or rendering in a separate thread?
dan1st
dan1stโ€ข2y ago
What exactly do you mean with moving and then rendering?
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
yes
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
well calculating the overlap happens one tick after moving, right?
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
Like - Tick 1 - move into the object - you render - Tick 2 - You check for collision and move out - you render that's my guess
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
is it intended that you don't use the result of isColliding?
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
GitHub
SAT/src/main/java/org/example/OBB.java at 0acdac4ca664f191dad9a1413...
Contribute to zivthefire/SAT development by creating an account on GitHub.
JavaBot
JavaBotโ€ข2y ago
int additionX = (shortest.x == 0) ? 0 : (shortest.x > 0) ? 1 : -1;
int additionY = (shortest.y == 0) ? 0 : (shortest.y > 0) ? 1 : -1;
this.x += shortest.x;
this.y += shortest.y;
this.localvelocity = box2.velocity;
this.localvelocity =box2.velocity;
int additionX = (shortest.x == 0) ? 0 : (shortest.x > 0) ? 1 : -1;
int additionY = (shortest.y == 0) ? 0 : (shortest.y > 0) ? 1 : -1;
this.x += shortest.x;
this.y += shortest.y;
this.localvelocity = box2.velocity;
this.localvelocity =box2.velocity;
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
ah ok This this issue happen regardless of where you collide from?
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
ok
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
Also if you put a System.out.println("collision") or similar before the return true;, is that executed once per collision or multiple tikmes? btw you can remove the previous testing code if you haven't already
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
the things with the time changes and the System.out.println with the time difference
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
Did you check that?
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
Callinjg repaint() twice doesn't change anything, right? Note that from the docs of repaint:
If this component is a lightweight component, this method causes a call to this component's paint method as soon as possible. Otherwise, this method causes a call to this component's update method as soon as possible.
maybe it cannot repaint and "as soon as possible" happens one frame later for some reason? actually no that doesn't seem to be the case you could try debugging the isColliding method and check whether the x/y are actually correct Does the isColliding method actually return true in the same frame as the collision happens? What actually causes the collision? Is the collision caused by moving in tick()? If so, where does that movement happen? oh yeah I think I know why
JavaBot
JavaBotโ€ข2y ago
if(!Static) {

velocity.add(accleration);


totalvelocity = Vector2D.add(velocity,localvelocity);

x += totalvelocity.x;
y -= totalvelocity.y;

}
if(!Static) {

velocity.add(accleration);


totalvelocity = Vector2D.add(velocity,localvelocity);

x += totalvelocity.x;
y -= totalvelocity.y;

}
dan1st
dan1stโ€ข2y ago
JavaBot
JavaBotโ€ข2y ago
if (i == 0) {


yAxisRelative = v;

} else if (i == 1) {
v.reverse();

xAxisRelative = v;


}
if (i == 0) {


yAxisRelative = v;

} else if (i == 1) {
v.reverse();

xAxisRelative = v;


}
dan1st
dan1stโ€ข2y ago
before moving in other words, you first update the axis used for collision checking and move afterwards so it uses old information for collision checking take that movement code and try moving it up to the beginning of the tick method .
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1stโ€ข2y ago
ok so the part with the delay works Do you still have the problem of something being 10px off?
Unknown User
Unknown UserOPโ€ข2y ago
Message Not Public
Sign In & Join Server To View
JavaBot
JavaBotโ€ข2y ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
JavaBot
JavaBotโ€ข2y ago
Post Closed
This post has been closed by <@641684400546775040>.

Did you find this page helpful?