detect click on shape pygame - geometry

so I have tried detecting the collision by making 2 shapes but there is some kind of mistake in it and I am wondering if there are easier ways. so this is what i tried:
cursorSurface = pygame.Surface((0,0))
cursor = pygame.draw.rect(cursorSurface, (0,0,0),(mouseX,mouseY,2,2))
mouse_mask = pygame.mask.from_surface(cursorSurface,255)
mouse_rect = cursor
if mouseAction[0] == 'move':
if mouseX > x and mouseX < xEnd and mouseY > y and mouseY < yEnd:
topTriangle = selectedSquare.subsurface(x+4,y+4,xEnd-(x+5),int((yEnd-(y+5))*0.25))
bottomTriangle = selectedSquare.subsurface(x+4,y+4+int((yEnd-(y+5))*0.75),xEnd-(x+5),int((yEnd-(y+5))*0.25))
leftTriangle = selectedSquare.subsurface(x+4,y+4,int((xEnd-(x+5))*0.25),yEnd-(y+5))
rightTriangle = selectedSquare.subsurface(x+4+int((xEnd-(x+5))*0.75),y+4,int((xEnd-(x+5))*0.25),yEnd-(y+5))
collisionTop_mask = pygame.mask.from_surface(topTriangle,255)
collisionTop_rect = topTriangle.get_rect()
collisionTop_rect.topleft = (0,0)
pygame.draw.rect(selectedSquare, colorDark,(x+5+int((xEnd-(x+5))*0.25),y+5+int((yEnd-(y+5))*0.25),int((xEnd-(x+5))*0.75)-int((xEnd-(x+5))*0.25)-2,int((yEnd-(y+5))*0.75)-int((yEnd-(y+5))*0.25)-2))
pygame.draw.polygon(topTriangle, colorDark, ((1,0), (topTriangle.get_width()-2,0), (int((xEnd-(x+7))/2),(int((yEnd-(y+7))/2)-1))))
pygame.draw.polygon(leftTriangle, colorDark, ((0,1), (0,leftTriangle.get_height()-2), (int((xEnd-(x+7))/2)-1,(int((yEnd-(y+7))/2)))))
pygame.draw.polygon(bottomTriangle, colorDark, ((1,yEnd-(y+6)-int((yEnd-(y+5))*0.75)), (bottomTriangle.get_width()-2,yEnd-(y+6)-int((yEnd-(y+5))*0.75)), (int((xEnd-(x+7))/2),(int((yEnd-(y+7))/2)+1-(yEnd-(y+5))*0.75))))
pygame.draw.polygon(rightTriangle, colorDark, ((xEnd-(x+6)-int((xEnd-(x+5))*0.75),1), (xEnd-(x+6)-int((xEnd-(x+5))*0.75),rightTriangle.get_height()-2), (int((xEnd-(x+7))/2)+1-int((xEnd-(x+5))*0.75),(int((yEnd-(y+7))/2)))))
screen.blit(selectedSquare, (0,0))
if collisionTop_mask.overlap(mouse_mask,(mouse_rect.left-collisionTop_rect.left,mouse_rect.top-collisionTop_rect.top)) != None:
print('detect')
but i have also seen this methode:
if topTriangle.get_rect().collidepoint(pygame.mouse.get_pos()):
the problem is that this only detects squares and I need to detect a triangle. could someone pleas help me with this?

