How to move 3D model on XNA? - visual-studio-2010

I want to control my 3D model, move it every direction but I don't know way to do this. Anybody have any idea ?

You only have to provide to the effect the model's world transform.
Matrix World = Matrix.CreateWorld(position, forward, up);
In your update method you can modify the position:
LastPosition = Position;
if (IsKeyDonw(Left) Position -= Vector3.UnitX * Speed * ElapsedTime; ForwardDirty = true;
if (IsKeyDonw(Right) Position += Vector3.UnitX * Speed * ElapsedTime; ForwardDirty = true;
if (IsKeyDonw(Up) Position -= Vector3.UnitZ * Speed * ElapsedTime; ForwardDirty = true;
if (IsKeyDonw(Down) Position += Vector3.UnitZ * Speed * ElapsedTime; ForwardDirty = true;
// the forward is the direction where will point your model.
if (ForwardDirty) {
Forward = Position - LastPosition;
Forward.Normalize();
ForwardDirty = false;
}
You also can base your movement in forward vector, or smooth the angle change interpolating the final forward with the current,...

Related

Weird values while debugging values in Unity

I found out while debugging floats and stuff in Unity the values are sometimes off. I.e. I have a float called currentSpeed and when the player is moving this is a value and when he is not it is 0. The problem is when debugging this value while walking the following happens; i.e. 1,1,1,1,1,1,0,1,1,1,1,1,1,0,1,1,1. I found out this is happening with multiple values and Vectors. The weird thing is, when my framerate is lower it doesn't happen (i.e. while having the player selected in the inspector the framerate drops from 60 to 48). I'm using Unity 2018.3 and have no clue how to fix this. I hope someone can help me.
Edit (added code):
void Move(Vector2 inputDir, bool running)
{
if (inputDir != Vector2.zero)
{
// When the player is not pushing we can create a moving direction
if (!playerIsPushing)
{
float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));
}
}
float targetSpeed = ((running) ? runSpeed : movementSpeed) * inputDir.magnitude;
currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));
velocityY += Time.deltaTime * gravity;
Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
// Here we cap the players velocity to a maximum speed. In this case the runSpeed. This means the player can never exceed the runSpeed.
if (velocity.x > runSpeed)
{
velocity.x = runSpeed;
}
if (velocity.x < -runSpeed)
{
velocity.x = -runSpeed;
}
if (velocity.z > runSpeed)
{
velocity.z = runSpeed;
}
if (velocity.z < -runSpeed)
{
velocity.z = -runSpeed;
}
controller.Move(velocity * Time.deltaTime);
currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z).magnitude;
// Debugging the players velocity
Debug.Log(currentSpeed);
float animationSpeedPercent = ((running) ? currentSpeed / runSpeed : currentSpeed / movementSpeed * .5f);
Edit 2: I have tested some more and found out my pushforce isn't framerate independant. During pushing an object in my game I see the weird values while debugging i.e. 1,1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1. I'm working with a CharacterController so adding force to a rigidbody is done via script.
void OnControllerColliderHit(ControllerColliderHit hit)
{
Rigidbody body = hit.collider.attachedRigidbody;
if (body == null || body.isKinematic)
return;
if (hit.moveDirection.y < -.3f)
return;
Vector3 pushDirection = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);
body.velocity = pushForce * pushDirection;
}
Might this be the issue for the weird values?

DirectX Camera Jitter

