Page 1 of 1

understanding velocity

PostPosted: Thu Mar 20, 2014 11:55 pm
by tazinator
  Code:
bot:setVel(pt vel)

Sets the item's velocity.

As with other functions that take a point as an input, you can also specify the x and y components as numeric arguments.

Parameters
vel A point representing item's velocity.

Confusing to me. This point can't just be (x, y) because the bot will not work if that was the case. you can't just getPos and put that value in. I don't know what goes on with velocity...


working code: why is my ship faster?
  Code:
function onTick()
    table.clear(items)
    targetShip = findClosest(findAllObjects(items, ObjType.Ship))
    if(targetShip ~= nil) then
    local targetShiploc = targetShip:getPos()
    local angle = targetShip:getAngle()
    local playerVel = targetShip:getVel()
    if(bot:canSeePoint(targetShiploc)) then
        bot:setVel(playerVel)
        --experimental velocity code above. Why is my ship faster?
        --you can also do bot:setVel(2*playerVel) multiplying speed
        --TAZ
        --the following line is a pt, right?
        --bot:setVel(targetShiploc)
        bot:setAngle(angle)
        bot:fireWeapon(Weapon.Phaser)
        else
        bot:setAngle(angle)
        end
    end
end

 

Re: understanding velocity

PostPosted: Fri Mar 21, 2014 1:42 am
by watusimoto
I don't know the answer to your question, but you can see what velocity is by printing it using either logprint or, I think, print. Printed output is visible on the game console (ctrl-/).

I don't have the code in front of me, but I do believe that the velocity should sum to 1 (i.e. sqrt(x^2 + y^2) = 1), and that 1 represents full speed.

Re: understanding velocity

PostPosted: Fri Mar 21, 2014 2:06 am
by tazinator
I'm not so good at math anymore..

But what you said sounds wrong.. sqrt is square root?
sqrt(x^2 + y^2) = 1


if x = 10 and y = 5
then x^2 + y^2 = 10^2 + 5^2 = 100 + 25 = 125
sqrt of 125 ≠ 1
if ^'s multiplication, not squaring
10x2 + 5x2 = 20 + 10 = 30
sqrt of 30 ≠ 1


I probably won't understand it, but maybe I will.
p.s. How do I get the table to print in console? it only shows me if something fails :D

Re: understanding velocity

PostPosted: Fri Mar 21, 2014 7:53 am
by bobdaduck
You pass in a point value.

The X value of the point is the ships X velocity.
The Y value of the point is the ships Y velocity.

local vel = blah:getVel()
blahItem:setVel(vel)

blahItem:setVel(point.new(50, 50))

local blahItemVel = blah:getVel()
blahItem:setVel(blahItemVel.x + 50, blahItemVel.y + 50)
logprint("hello world! Your vel is " .. blahItem:getVel())

Re: understanding velocity

PostPosted: Sat Mar 22, 2014 11:59 am
by watusimoto
What I meant was that I thought the total length of the moves added up to 1. That is, if you have c^2 = a^2 + b^2, and c was the length of your move, and a and b are x and y respectively, the total length would be 1.

I looked at the code just now, and it's not obvious that I am correct. I may be, but I may not be.

I thought (and still sort of think) that if you set your move.x to 100 and your move.y to 100, they will both get reduced to sqrt(2)/2, ~.707. I don't think you'll go 100x normal speed.

Re: understanding velocity

PostPosted: Tue Mar 25, 2014 1:27 am
by tazinator
I still have no idea what's going on in velocity, then. :D
local targetShiploc
bot:setVel(point.new(targetShiploc))
bot:setVel(1.5(playerVel))

playerVel is defined earlier as
targetShip:getVel()

The first is funky, because it is missing speed.
The second is velocity working right.

anyway, I'd like to share that

MoveObject::setVel ( point vel )

mislead me. It makes me think I can just do
  Code:
bot:setVel(x, y)


wrong.

BobdaDuck, I did a logprint, and yep, getVel printing velocity gives you an (x, y) value. But setting velocity to an (x, y) value is seen to simply crash.

Re: understanding velocity

PostPosted: Tue Mar 25, 2014 1:42 am
by tazinator
also bobdaduck, using your velocity example crashed bitfighter

  Code:
   local botVel = bot:getVel()
        bot:setVel(botVel.x + 50, botVel.y + 50)

Re: understanding velocity

PostPosted: Tue Mar 25, 2014 8:14 am
by bobdaduck
Oops, that's because you have to use point.new() any time you're using a point.

local vel = blah:getVel()
blahItem:setVel(vel) --this works because getVel() gets as a point value

blahItem:setVel(point.new(50, 50)) --Passing in a point like this requires point.new()