Ball-Line Segment Collision on End-Point of Line - python

So I have a program where a ball subject to gravity bounces off of lines created by a user with mouse clicks. These lines are normally sloped. My collision bounces work perfectly EXCEPT in the case where ball does approximately this:
->O ------
My code works by finding the normal vector of the line such that the scalar product of the incident velocity vec of the ball and the normal of the line is negative (this means the vector have opposite directions).
Then I decompose the velocity into terms parallel and perpendicular to the normal,
and the reverse the direction of the parallel component.
During the edge case described above ball moves basically along the line. How can I account for this? Any advice?>

Possible Solutions:
Instead of using a single 1D 'line', you could construct a 2D rectangle (that is as this as you want/need it to be) --- composed of 4 separate 'lines'. I.e. you can have collisions with any of the 4 faces of the rectangle object. Would that work?
Do some sort of corner collision -- if the ball is 'hits' the start or end of a line, have it bounce off appropriately. I think the way this would be done is as follows:
i. Collision occurs if the corner falls within the radius of the ball.
ii. Define a line between the corner and the center of the ball.
iii. Reverse the component of the ball's velocity along this line.

Related

Python-Open CV- how do i change world space origin point

I am building a game (like this https://www.youtube.com/watch?v=ylkmOzsDv1g) that tracks a ball using 2 cameras, I then use those 2 cameras to find the 3D coordinates of the ball. When the ball hits the wall the code detects it and changes the projector image.
I am using some code that takes images and finds the intrinsic and distortion matrix. I also have found the rotation and translation matrix from the 2 cameras that I have. This gives me code that takes 2x 2D coordinates from the cameras that I make into 3D coordinates with the world space origin on camera 1.
The problem is that I want to change the world coordinates to make the X,Y at the bottom left of the wall (or just anywhere on the wall). The Z-axis can be into or out of the wall but at 0 on the wall. This will allow me to program the game to just need the X,Y coordinates to run the game.

Is there a way to get the co-ordinates of the point of collision?

I am making a game in which the change in velocity of a ball depends on the point at which it collides with an object. I looked up the documentation but was unable to find something to do the job.
Is there any way to get the coordinates of the point of collision in Pygame?
Yes, work out the difference in the x and y co-ordinates of your ball and object. Then use mask.overlap from pygame to return a tuple of the point of collision. An example would be:
def collide(obj1, obj2):
offset_x = obj2.x - obj1.x
offset_y = obj2.y - obj1.y
return obj1.mask.overlap(obj2.mask, (offset_x, offset_y))
You can find the docs for 'mask.overlap()' here.
I support the answer given by Abishake Srithar. This is not meant to displace that answer, just add information to it.
A slight caution about the result of this mask.overlap() call. It does not give you the center of where the point of contact might be.
As you can see in the description in the docs here, the overlap is searched for by proceeding top to bottom in columns moving left to right. Which means that overlap() returns the leftmost, upper (sort of) point of the overlap.
So if your update/move of the objects causes the objects to overlap over an area (which is likely), the point returned by overlap() is the leftmost, upper point of that overlap. It is not the center of the area, or the point where the two objects might have first touched if they were not overlapping.
An example is if this were two spheres colliding, it will give you the edge of the overlap and not actually the centerpoint of the collision. This could change your angles of reflection.
This difference can affect the collision physics.
It is likely good enough for most games but I thought it worth pointing out.

Walls logic in Pygame

I'm making a game with Pygame, and now I stuck on how to process collision between player and wall. This is 2D RPG with cells, where some of them are walls. You look on world from top, like in Pacman.
So, I know that i can get list of collisions by pygame.spritecollide() and it will return me list of objects that player collides. I can get "collide rectangle" by player.rect.clip(wall.rect), but how I can get player back from the wall?
So, I had many ideas. The first was push player back in opposite direction, but if player goes, as example, both right and bottom directions and collide with vertical wall right of itself, player stucks, because it is needed to push only left, but not up.
The second idea was implement diagonally moving like one left and one bottom. But in this way we don't now, how move first: left or bottom, and order becomes the most important factor.
So, I don't know what algorithm I should use.
If you know the location of the centre of the cell and the location of the player you can calculate the x distance and the y distance from the wall at that point in time. Would it be possible at that point to take the absolute value of each distance and then take the largest value as the direction to push the player in.
e.g. The player collides with the right of the wall so the distance from the centre of the wall in the y direction should be less than the distance in x.
Therefore you know that the player collided with the left or the right of the wall and not the top, this means the push should be to the right or the left.
If the player's movement is stored as in the form [x, y] then knowing whether to push left or right isn't important since flipping the direction of movement in the x axis gives the correct result.
The push should therefore be in the x direction in this example
e.g. player.vel_x = -player.vel_x.
This would leave the movement in the y axis unchanged so hopefully wouldn't result in the problem you mentioned.
Does that help?

How to determine the distance in pixels between a circle and various obstacles

So I have a circle created in pygame as my main character, and I'm looking to find the shortest distance between its sides and the various surrounding obstacles. You can see what I mean in the picture. I have a left quadrant and a right quadrant, and I want to see what exists in these quadrants. Whatever is the closest obstacle pixel wise to the circle, that pixel count becomes the left and right values (ie. if in the right quadrant the closest obstacle was 40 pixels away, right = 40). I also have a front value, that will be scanning for things directly in front of the circle.
I've seen things for looking for collision with a circle (creating a circular field around the object as a whole) and I've also seen stuff that uses Pythagorean theorem to look for distances, but I'm not sure how to tackle it in a more "spotlight" scope, if that makes sense.
Any suggestions on how to go about this would be much appreciated! The overall goal is for the circle to move automatically around these obstacles by avoiding them, hence why I want it to scan in various areas to determine how to move about the space.
The entire space is enclosed with a wall, and all the obstacles are randomly placed squares within the wall.
The distance to a circle is the same as the distance to the center minus the radius.
Now, the distance from a rectangle to a point is found by considering the nine regions defined by the supporting lines of the sides.
Depending on the region, the shortest distance is axis-aligned (from a side to the point) or oblique (from a corner to the point) and the formulas are easy.
The discussion is even simpler for a rectangular hole or inner angle.

Turtle module - Detecting collisions with polygons

I'm doing a small experiment with turtle, basically a small game where you can control a turtle... I have trajectory calculations already, so I can control it pretty decently!
However, I'd like to define obstacles, mostly polygons defined by a set of points.
Those would trigger collisions if the turtle hurt them (elastic collisions).
My question is...
How can I detect if I'm on the border of a polygon? Say I run straight into the middle of the hypotenuse of a triangle, how can I know this is actually a line which I cannot go through, at the right moment, in order to trigger the collision code?
Thanks!
Your polygons are defined by line segments. The turtle move is also a line segment, so you merely need to check for intersections between the turtle line segment and the list of polygon line segments.
You can probably do this for a hundred or a thousand line segments fairly quickly. Beyond that you'll need to look for smarter algorithms. eg. the Bentley-Ottmann algorithm.

Categories