Rendering a image with Graphics2D based on 4 points
103 Replies
โ
This post has been reserved for your question.
Hey @Hunter! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose 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.
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
๐ค
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.
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 yourselfFrom 4 points there is no reason to believe the points make out a rectangle, yet that method assumes so
oh
right
My bad
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
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
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
Do these 4 points form a rectangle or are they completely arbitrary?
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.

Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
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 UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
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
e.g. can the points be arranged like this?

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
@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.

Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
oops
@Hunter
btw, that took me about a month to figure out (I was messing around with distancing before) so yw
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.
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 UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
Why?
Yes
tbh I wouldn't be so sure about that
๐ค
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 UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
reduce it to a minimal reproducable example
(so that it isn't problematic in any way)
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
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 UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
Are you possibly blocking the UI thread?
๐ค
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 UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
Is your code actually executed delayed or does your code execute in time and it's just the rendering?
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
How much behind?
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
px?
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
not milliseconds?
I thought behind meant behind in time
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
with behind, do you mean it's below than where it should be?
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
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.
Can you check whether the tick method is actually executed 100 times per second?
i.e. you can add something like
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
Is it just the image?
What values does that typically print?
How much delay is there with rendering?
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
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 UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
you mean the screen is black?
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
oh nvm it didn't show properly
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
ok you have a square and a line
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
what's the issue about that?
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
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?Also adjust the dt then ๐
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
well ig my goal is to make everything run slower
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
do you do move calculation in a separate thread or something?
or rendering in a separate thread?
GitHub
SAT/src/main/java/org/example/Display.java at master ยท zivthefire/SAT
Contribute to zivthefire/SAT development by creating an account on GitHub.
What exactly do you mean with moving and then rendering?
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
yes
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
well calculating the overlap happens one tick after moving, right?
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
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 UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
is it intended that you don't use the result of
isColliding
?Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
https://github.com/zivthefire/SAT/blob/0acdac4ca664f191dad9a1413244a7f31ceb7198/src/main/java/org/example/OBB.java#L428-L433
You aren't using
additionX
/additionY
here. Is that intended?GitHub
SAT/src/main/java/org/example/OBB.java at 0acdac4ca664f191dad9a1413...
Contribute to zivthefire/SAT development by creating an account on GitHub.
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
ah ok
This this issue happen regardless of where you collide from?
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
ok
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
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 alreadyUnknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
the things with the time changes
and the
System.out.println
with the time differenceUnknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
Did you check that?
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
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 whyyou are mvoing here: https://github.com/zivthefire/SAT/blob/0acdac4ca664f191dad9a1413244a7f31ceb7198/src/main/java/org/example/OBB.java#L833-L843
GitHub
SAT/src/main/java/org/example/OBB.java at 0acdac4ca664f191dad9a1413...
Contribute to zivthefire/SAT development by creating an account on GitHub.
but you are updating the relative axis here: https://github.com/zivthefire/SAT/blob/0acdac4ca664f191dad9a1413244a7f31ceb7198/src/main/java/org/example/OBB.java#L807-L818
GitHub
SAT/src/main/java/org/example/OBB.java at 0acdac4ca664f191dad9a1413...
Contribute to zivthefire/SAT development by creating an account on GitHub.
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 UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
ok so the part with the delay works
Do you still have the problem of something being 10px off?
Unknown UserOPโข2y ago
Message Not Public
Sign In & Join Server To View
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.
Post Closed
This post has been closed by <@641684400546775040>.