Lua Mouse Drag Quaternion - lua

I am looking for a bit of help with this lua script I am making. I want to use Quaternion's to rotate an object based on your mouse movement/drag. I am using this library for my Quaternion https://github.com/topameng/CsToLua/ . I have got the object to move but all the rotations are messed up, here is a link to a video of what I have so far http://img.bcdojrp.net/videos/uploads/2021-03-17%2003-58-07_Trim.mp4 I have spent hours looking this over and can't find what to change... any help is appreciated, thanks!
function GetCursor()
local sx, sy = GetActiveScreenResolution()
local cx, cy = GetNuiCursorPosition()
local cx, cy = (cx / sx) + 0.008, (cy / sy) + 0.027
return cx, cy
end
if dragging then
-- this is the screen position
local intx, inty = GetCursor()
local deltaMove = {
['x'] = intx-previousMousePosition.x,
['y'] = inty-previousMousePosition.y
}
local deltaRotationQuaternion = Quaternion.Euler(deltaMove.y * 40, deltaMove.x * 40, 0)
local x,y,z,w = GetEntityQuaternion(curObject)
local quatNew = deltaRotationQuaternion.__mul(Quaternion.New(x,y,z,w), deltaRotationQuaternion)
local valX, valY, valZ, valW = quatNew.x, quatNew.y, quatNew.z, quatNew.w
SetEntityQuaternion(curObject, valX, valY, valZ, valW)
--cube.quaternion.multiplyQuaternions(deltaRotationQuaternion, cube.quaternion);
previousMousePosition.x = intx
previousMousePosition.y = inty
else
previousMousePosition = {
['x'] = 0,
['y'] = 0
}
end

Related

How to detect object clicked in WebGL?

Firstly, I am not using 3Js in my Orbits app because I encountered a number of limitations including, but not limited to, issues with texture resolution and my requirement for complex lighting equations but I would like to implement something like 3Js' raycaster to allow me to detect the object clicked by the user.
I'm new to WebGL, but an "old hand" in software development so I'm looking for some hints about where to start.
The approach is as follows:
You generate your scene twice, once normally which is displayed and the second, with the objects uniquely coloured but not displayed. Then you use gl.readPixels from the second scene using the position on the first and decode the colour to identify the object.
Now I have to implement it myself.
Picking spheres
When picking spheres, or objects that are separated (not one inside another) you can use a simple distance from ray to very quickly get the closest object.
Example
The function returns a function that does the calculation. As it is only the closest you are interested in the distances can remain as squares. The distance from the camera is held as a unit distance along the ray.
function distanceFromRay() {
var dSqr, ox, oy, oz, vx, vy, vz;
function distanceSqr(px, py, pz) {
const ax = px - ox, ay = py - oy, az = pz - oz;
const u = (ax * vx + ay * vy + az * vz) / dSqr;
distanceSqr.unit = u;
if (u > 0) { // is past origin
const bx = ox + vx * u - px, by = oy + vy * u - py, bz = oz + vz * u - pz;
return bx * bx + by * by + bz * bz; // dist sqr to closest point on ray
}
return Infinity;
}
distanceSqr.unit = 0;
distanceSqr.setRay(x, y, z, xx, yy, zz) { // ray from origin x, y,z,
// infinite length along xx,yy,zz
(ox = x, oy = y, oz = z);
(vx = xx, vy = yy, vz = zz);
dSqr = vx * vx + vy * vy + vz * vz;
}
return distanceSqr;
}
Usage
There is a one time setup call;
// setup
const distToRay = distanceFromRay();
At the start of a frame that requires a pick, calculate the pick ray and set it. Also set the min distance from ray and eye.
// at start of frame set pick ray
distToRay.setRay(eye.x, eye.y, eye.z, pointer.ray.x, pointer.ray.y, pointer.ray.y);
var minDist = maxObjRadius * maxObjRadius;
var nearestObj = undefined;
var eyeDist = Infinity;
Then for each pickable object get the distance by passing the objects center and comparing it to any previous (in frame) found distance, objects radius, and distance from eye.
// per object
const dis = distToRay(obj.pos.x, obj.pos.y, obj.pos.z);
if (dis < obj.radius && dis < minDist && distToRay.unit > 0 && distToRay.unit < eyeDist ) {
minDist = dis;
eyeDist = distToRay.unit;
nearestObj = obj;
}
At the end of the frame if nearestObj is not undefined it will hold a reference to the picked object.
// end of frame
if (nearestObj) {
// you have the closest object
}

