Monogame - sprite not rotating around origin point - rotation

I'm trying to rotate a sprite around its center with following code:
Vector2 origin = new Vector2(position.Width / 2, position.Height / 2);
s.Draw(position, origin, angle, Color.White);
spriteBatch.Draw(texture, position, sourceRectangle, color, rotation, origin, SpriteEffects.None, 0);
Note: Since I'm drawing from a sprite sheet, the source rectangle is being calculated.
The original size of my sprite is 15x32. If I use this size, the rotation looks nearly correct but it's still a little bit shifted:
However, when I resize the width and height to 75x128 the sprite is completely displaced:
Is there a way to always place the sprite correct, when resizing it? And why is the sprite even displaced when drawing it in the original size?
By the way, the green box is the origin point with the size of the sprite.
Thank you very much!

Ok I figured it out myself!
When creating the origin point, I used the new Width and Height (75, 128) but one has to use the width and height of the original sprite. In this case (15, 32).

Related

Does Three.js sphere geometry have a size limit?

I am making a Three.js sphere geometry with a radius of 4000, put a JPEG image on it and position the camera within its center and it works fine.
When I made the sphere with a radius of 9000 so the texture should look further away, it appeared as if there was a black "hole" in the sphere in the very direction the camera was looking. Also the texture on the sphere looks nearer and not further away. When I move the camera towards this "hole" it becomes smaller.
new THREE.SphereGeometry(9000, 32, 32);
The camera has a .near and .far setting. If .far is lower than your sphere radius, it will clip like you are describing.
Try setting camera.far = 20000

Update plane texture offset from movement on a sphere

I'm working on a driving simulation in Three.js using height map data from the planet Venus.
GitHub repo here: https://github.com/hypothete/venus-walk
Here's how the simulation works so far:
In a hidden scene, a camera called the globeCamera moves at a fixed height over a sphere textured with the Venus height map. You can see this happening in the lower left viewport in my picture. The globeCamera renders its view to a WebGLRenderTarget to be used as a local height map. The result is in the second viewport in the middle left.
In the visible scene, a plane mesh called the terrainMesh has its vertices displaced up and down in correspondence with the values from the local height map. This gives the illusion that a vehicle placed in the center of the plane is moving across a surface when actually we're just updating the plane's vertices from the movement of the globeCamera.
Since I know the rotation of the globeCamera, I can pass that value to my fragment shader to rotate the terrainMesh's rock texture with the height map.
How can I offset the rock texture's position so that texture units translate with the terrain as well? I've tried tracking the globeCamera's offset as a 2D vector and adding that to the rotated UV in the fragment shader, but my results were inconsistent. Thanks for your help.

libgdx: Rotate a texture when drawing it with spritebatch

Im trying to rotate textures when I draw them. I figured it would make more sense to do this than to rotate the images 90 degrees in paint.net and save them in different files. I looked thought the api documentation for spritebatch drawing arguments but I just dont understand. There are a bunch of arguments such as srcX, srcY, originX and so on. Also i would like to know how to do the same for texture regions. Heres a link to the api documentation page:http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/SpriteBatch.html
Thank you!
again from the documentation, but copied here for ease of use and so I can explain a little better.
x - the x-coordinate in screen space
y - the y-coordinate in screen space
these two values represent the location to draw your texture in screen space (game space). Pretty self explanatory.
originX - the x-coordinate of the scaling and rotation origin relative to the screen space coordinates
originY - the y-coordinate of the scaling and rotation origin relative to the screen space coordinates
these two values represent the location where rotations (and scaling) happen from with respect to the screen space. So for instance, if you give the value 0, 0 here, the rotation and scaling will happen around one of the corners of your texture (the bottom left I believe), whereas if you give the center (width/2, height/2), the rotation and scaling would happen around the center of your texture (this is probably what you want for any "normal" rotations)
width - the width in pixels
height - the height in pixels
the dimensions for drawing your texture on screen.
scaleX - the scale of the rectangle around originX/originY in x
scaleY - the scale of the rectangle around originX/originY in y
values representing the scale of your rectangle, where values between 0 and 1 will shrink the rectangle, and values greater than 1 will expand the rectangle. Note that this is with respect to the origin you gave earlier, which means that if this is not the center the image may look distorted.
rotation - the angle of counter clockwise rotation of the rectangle around originX/originY
the angle to rotate the image by. Again, this is around the origin given earlier, so the rotation may not appear "correct" if the origin is not the center of the image
srcX - the x-coordinate in texel space
srcY - the y-coordinate in texel space
these two values are the starting location of the actual region of the image file (.png, .jpg, whatever) that you wish to use, in pixels. Basically the start of your image.
srcWidth - the source with in texels
srcHeight - the source height in texels
similarly, these two values are the width and height of the actual region of the image file you are using, in pixels.
flipX - whether to flip the sprite horizontally
flipY - whether to flip the sprite vertically
Finally, these two booleans are used to flip the image either horizontally or vertically.
Now you may notice that the similar method for drawing TextureRegions has no srcX, srcY, srcWidth, or srcHeight. This is because those are the values you give to a texture region when you create it from a texture.
Essentially what that means is that the command
//with TextureRegions
SpriteBatch.draw(textureRegion, x, y, originX, originY, width, height, scaleX, scaleY, rotation);
is equivalent to
//with Textures from TextureRegions
SpriteBatch.draw(textureRegion.getTexture(), x, y, originX, originY, width, height, scaleX, scaleY, rotation, textureRegion.getRegionX(), textureRegion.getRegionY(), textureRegion.getRegionWidth(), textureRegion.getRegionHeight(), false, false);

Is it possible in three.js to crop the viewport?

I have a scene with a sphere in the middle and a camera spinning around it. Now I was wondering if it is possible with Three.JS to make a crop of this viewport so that the sphere for example would appear on the left or right.
I'm not looking to moving the camera or looking at a point besides the sphere, since that would distort the perspective, but a clip or crop of the rendering.
Is this possible?
One potential solution is to position a second canvas over your Three.js canvas, then re-draw the three.js scene onto your second canvas by blitting:
overlayCanvas.getContext('2d').drawImage(threeCanvas, 40, 40, 960, 960, 0, 0, 1200, 1200);
You can read more about the parameters to drawImage() in the documentation, but what this does is crop a certain area of the three.js canvas and stretch it over a larger area of your second canvas. It should be much faster than rendering the scene a second time, and it will display at the size you want it (albeit with some stretching).
You can alleviate the stretching by drawing the original (three.js) scene at a larger size than it would normally be displayed such that the area you want to crop into view is the same size as your secondary canvas.

Rotating an NSView

I need to rotate the frame of an instance of NSView in such a way that its width becomes its height and its height becomes its width. The view contains a string and it is important that this string is rotated as well.
I had a look at NSView's setFrameRotation: but this rotates around the frame origin which is not what I want. I suppose that technically my requirement is not a rotation but more a mirroring at a 45 degree angle from the origin.
How can I accomplish this?
Try Desire to rotate around centre
This piece of could should helper.
[NSView rotateByAngle]

Resources