Matter.js Collision Filtering Not working - collision

Inspired by the code bullet I wanted to make a hill climb racing game and so like him, I'm using the p5.js engine but unlike him, I'm going to use matter.js as my physics. Ive finished creating the terrain using perlin noise. And am now creating the car. I don't want to use the matter.js car because there doesn't seem like there is any collision is there so plan b is to create 2 circles that are our wheels that are constraind with each other. Also create a car body that's constraint to the wheel. Now in this I also want it to be so that there is collision between the wheels and the ground and the car body and the ground but not the body and the wheel because I want to use suspension in my car. So my question is how do I make it so that the ground can collide with the car body and wheel but the car body and wheel don't collide with themselves. I've been trying this with many failed attempts with this being the latest https://github.com/KidCoderT/hill-climb-racing and I don't understand why it happens. Ive created 3 different masks.
let worldCategory = 0x0002,
carBodyCategory = 0x0003,
carWheelCategory = 0x0004;
and applied them separately to each item yet they all collide with each other what am I doing wrong. Please help me thank you

Related

AS3 Using a object as collision detection

I'v been thinking and I want to know if this is a efficient way to make a game collision.
I am trying to make a game like Double Dragon(NES). I'm trying to copy the mechanic of moving up and down on a 2D screen but when you go back you go behind it.
But the mechanic I'm having trouble with is I want the feet to collide with an object but I'm not sure if hitTestPoint will do it so I want to use a object to be the collision detector.
The Player Collider will be attached to the feet, but I don't know how to keep the Collider on the feet of another object. I'v done it but when I do it, it is very laggy and I want to know how you guys would handle this.
Over all what I'm trying to say is. There are 2 objects, 1 of them is the player sprite and the other is the collision detector (which is also a object). I would like to know if that would be ok to use as a collision detector or is there a more efficient way of doing it.

having trouble setting up simple rectangular collisions in chipmunk