How to "rotate" an ellipse?

Using this:
local W, H = 100, 50
function love.draw()
love.graphics.translate(love.graphics.getWidth()/2,love.graphics.getHeight()/2)
for i = 1, 360 do
local I = math.rad(i)
local x,y = math.cos(I)*W, math.sin(I)*H
love.graphics.line(0, 0, x, y)
end
end
I can connect a line with the center of an ellipse (with length W and height H) and the edge. How do you 'rotate' the ellipse around it's center, with a parameter R? I know you can sort of do it with love.graphics.ellipse and love.graphics.rotate but is there any way I can get the coordinates of the points on a rotated ellipse?
This is a Trigonometry problem, here is how the basic 2D rotation work. Imagine a point located at (x,y). If you want to rotate that point around the origin(in your case 0,0) by the angle θ, the coordinates of the new point would be located at (x1,y1) by using the following transformation
x1 = xcosθ − ysinθ
y1 = ycosθ + xsinθ
In your example, I added a new ellipse after rotations
function love.draw()
love.graphics.translate(love.graphics.getWidth()/2,love.graphics.getHeight()/2)
for i = 1, 360, 5 do
local I = math.rad(i)
local x,y = math.cos(I)*W, math.sin(I)*H
love.graphics.setColor(0xff, 0, 0) -- red
love.graphics.line(0, 0, x, y)
end
-- rotate by angle r = 90 degree
local r = math.rad(90)
for i = 1, 360, 5 do
local I = math.rad(i)
-- original coordinates
local x = math.cos(I) * W
local y = math.sin(I) * H
-- transform coordinates
local x1 = x * math.cos(r) - y * math.sin(r)
local y1 = y * math.cos(r) + x * math.sin(r)
love.graphics.setColor(0, 0, 0xff) -- blue
love.graphics.line(0, 0, x1, y1)
end
end

Shoot from same point of a rotating image

I am trying to make a basic prototype for a top down shooter in LOVE but I am having some issues getting the bullet to shoot from the same point of the image when it is rotated. This is the code I have so far which works fine as the bullet shoots from the center of the image but I want it to shoot from the right hand side of the sprite where the gun is:
function love.load()
love.graphics.setBackgroundColor(54, 172, 248)
player = love.graphics.newImage('/assets/images/player.png')
playerX = 300
playerY = 300
playerSpeed = 200
bullets = { }
bulletSpeed = 250
cursor = love.mouse.newCursor('assets/images/crosshair.png', 24, 24);
love.mouse.setCursor(cursor);
end
function love.update(dt)
-- Get mouse position to rotate player
local mouseX, mouseY = love.mouse.getPosition()
playerRotation = math.atan2(mouseY - playerY, mouseX - playerX);
-- Keyboard input to move the player
if love.keyboard.isDown('s') then
playerY = playerY + playerSpeed * dt
elseif love.keyboard.isDown('w') then
playerY = playerY - playerSpeed * dt
end
if love.keyboard.isDown('a') then
playerX = playerX - playerSpeed * dt
elseif love.keyboard.isDown('d') then
playerX = playerX + playerSpeed * dt
end
-- update all bullets position
for i, v in ipairs(bullets) do
v.x = v.x + (v.dx * dt)
v.y = v.y + (v.dy * dt)
end
end
function love.draw(dt)
-- Draw player
love.graphics.draw(player, playerX, playerY, playerRotation, 0.5, 0.5, player:getWidth() / 2, player:getHeight() / 2);
-- Draw all bullets
love.graphics.setColor(128, 128, 128)
for i, v in ipairs(bullets) do
love.graphics.circle("fill", v.x, v.y, 3);
end
end
function love.mousepressed(x, y, button)
if button == 1 then
local startX = playerX;
local startY = playerY;
local mouseX = x
local mouseY = y
local angle = math.atan2((mouseY - startY), (mouseX - startX))
local bulletDx = bulletSpeed * math.cos(angle)
local bulletDy = bulletSpeed * math.sin(angle)
table.insert(bullets, {x = startX , y = startY, dx = bulletDx, dy = bulletDy})
end
end
How do I draw it from the same point of the sprite as it is rotated?
If I got you correctly you have an image representing your player. (playerX, playerY) is the position of your player where the center of your image is.
Atm you start your bullets at the image center.
So the starting point of your bullets is invariant to player rotation.
If you move the gun away from the player center it rotates around that point where the radius is the distance of the gun position to the player position.
For distance 1 you can refer to the unit circle
So simply multiply that with your radius so:
local gunXRotated = playerX + r * math.cos(t)
local gunYRotated = playerY + r * math.sin(t)
where t is your rotation angle and r is the Euclidean distance between player center and gun muzzle.
Then simply use the new gun coordinates as the origin of your bullets.

