Im not intrested in the code for this problem i just want to be pointed in the right direction.
Im using C# XNA if that helps.
Basicaly for my game i am adding collisions for example the player can't walk or fall through stones.. I have the rectangle and i know i use the .intersects comand but would i need to check collision depending on the side of the rectangle here?
I know if the player is falling i can make if playerRectangle intersects stoneRectangle playerY = stopFalling..
But if the rectangle is coliding on the side of the player.. That would be different wouldn't it ?
One simple way of dealing with this is to work out the main axis of penetration, i.e. is the player further into the rectangle horizontally, or vertically. Then, using this information, you'd move the player so they were just touching the rectangle.
i.e. Player bumps into a wall on his right.
Main axis is Horizontal, and he's to the left of the centre of the rectangle, so we know to move him left.
Searching on gamedev.stackexchange.com will give you lots of different collision detection/response options.
If your game is 2D I would recommend using something like FarSeer Physics rather than implementing yourself.
If you are interested in the algorithms it is open source and you can poke around the code to see how they implemented collision detection.
Related
I am trying to make the movement of the checker (shown in a circle) equal to its diameter, the move is made only in the free space. (In the game, you can direct where the checker goes) To represent such a movement, we decided to make a outline circle, as a mark that the checker can go there. As an element of game design and simplicity, the outline circle can collide with other objects, preventing it from going into a closed position.
Here's a gameplay video showing how this should work.
To implement this, I made the following checker hierarchy:
Checkers brain
-> Outline circle
The checker's brain is responsible for the player's turns and the move itself. To embody the turns, we change the transform.rotation, and the move is made to the position of the outline circle.
The outline circle is responsible for collisions with other objects, and does not allow you to go to a closed position.
And to make collisions with other objects, we decided to use the Physics.sphereCast.
The decision to use Physics.Spherecast as a bad decision, that's why:
Consider a situation where, in fact, the checker must pass through these walls. But he cannot pass through this wall, since the Physics.SphereCast works like a ray, the ray colliding with the wall tells the outline circle not to pass through the wall.
Can u find a solution for a outline circle that can collide with objects, but which does not work as a ray?
P.S. I tried to change the original position of Physics.SphereCast, but this does not work due to the fact that Physics.SphereCast only shifts towards the origin.
I'm writing a game just now in Unity. The basic premise is there are a few objects on screen in 3d space which you swipe to destroy. I've done most of the hard graft but got stuck on something I think should be fairly simple.
I've written a touch controller which spits out the start, end and direction of a swipe. How can I use this to check whether the swipe line in screen coordinates intersects an object in 3d space? I've looked into Rays but can't seem to get it working when casting from anywhere but the camera. My objects do have a collider and I attempted (briefly) to use the collider bounds too. Just can't seem to crack it!
Any help is appreciated,
use "Camera.ViewportPointToRay" to shoot rays depending on which pixels are swiped. Bottom-left of the screen is (0,0) top-right is (1,1).
I've searched the board, as well as the oculus board, and unity board. Couldn't really find something that helped.
I'm working on a vehicle simulation. Before we started using the oculus, it was just a regular first person perspective. You used a racing wheel/pedals to drive and the mouse to control all the buttons and switches etc. We use raycasting from the mouse point on the screen into the world to interact with the various controls in the vehicle.
Now that we're using the oculus, the raycast isn't taking into account the distortion matrix used on the oculus cameras. So you're not actually casting a ray at what you're visually clicking on. Using Debug.DrawRay I found that it was slightly off. Just to be sure, I disabled the lens correction via inspector on the OVRCameraController and sure enough the raycasting was working again.
The ray itself is calculated the usual way one does when firing from the mouse point:
ScreenPointToRay(Input.mousePosition);
Would anyone have any idea how I can adjust my ray so it works with lens correction on?
Cheers,
Gordon
Simply multiply the Distortion-Matrix with the Ray's Vectors (Position and Normal) and you have your new Ray. I would suggest using Homogeneous Coordinates with 4x4 Matrix and Vec4's where Positions has component w = 1.0 and Normals have w = 0.0; This way you can simply multiply and you are done - depending on lookup direction you might have to use the inverse matrix :)
Alright, what I ended up doing was creating a 3D cursor, bypassing the distortion matrix entirely.
I placed a gameobject at the same place as the "head" (between left eye and right eye cameras). It has a script on it that rotates up/down/left/right based on mouse movement. I then temporarily put a spot light with a narrow cone and high intensity on it so it looked like a laser pointer. I figured if the light is hitting things, so should a raycast of the same origin. Which ended up working.
However this didn't really solve the issue of using a cursor. I tried a number of things that ultimately didn't work (didn't line up with with where the light/raycast hit).
Finally I realized I was overlooking something very simple. I lowered the near clipping plane of the cameras and placed a plane as close as I could to the camera while still being visible. I then rotated it on local y by 180 so it would be invisible to the cameras and not block ray casts.
I then added some code so that when a raycast hit something, it would fire a second raycast from the hit point back to the origin. On the way it would have to hit the plane, which was essentially at the near clipping plane. I would then move my 3D cursor to that hit point.
Now it works as intended. Where the cursor is, is where the original raycast hit. The cursor now matched the position of the laser dot. So then I removed the light component. Done.
Hope this helps someone else someday.
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
I'm designing a 3D game with a camera not entirely unlike that in The Sims and I want to prevent the player character from being hidden behind objects, including walls, pillars and other objects.
One easy way to handle the walls case is to have them face inwards and not have an other side, but that won't cover the other cases at all.
What I had planned is to somehow check for objects that are "in front" of the player, relative to the camera, and hide them - be it by alpha blending or not rendering at all.
One probably not so good idea I had in mind is to scan from the camera to the player in a straight line and see if you hit a non-hidden object, continuing until you reach the player. Unfortunately, I am an almost complete newbie on 3D programming.
Demonstration SVG illustration < that wall panel obscures the player, so it must be hidden. Another unrelated and pretty much already solved problem is removing all three wall panels on that side, which is irrelevant to this question and only caused by the mapping system I came up with.
What I had planned is to somehow check for objects that are "in front" of the player, relative to the camera, and hide them - be it by alpha blending or not rendering at all.
This is a good plan. You'll want to incorporate some kind of bounding volume onto the player, so the entire player (plus a little extra) is visible at all times. Then, simply run the intersection algorithm for each corner of the bounding volume.
Finding which object is at a given point on screen is called picking. Here's an XNA link for you which should direct you to an example. The idea is that you retrieve the 3D point in the game from the 2D point, and then can use standard collision detection methods to work out which object is occupying that space. Then you can elect to render that object differently.
One hack which might suffice if you have trouble with the picking approach is to render the character once as part of the scene, and then render it again at the end at half-alpha on top of everything. That way you can see the whole character and the wall, though you won't see through the wall as such.
One easy way, at least for prototyping, would be to always draw the player after you draw the rest of the scene. This would ensure that the player is rendered on top of anything else in the scene. Crude but effective.
Create a bounding volume from the camera to the extents of the player, determine what objects intersect that volume, and then render them in whatever alternate style you want?
There might be some ultra-clever way to do this, but this seems like the pretty straightforward version, and shouldn't be too much of a perf hit (you're probably doing collision every frame anyway....)
The simplest thing I can think of that should work relatively well is to model all obstacles by a plane perpendicular to your ground (assuming you have a ground.) Roughly assuming everything that is an obstacle is a wall with some height.
Model your player as a point somewhere, and model your camera as another point. The line in 3d that connects these two points lies in a plane that is particularly interesting to you, because if this plane intersects an "obstacle plane" below the height of the obstacle, that means that that obstacle is blocking your view of the player point.
I hope thats somewhat clear. To make this into an algorithm you'd have to implement a general method for determining where two planes intersect (to determine if the obstacle is tall enough to block view.)