Source Code.
I'm making a small DirectX Demo Scene but my camera seems to "snap" to odd positions when I attempt to rotate it. It only happens when rotating and I can't seem to find out what is causing it.
// Get the cursor pos and calculate change in movement
POINT cursorPos;
GetCursorPos(&cursorPos);
LONG deltaX = oldCursorPos.x - cursorPos.x;
LONG deltaY = oldCursorPos.y - cursorPos.y;
// Hold right click to rotate
if (GetAsyncKeyState(VK_RBUTTON))
{
XMMATRIX xRotation = XMMatrixRotationY(((float)-deltaX * (float)timer.Delta()));
XMMATRIX yRotation = XMMatrixRotationX(((float)-deltaY * (float)timer.Delta()));
XMMATRIX view = XMLoadFloat4x4(&cameraMatrix);
XMFLOAT4 viewVector = XMFLOAT4(cameraMatrix.m[3][0], cameraMatrix.m[3][1], cameraMatrix.m[3][2], 1.0f);
for (size_t i = 0; i < 3; i++) { cameraMatrix.m[3][i] = 0.0f; }
view = view * xRotation;
view = yRotation * view;
XMStoreFloat4x4(&cameraMatrix, view);
cameraMatrix.m[3][0] = viewVector.x;
cameraMatrix.m[3][1] = viewVector.y;
cameraMatrix.m[3][2] = viewVector.z;
}
oldCursorPos = cursorPos;
Above is the code that performs the rotations to the camera matrix, below is the code I use to set the view matrix equal to the inverse of the camera matrix. Both of these operations are done every frame.
XMMATRIX camera = XMLoadFloat4x4(&cameraMatrix);
XMMATRIX view = XMMatrixInverse(NULL, camera);
XMStoreFloat4x4(&sceneMatrix.viewMatrix, view);
Both of these snippets don't seem to be the problem though, as I have triple checked my notes and this is exactly how my instructor expects it to be done. This bug happens in debug and release mode.
I put the source code in the link above if an attractive person such as yourself dare look at the rest of the code. Beware: It is a small demo application so try not to cringe at the hard-coded objects and such.
I'm not certain it's causing your problem, as a simple demo might have a consistent frame-rate, but you shouldn't be scaling mouse movement by a time delta.
These lines:
XMMATRIX xRotation = XMMatrixRotationY(((float)-deltaX * (float)timer.Delta()));
XMMATRIX yRotation = XMMatrixRotationX(((float)-deltaY * (float)timer.Delta()));
Should be
float fRotationSpeed = 0.01f; // Tweak this.
XMMATRIX xRotation = XMMatrixRotationY(((float)-deltaX * fRotationSpeed));
XMMATRIX yRotation = XMMatrixRotationX(((float)-deltaY * fRotationSpeed));

Unity: rotating object around Z axis with Lerp/Slerp

Im trying to rotate my player to face where I last clicked. I've acutally manged to do this, but now I want to see the player rotate at a set speed instead of the sprite just changing rotation instantly.
Ive tried several methods I've found online, but none of them work for me. Here's what I have
void Update()
{
if (Input.GetMouseButtonDown (0))
{
Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
diff.Normalize();
float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
transform.rotation= Quaternion.Euler(0f, 0f, rot_z - 90);
Instantiate(ProjectilePrefab, transform.position, transform.rotation);
}
}
The code above works fine, but it shows no movement. I have tried to do this but the position is wrong and the rotation is instant as well:
Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
var newRotation = Quaternion.LookRotation(diff);
newRotation.y = 0.0f;
newRotation.x = 0.0f;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 30);
ANy ideas?
Two problems here. First, the Slerp function is only called once after the user pressed the mouse button. You have to move it outside the if part or use a coroutine. Second, Slerp expects a float from 0 to 1 to indicate the progress and not time or deltaTime. The official examples covering the lerp functions are just bad, because using the time value would work only during the first second the game is running.
You need something like this
if (Input.GetMouseButtonDown (0)) {
// your other stuff here
float starTime = Time.time;
}
float t = Time.time - startTime;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, t);
The rotation would finish after one second. If you want it faster or slower just multiply t with a factor.
I found the answer. This is how I made it work
if (Input.GetMouseButtonDown (0)) {
target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
rotateToTarget = true;
print ("NEW TARGET: " + target);
}
if (rotateToTarget == true && target != null) {
print ("Rotating towards target");
targetRotation = Quaternion.LookRotation (transform.position - target.normalized, Vector3.forward);
targetRotation.x = 0.0f;//Set to zero because we only care about z axis
targetRotation.y = 0.0f;
player.transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, Time.deltaTime * rotationSpeed);
if (Mathf.Abs (player.transform.rotation.eulerAngles.z - targetRotation.eulerAngles.z) < 1) {
rotateToTarget = false;
travelToTarget = true;
player.transform.rotation = targetRotation;
print ("ROTATION IS DONE!");
}
}

Transform mouse coordinates to 3d coordinates

