Is there a 3D equivalant to clamp in XNA? - c#

I'm building a 3D game but i only plan on using a 2D perspective thus not taking the z axis into the equasion,
i want to be able to limit the movement of one of my models so it doesn't move out of the unmoving field of view,
when i was designing 2D it was simple just use clamp, but i cant seem to figurebout how to do this in 3d
any help would be much appreciated
Regards

Just use Vector3.Clamp(Vector3 value1, Vector3 min, Vector3 max) and use the constructor on Vector3 that takes a Vector2 and an int (for z value).

If you simply want to do the same thing as a 2D clamp would - Vector3.Clamp set the Y (usually 'UP') component of the two bounding vectors you pass to be 0.
I'm slightly confused on the question however, it seems that maybe what you are after is a form of collision detection with the view frustum This article may help with that if your model can fit into a bounding sphere relativity nicely.
You will need to for test collision vs all the planes which define the view space. If its a perspective camera your using, you'll need to get the Frustum Planes, otherwise if its an orthographic camera they are the planes that make the bounding box of the view space (a cuboid which is orientated the same way as the camera).

So you want in pseudocode:
if (object.position+movementVec is in view of camera)
object.position+=movementVec;

Related

drawing multiple vector3 on a 3d coordinate axis (c#)

I need to create a program which draws multiple vector3 in 3 dimensional space, like so: http://www.intmath.com/vectors/7-vectors-in-3d-space.php
the only trouble being I have no idea how to get started with drawing an x,y and z axis and then drawing the vectors. Any help?
Not sure what kind of program you are going for, but this kind of thing is usually easier in any kind of 3d game engine like Unity, for example.

Rotate an object according to a plane normal

I am making a FPS game, I created a peaceful AI for the moment and when the character is died, I just want it to be oriented according to the normal below it. I show you the result for the moment :
as you can see, the character is flying, because the terrain is not straight.
I am trying (without success) to make something like that :
I have the (x,y,z) coordinates (character position) and the normal to the plane.
Yes the normal is always facing up, as you can see on my drawing, even if I drew it in the good pose. I dont understand when you are talking about the quaternion, which new normal are you talking about ?
I already have the normal of the plane under the character, so, part of the jobs is already done :)
Your character's death pose is always with the normal facing up, right?
And you want to rotate it to another normal.
You can find a Quaternion that represents the rotation between two vectors (up and the new normal) to rotate the mesh.
This question has the answer to that:
Quaternion from two vector pairs
To know the normal of your terrain, you can probably cast a ray down and get the normal from the collision information depending on the physics engine you are using.
Edit:
Some background info:
A quaternion represents a rotation. So what I'm suggesting is that you use the answer from the question I linked to calculate the rotation between the default orientation of the character's death pose (UP) to the new orientation (Terrain Normal)
Then you can just transform your dead character with the Quaternion and it'll follow the terrain's normal.
Here's a sketch: calculate Q from UP and N with the solution from the Link and rotate your character's model with Q:

xna collision without max speed

I'm making a platform game and I've looked at different ways to add collisions in a tile based game without rotation. But everything I look at assumes that in no case will one of the colliding objects be going fast enough to pass the other before collision is detected. I've tried using Box2D and Farseer but they were over complicated and ran quite slowly when making lots of tiles. I also tried my own method using 2D convex hulls but that ran too slowly as well. So is there a way to to detect collisions without a maximum speed or letting items pass each other that isn't over complicated and would work with lots of tiles? any help is much appreciated
Depend on objects and their movement and whole gameplay. if you are shooting bullet to some obstacle, you can calculate if bullet will intersect with obstacle before bullet hit it. you have positions, you can calculate distance between them. so you can check collision and distance.
other way, as #cwohlman suggested. you can have 2 collision boxes, one big "assumed" collision area and one excact that cover objects. so if object triggers assumed collision, you know that there i a chance that bullet will hit object and you do some detailed calculations if button will hit object or will pass above or under it. for determinating if object is in assumed collision area you can use circle collsion.
private bool DetectCircularCollision(Sprite a, Sprite b)
{
float radius = Math.Sqrt(a.Width / 2 * a.Height / 2 + b.Width / 2 * b.Height / 2);
float distance = Vector2.Distance(a.Position, b.Position);
if (distance < radius) return true;
return false;
}
so far i didn't encounter this problem, as all flying objects should be visible to players eye. if you move object 50px or 100px per frame player will not notice this or will be confused what is this.
You will probably need to combine few collision methods to achieve what you need. Advice, do not use pixel collsion as it's completly unnecessary and it's resuorce killer. Instead, cover objects with few bounding boxes or circles.
example where blue circle is assumed collision area, and red circles are exact collision area.
You might try a dual algorithm approach, using a simple algorithm first to deturmine if the objects 'might' have collided and if if so, use a complex algorithm to see if they did collide.

A point in 3D space from an angle

I'm currently working on a camera for a game. But I got stuck at the rotation.
When I move my mouse across the x- or y-axis, I want the camera to rotate around my character.
What would be a formula to calculate this vector, if the distance to the character is always the same?
I am doing this in Unity, with C#, if this is of any help.
this function may help: transform.RotateAround(Vector3 axis, float degree)
you can read the Unity Script Reference for more info.
-oh and I think you should tag your next questions with "unity3d"
but you get the best Unity3d-help at UnityAnswers-Forum http://answers.unity3d.com/index.html.
You can utilise spherical coordinates — they seem to fit more than Euler angles for the purpose of camera movement. The Cartesian vector which you need can be obtained by simple formulas as described there.

Collision Detection for arbitrarily sized, positioned and rotated rectangles in XNA

I'm working with xna in C# and in my game I will have a variety of space ships flying all over the place. They will each have an arbitrary rotation, size and position in space and I need a method to determine when they collide. Ideally the method would take two Rectangles, two doubles and two Vector2s for size, rotation and position respectively and return a boolean that indicates whether they have intersected or not.
Have a look at these links:
Collision Detection Overview
Collision Detection Matrices
Putting Collision Detection Into Practice
They show you a way to do pixel-based collision detection, which is more accurate than rectangle-based for irregularly shaped objects.
Update 2021-01-17 (Martin Senne)
Links of XNA have moved to
XNA
XNA - Collision
You could also consider just using an out of the box solution for this and integrating something like the Farseer Physics Engine:
http://farseerphysics.codeplex.com/
These rectangles you describe are called OBB (Oriented Bounding Boxes)
The way to do collisions between them is using the 'Separating axis theorem'
A really nice page describing it in detail with lots of pictures can be found here

Categories

Resources