You could do something like this I think (didn't test):
posx, posy = pygame.mouse.get_pos()
posx -= XCoordinateOfTopTriangleOnScreen
posy -= YCoordinateOfTopTriangleOnScreen
if topTriangle.get_at((posx,posy)) == colorDark:
print('detect')

Related

Navigation2d use with move_and_slide in Godot

Rewritten and Edited for clarity. Assume that I have a 2d platformer like the following example:
https://github.com/godotengine/godot-demo-projects/blob/master/2d/kinematic_character/player.gd
Now... Say I have the player location (vector2) and the enemy location (vector2). The enemy movement works just like the player in the above example. I use get_simple_path to build an array of pre-existing points that lead from the enemy location to the player location. How do I use that array with move_and_slide to get the enemy from point A to point B?
You could try the following:
const GRAVITY = 1000
const SLOPE_SLIDE_STOP = false
var speed = 250
var velocity = Vector2()
var points = [Vector2(100, 200), Vector2(200, 400), Vector2(400, 800)]
var current_point = null
func _physics_process(delta):
if points and current_point is null:
current_point = points.pop_front()
if current_point:
if current_point.distance_to(position) > 0:
target_direction = (position - current_point).normalized()
velocity.y += delta * GRAVITY
velocity = lerp(velocity, target_speed, 0.1)
velocity = move_and_slide(velocity, target_direction, SLOPE_SLIDE_STOP)
else:
current_point = null

Unity C# Rigidbody 2D Gravity Scale not working properly

I have it set where an object will fall if the player is within two positions, and it will rise again. if the player is not between the position the gravity is set to zero. It does fall and rise, but if i step out of the two positions, the object keeps on rising and doesn't stop. I am trying to recreate the rock boulder in mario that falls if the player is near. (Mass =1, Linear Drag = 0, Fixed Angle = Checked, Kinematic = NOT Checked, Interpolate = none, Sleeping Mode = Start Awake, Collision Detection = Continuous)
//the positions where gravity is set to zero
if (rockk.transform.position.y > 4.0 && player.transform.position.x < 59)
{
rockk.rigidbody2D.gravityScale = 0f;
a = 0;
}
if (rockk.transform.position.y > 4.0 && player.transform.position.x > 64)
{
rockk.rigidbody2D.gravityScale = 0f;
a = 0;
}
//go up , -1.2 is the ground
if (rockk.transform.position.y < -1.2 )
{
rockk.rigidbody2D.gravityScale = -1f;
}
//go down
if ( rockk.transform.position.y > 4.0 && player.transform.position.x >59 && player.transform.position.x < 64)
{
rockk.rigidbody2D.gravityScale = 1f;
a = 1;
}
When you reset the gravity scale to 0 your Rigidbody still has velocity. With no drag, it will never stop. Try something like:
rockk.rigidbody2D.gravityScale = 0f;
rockk.rigidbody2D.velocity = Vector2.zero;
Also, using the rigidbody2D property is deprecated in newer versions of Unity. You might want to try something like:
Rigidbody2D body = GetComponet<Rigidbody2D>();
body.gravityScale = 0f;
body.velocity = Vector2.zero;

Translate mouse input to gamepad stick

I'm trying to emulate a gamepad thumbstick by converting mouse input but I'm having a lot of trouble making it smooth, all I can get is short jagged movements. Does anyone know how I can smooth this out without creating a noticeable amount of lag? I tried copying some values out of the PCSX2 lilypad plugin without much success.
This code gets the current mouse position, subtracts it from the last mouse position and calculates the force which should be applied to the thumbstick. The force gets applied up to the max and min values for the thumbstick which are 32767 and -32767 respectively.
I think there may be a couple of problems with this code - if I process it constantly without pausing sometimes it thinks the mouse hasn't moved and resets it back to 0 resetting all movement, obviously sleeping so it has more time to read mouse movement causes lag which isn't really an option here. What I need is a way of calculating a smooth amount of force to apply without resetting movement or adding lag to input.
POINT cursorPos{ 0, 0 };
POINT cursorPos2{ 0, 0 };
GetCursorPos(&cursorPos);
cursorPos2 = cursorPos;
while(true){
GetCursorPos(&cursorPos);
int dx = cursorPos.x - cursorPos2.x;
int dy = cursorPos.y - cursorPos2.y;
if (dx != 0)
{
unsigned short rightX = axisInput.RightX;
int force = (int)((SENSITIVITY*(255 * (__int64)abs(dx))) + BASE_SENSITIVITY);
if (dx < 0)
{
if ((rightX + force) > 32767)
rightX = 32767;
else
rightX += force;
}
else
{
if ((rightX - force) < -32767)
rightX = -32767;
else
rightX -= force;
}
axisInput.RightX = rightX;
}
else
axisInput.RightX = 0;
if (dy != 0)
{
unsigned short rightY = axisInput.RightY;
int force = (int)((SENSITIVITY*(255 * (__int64)abs(dy))) + BASE_SENSITIVITY);
if (dy < 0)
{
if ((rightY - force) < -32767)
rightY = -32767;
else
rightY -= force;
}
else
{
if ((rightY + force) > 32767)
rightY = 32767;
else
rightY += force;
}
axisInput.RightY = rightY;
}
else
axisInput.RightY = 0;
...
cursorPos2 = cursorPos;
}
Thanks.

How do I create an object boundary in Three.js / WebGL?

I'm new to graphics and was wondering how I would go about creating an object boundary for my game.
Right now I have an object following my mouse. This works fine for my boundary when my ship has a rotation.z of 0. However, when I rotate the ship, the boundary has odd behavior.
I tried getting the world position of the ship and simply checking the x and y boundaries. However, the world position seems to give me a different x and y when I rotate. How can I
go about creating a boundary that works for all the rotations of my ship.
Here's my game(collisions turned off for purpose of help): http://www.cis.gvsu.edu/~chaua/CS371/WebGLProject/home.html
The behavior I'd like occurs when you don't rotate the ship.
Rotate the ship with left/right mouse.
Relevant code snippets(in render loop):
if(mouseLeftDown)
ship.rotation.z += leanSpeed;
if(mouseRightDown)
ship.rotation.z += -leanSpeed;
....
....
targetX = ((lastMouseX / window.innerWidth) * 2 - 1) * 90;
targetY = -((lastMouseY / window.innerHeight) * 2 - 1) * 60;
var worldPosition = (new THREE.Vector3()).getPositionFromMatrix(ship.matrixWorld);
if(worldPosition.x + targetX <= (randSpawnWidth/2)-500 && worldPosition.x + targetX >= -(randSpawnWidth/2)+500)
ship.translateX(targetX);
if(worldPosition.y + targetY <= (randSpawnHeight/2)-500 && worldPosition.y + targetY >= -(randSpawnHeight/2)+500)
ship.translateY(targetY);
I would appreciate any help. Thanks!

Any way to stretch/collapse canvas grid in fabric js?

I am thinking of stretching/collapsing the canvas grid using input fields. I tried the grid.js also. But this is not for fabric js, though i tried hard.
Is it possible to stretch/collapse the canvas grid by user input?
I have come up with a solution using some other guys solution those I did find in jsfiddle but I cannot find that reference link right now. I just have customized that solution to work with my code. Thanks to that guy. Here is my solution -
function draw_grid(grid_size) {
grid_size || (grid_size = 25);
currentCanvasWidth = canvas.getWidth();
currentcanvasHeight = canvas.getHeight();
// Drawing vertical lines
var x;
for (x = 0; x <= currentCanvasWidth; x += grid_size) {
this.grid_context.moveTo(x + 0.5, 0);
this.grid_context.lineTo(x + 0.5, currentCanvasHeight);
}
// Drawing horizontal lines
var y;
for (y = 0; y <= currentCanvasHeight; y += grid_size) {
this.grid_context.moveTo(0, y + 0.5);
this.grid_context.lineTo(currentCanvasWidth, y + 0.5);
}
grid_size = grid_size;
this.grid_context.strokeStyle = "black";
this.grid_context.stroke();
}
I hope this will someone someday.

Resources