I have the following problem:
I have my mouse coordinates and I have a Model (Data Points) and I want the 3d Coordinates of my mouse coordinates and my lookAt Vector of this coordinates, so that I can make a raycast with the object, so that I can see the 3d values of the data points. So I want to click with the mouse and then I want to see the coordinates of the datapoint I clicked at.
I have the following from a tutorial, but it doesn't work. The ray origin and ray direction are not correct (I draw I line from ray origin in the ray direction and the ray origin isn't right:
Can anyone help me? Here is the code:
// Move the mouse cursor coordinates into the -1 to +1 range.
pointX = ((2.0f * (float)mouseX) / (float) screen_width) - 1.0f;
pointY = (((2.0f * (float)mouseY) / (float) screen_height) - 1.0f) * -1.0f;
m_D3D->GetProjectionMatrix(projectionMatrix);
pointX = pointX / projectionMatrix._11;
pointY = pointY / projectionMatrix._22;
// Get the inverse of the view matrix.
m_Camera->GetViewMatrix(viewMatrix);
D3DXMatrixInverse(&inverseViewMatrix, NULL, &viewMatrix);
// Calculate the direction of the picking ray in view space.
direction.x = (pointX * inverseViewMatrix._11) + (pointY * inverseViewMatrix._21)+
inverseViewMatrix._31;
direction.y = (pointX * inverseViewMatrix._12) + (pointY * inverseViewMatrix._22)
+ inverseViewMatrix._32;
direction.z = (pointX * inverseViewMatrix._13) + (pointY * inverseViewMatrix._23)
+ inverseViewMatrix._33;
// Get the origin of the picking ray which is the position of the camera.
origin = m_Camera->GetPosition();
// Get the world matrix and translate to the location of the sphere.
m_Impact->GetWorldMatrix(worldMatrix);
//D3DXMatrixTranslation(&translateMatrix, -5.0f, 1.0f, 5.0f);
//D3DXMatrixMultiply(&worldMatrix, &worldMatrix, &translateMatrix);
// Now get the inverse of the translated world matrix.
D3DXMatrixInverse(&inverseWorldMatrix, NULL, &worldMatrix);
D3DXVec3TransformCoord(&rayOrigin, &origin, &inverseWorldMatrix);
D3DXVec3TransformNormal(&rayDirection, &direction, &inverseWorldMatrix);
// Normalize the ray direction.
D3DXVec3Normalize(&rayDirection, &rayDirection);
//collision_object->setTransform(col_matrix);
collision_model->setTransform(col_matrix);
float collision_point[3];
//bool collision_result = collision_object ->rayCollision(rayOrigin,
rayDirection, true);
bool collision_result = collision_model ->rayCollision(rayOrigin,
rayDirection, true);
if(collision_result == true)
{
intersect = true;
//collision_object->getCollisionPoint(collision_point, true);
collision_model->getCollisionPoint(collision_point, false);
*coordX = collision_point[0];
*coordY = collision_point[1];
*coordZ = collision_point[2];
}
One simple way to build a ray from mouse is as follow (pseudo code)
Get mouse coords to -1 -> 1 range (as you already do)
Create view projection matrix (view*projection)
Invert it.
Create 2 mouse vectors:
near = Vector3(mousex,mousey,0);
far = Vector3(mousex,mousey,1);
rayorigin = transformcoord(near, inverseviewprojection);
rayend = transformcoord(far, inverseviewprojection);
raydir = normalize(rayend-rayorigin);

Tile map collision detection