Corona Vertices of a Hexagon

Ok, so I'm trying to create hexagons for my game. The first option I had is to have several images of hexagon, but I'm having problems with clickable area since these images are positioned side-by-side.
So i guess my only option is to create objects using polygons. Here is a code from corona sdk's website:
local halfW = display.contentWidth * 0.5
local halfH = display.contentHeight * 0.5
local vertices = { 0,-110, 27,-35, 105,-35, 43,16, 65,90, 0,45, -65,90, -43,15, -105,-35, -27,-35, }
local o = display.newPolygon( halfW, halfH, vertices )
o.fill = { type="image", filename="mountains.png" }
o.strokeWidth = 10
o:setStrokeColor( 1, 0, 0 )
That code is for creating a star. But I don't know how to create a hexagon using vertices.
Try this to create the vertices array:
local R = 45
local N = 6
local vertices = {}
local i = 0
for t = 0, 2*math.pi, 2*math.pi/N do
i=i+1; vertices[i]= R*math.cos(t)
i=i+1; vertices[i]= R*math.sin(t)
end
And this to draw the hexagon:
local halfW = display.contentWidth * 0.5
local halfH = display.contentHeight * 0.5
local hexagon = display.newPolygon( halfW, halfH, vertices )
hexagon.fill = { type="image", filename="mountains.png" }
hexagon.strokeWidth = 10
hexagon:setStrokeColor( 1, 0, 0 )
I chose R=45 to produce a polygon of the same size of your star.
You could always use a graphics.newMask() to apply a mask to each image hex that would make the outside area not touchable.

AnAl - weird rotating

I am totally new to programming. I've tested how to script an other things like that... but I tried to use animations in my "game". I used the "AnAl" library. All worked good. But then where I liked to use "moving" (or how it's called ;P) the animations doesn't worked and the character rotated. I don't know what I need to do...
And I used the Lua language, btw.
require ("AnAl")
function love.load()
-- Shortcuts
lg = love.graphics
lkid = love.keyboard.isDown
local img = lg.newImage ("img.png")
anim = newAnimation(img, 100, 100, 0.1,5,0)
image = {
x = 250,
y = 150,
rotation = math.rad (0),
moveSpeed = 200
}
end
function love.draw()
anim:draw(figur, image.x, image.y, image.rotaion, 0.5, 0.5)
end
function love.update(dt)
if lkid("w") then image.y = image.y - image.moveSpeed * dt end
if lkid("s") then image.y = image.y + image.moveSpeed * dt end
if lkid("a") then image.x = image.x - image.moveSpeed * dt end
if lkid("d") then image.x = image.x + image.moveSpeed * dt end
anim:update(dt)
end
I have no idea what figur refers to in your code.
The arguments to anim:draw should be x, y, rotation, scalex, scaley. Since you have added figur before the arguments for some reason, you are setting the rotation to be the y position.
anim:draw(image.x, image.y, image.rotation, 0.5, 0.5)

Resources