Recently I've been trying to create something I've always wanted, but never had the skill and time to do - a computer game. To be more precise, a homage / clone of one of many of my favourite games. To start with something simple I've decided to create a classic 2D platform based on the Castlevania series.
Being a Ruby programmer I've decided to use Gosu. Then I decided I don't want to reinvent the wheel so I'm going to use Chipmunk.
After a few days, I've ended up having inexplicable collision detection problems. I've added boundary-box drawing functions just to see what the hell is going on.
As you can see, Belmont collides with blocks of walls he's not remotely close to touching. Since the demo game included with gosu gem works fine, there must be something wrong I'm doing, I probably don't really udnerstand how a polygon Shape is defined and added to the space. I'm pretty sure it's not really where I draw it.
There's a public repo with the game, so you can see how walls (Brush < Entity) and player (Player < Entity) are defined and that they indeed have a simple, rectangular polygon shape. Walls are not added to the space (they are rogue), only the player is. I've tried debugging the game and see where the body position is, but all looked fine.
https://github.com/ellmo/castellvania
Player falls down slowly, but you can control him with up / left / right arrows. Tilde button (~) shows the boudning boxes and the collision boxes are supposed to be always visible.
I need some help trying to understand what am I doing wrong.
I probably don't really udnerstand how a polygon Shape is defined and added to the space. I'm pretty sure it's not really where I draw it.
That's it. Shape coordinates are added to the body position, not substracted from it.
In your Entity.boundaries replace the line
verts << CP::Vec2.new(#shape.body.p.x - #shape[vert].x, #shape.body.p.y - #shape[vert].y)
with
verts << CP::Vec2.new(#shape.body.p.x + #shape[vert].x, #shape.body.p.y + #shape[vert].y)
and you will get correct picture. (drawing will be still broken, but bounding boxes will be correct.

Collision between wall and player controller

I'm using Farseer and XNA on on WP7. I have 2 objects in my game. The first one is a wall generated from a bitmap. The second one is a player controller - in fact it's just a circle object. This circle follows player's finger.
I need a certain behavior - probably it's very basic, but I can't figure out how to google it. It's a collision detection that just wouldn't allow the controller to come into the wall. It shouldn't bounce. It should just try to follow the finger but not enter the wall.
I know it's not hard to implement it on my own, but if I'm using a physics engine and it happens to offer such a functionality it would be a shame not to take advantage of it. :)
you need to use a BoundingBox object, and check collision VS the object (you should create a BoundingSphere wrapping it)
http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2/Collision_detection.php

How to add collision detection in a 2D car game in XNA?

Right, I'm making a 2d car racing game. So far I've got the car moving etc (with a little help of course) and was wondering how do I go about adding collision detection in XNA. I've taken a bumper part (from the whole track), and made it as a separate .png file. And I was thinking of adding a collision detection box around it (so if 'car' hits 'bumper' move back by so and so). How do I add collison detection to the bumper, and integrate it with the car? Thank you!
Try this tutorial: http://create.msdn.com/en-US/education/catalog/tutorial/collision_2d_perpixel
Source code for the tutorial is in Downloads, under the two (ugly) blue boxes.
It seems this would be a lot easier if the cars were always straight--not rotated. If they were rotated you wouldn't be able to use rectangles to help.
If you do, then you could instead have the bumper included in car.png.
Then you could use the coordinates of the car and add certain values to get the length and width of the region for the bumper.
You can then do
Rectangle bumperBoundingBox = new Rectangle
(
(int)X_COORDINATE_OF_CAR,
(int)Y_COORDINATE_OF_CAR,
(int)X_COORDINATE_OF_CAR + WIDTH_OF_BUMPER,
(int)Y_COORDINATE_OF_CAR + HEIGHT_OF_BUMPER
);
Rectangle otherCarBoundingBox = new Rectangle( \* x, y, ... *\ );
bool carIsTouchingBumper = otherCarBoundingBox.Intersects(bumperBoundingBox);
This may not be perfectly perfect, as in the parameters for rectangle might be in a different order or something like that. But once you have it, you can use carIsTouchingBumper and do stuff.
If you want the bumper to be a separate image, you could do the same thing as above, except use the coordinates of the bumper instead. Also you would have to make the bumper follow the car.

Where Should Collision Detection Code Be?

I'm writing a pong game and I have a ball class that has velocity x, y, positions and all that stuff that every frame is updated by calling #ball.update, this moves the ball forward by its x_vel and Y_vel etc. My question is Should my collision code be in the loop or in the update method of the ball class? Or should it all be in the loop and the ball should not have any control of its position?
The collision detection should not be done in you ball class because it would require the knowledge of all other objects in your game.
Imagine a shooter with many objects and think about what happens if each object would try to calculate the collision on it's own.
There should be a dedicated class that cares about collision detection. This class is notified if any of its supervised objects changed its position. Then it checks for collision and notifies all abjects (not only 2 :) that have a collsion.
Have fun... :)
A Ball class like you described is indeed a good idea. That way you can replicate it if, like in some "breakout" games, you ever want to spawn two or more balls -- or if you want to re-use that code in another game. You can also have a Paddle class with similar X,Y coordinates (or derive both Ball and Paddle from a "ScreenObject" class if they turn out to share many such similar members). Paddle can read a shared variable that gets updated by a thread/event receiving user input.
Simple collisions, such as against walls, can be done in your Ball class based on globally accessible values like the screen dimension extents. Your Update routine can simply reverse one velocity component when that particular wall is hit. You can define and set a delegate/callback in the Ball class to play a sound when the ball bounces. Similarly, Paddle's Update can check the screen size so you can't move the paddle off-screen.
Like TottiW recommended, collision between objects is best done in a parent "manager" class that owns all the objects or has access to their X,Y members or bounding boxes. This is good object-oriented design. Ball and Paddle shouldn't have access to each other's X,Y positions! ...but the manager does. It can also eliminate redundant collision checks. If object A checks to collide with object B, B shouldn't have to subsequently check for collision with A. So really all your manager class needs to do is, for each Paddle, check each Ball's position. This can be further simplified since a Paddle might only move in one direction, say horizontally, at a fixed Y position. Thus your first check can immediately eliminate any Ball.Y < Paddle.Y, for simplistic example (depending on Y's direction).
For games with lots of objects, you don't want to collison-detect every one, just the nearest ones. In that case, the "manager" becomes more of a "scene manager" which keeps linked lists of objects in both X and Y directions. As objects move past other objects, they exchange pointers in the lists, so the lists always stay sorted. That way, for any given object, we know the objects immediately to the left/right/above/below, so we need only do collision checks against those... saving lots of time and making your game run with maximum speed. Maybe you're not to this point, though!
Good luck and like the others have said, have fun with it!
For pong simple matrix operations should be sufficient. A ball class is not needed if there is only one and you use it only for holding a tuple.

Resources