There are many topics like this, but none with concrete answers. I am drawing a tile-map in the traditional way (two for loops) and keeping my player centered except when the edges of the map is reached. How would I create collision detection? I need to know how to translate tile location in the array to screen coordinates I think.
I will give you the code i wrote for point/tilemap collision detection. The code assumes that you have a point at (xfrom, yfrom) and you want to move it to (xto, yto) and want to see if there is a collision with a block in the tilemap map[Y][X]. I assume a method isSolid(tileId) which will return true if the tile is solid.
/**
* This method returns true if there is a collision between a point and a 2D tilemap map[Y][X].
* The isSolid method must be implemented to indicate if a tile is solid or not
* Assumes the tilemap starts at (0,0) and TILEWIDTH and TILEHEIGHT hold the size of a tile (in pixels)
* #param xfrom the original x-coordinate of the point
* #param yfrom the original y-coordinate of the point
* #param xto the destination x-coordinate of the point
* #param yto the destination y-coordinate of the point
* #param outCollisionPoint output the location where the collision occurs
* #return true if a collision is found
*/
public boolean collisionDetection(int xfrom, int yfrom, int xto, int yto, Point outCollisionPoint){
//Ref: A fast voxel traversal algorithm J.Amanatides, A. Woo
float tMaxX, tMaxY, tDeltaX, tDeltaY, collisionLength;
int X, Y, stepX, stepY, endX, endY, blkX, blkY;
//Calculate direction vector
float dirX = (xto - xfrom);
float dirY = (yto - yfrom);
float length = (float) Math.sqrt(dirX * dirX + dirY * dirY);
//Normalize direction vector
dirX /= length;
dirY /= length;
//tDeltaX: distance in terms of vector(dirX,dirY) between two consecutive vertical lines
tDeltaX = TILEWIDTH / Math.abs(dirX);
tDeltaY = TILEHEIGHT / Math.abs(dirY);
//Determine cell where we originally are
X = xfrom / TILEWIDTH;
Y = yfrom / TILEHEIGHT;
endX = xto / TILEWIDTH;
endY = yto / TILEHEIGHT;
//stepX: Determine in what way do we move between cells
//tMaxX: the distance in terms of vector(dirX,dirY) to the next vertical line
if (xto > xfrom){
blkX = 0;
stepX = 1;
tMaxX = ((X+1) * TILEWIDTH - xfrom) / dirX;
}else{
blkX = 1;
stepX = -1;
tMaxX = (X * TILEWIDTH - xfrom) / dirX;
}
if (yto > yfrom){
blkY = 0;
stepY = 1;
tMaxY = ((Y+1) * TILEHEIGHT - yfrom) / dirY;
}else{
blkY = 1;
stepY = -1;
tMaxY = (Y * TILEHEIGHT - yfrom) / dirY;
}
if (isSolid(map[Y][X])) {
//point already collides
outCollisionPoint = new Point(xfrom, yfrom);
return true;
}
//Scan the cells along the line between 'from' and 'to'
while (X != endX || Y !=endY){
if(tMaxX < tMaxY){
tMaxX += tDeltaX;
X += stepX;
if (isSolid(map[Y][X])) {
collisionLength = ((X + blkX) * TILEWIDTH - xfrom) / dirX;
outCollisionPoint = new Point((int)(xfrom + dirX * collisionLength), (int)(yfrom + dirY * collisionLength));
return true;
}
}else{
tMaxY += tDeltaY;
Y += stepY;
if (isSolid(map[Y][X])) {
collisionLength= ((Y + blkY) * TILEHEIGHT - yfrom) / dirY;
outCollisionPoint = new Point((int)(xfrom + dirX * collisionLength), (int)(yfrom + dirY * collisionLength));
return true;
}
}
}
return false;
}
It depends on the model.
If your model (the data) is a grid, then a collision occurs simply when two incompatible objects occupy the same location. The easiest way to handle this type of collision is just to make sure where you are trying to move a game entity to is "available". If it is, no collision, and update the model. If it wasn't free, then there was a collision.
The screen simply renders the model. With the exception of something like of per-pixel collision detection (think the original lemmings or worms), don't use it for collision detection.
The screen/view is just the rendering agent. While you can have the model tied tightly to the screen (e.g. you only need to update parts of the screen in which things have changed such as when a piece is moved), the screen is not, and should not, generally be considered part of the model. However, with modern computing speed, you might as well simple re-render the entire visible model each frame.
(Yes, I know I repeated myself. It was on purpose.)
Now, to answer the secondary question not mentioned in the title:
When you start rendering, simply draw screen_width/cell_width/2 cells to the left and screen_width/cell_width/2 cells to the right of the player (the player is assumed to take 1x1). Do the same for the up-and-down. Make sure to not cause an Index-Out-Of-Bounds exception. You can run the for-loops with out-of-bounds values, as long long as you clamp/filter before using them. If you wish to only make the character "push" the edge when he gets close, keep track of a current model-to-view reference as well.

Resources