Jump to content

How to properly get block center position?


Treexter

Recommended Posts

I was trying some stuff and I cannot make this properly.

Maybe I don't good at math or something...

 

Sooo, for example, I want to obtain the center position of a block for like... this callback of a Sector

onBlockDamaged(objectIndex, blockIndex, inflictorId, damage, damageType)

 

So I've tried the following (Spawning the money piece to check if the coordinates is correct)

function onBlockDamaged(objectIndex, blockIndex, inflictorId, damage, damageType) 
local entity = Sector():getEntity(objectIndex)
local plan = entity:getPlan()
local block = plan:getBlock(blockIndex)

Sector():dropMoney(block.box.position, nil, nil, 1)
end

 

But in fact the money spawns were miles away from damaged block... So I've tried this

function onBlockDamaged(objectIndex, blockIndex, inflictorId, damage, damageType) 
local entity = Sector():getEntity(objectIndex)
local plan = entity:getPlan()
local block = plan:getBlock(blockIndex)

local entityPos = entity.position.position
local drop = block.box.position
drop.x = drop.x + entityPos.x
drop.y = drop.y + entityPos.y
drop.z = drop.z + entityPos.z

Sector():dropMoney(drop, nil, nil, 1)
end

 

That thing was a bit closer to the truth, but still not accurate...

 

So, what is the correct way to obtain block center?

Link to comment
Share on other sites

Block cordinates is seperate from sector cordinates, you want to use entity pos like you did in you last example.

Adjust the vectors from the entities position to hwere you want it.

 

heres a snippet of code I use to spawn another entity behind and below the original entity. Perhaps you can use this to get what you need.

 

  
  local plan = entity:getPlan()
  local position = ship.orientation
  local sphere = ship:getBoundingSphere()
  local behindAndDown = ((ship.look * (-1)) + (ship.up * (-1))) / 2
  position.translation = sphere.center + behindAndDown  * (sphere.radius + plan.radius + 50);

 

Link to comment
Share on other sites

Ahaa...

 

To clarify what i'm trying to do... let's say there is a huge entity, and I damage some block on the side, i want to spawn something at the exact block location.

 

So as far as i understood my last snippet was missing rotation element.

The correct way should be...

1) obtain block center vector

2) Multiply it by entity rotation vector

3) add it to the entity center

 

But what math functions I should use for this? I think it's "mul" and "add"...

Oh... need to experiment...

Link to comment
Share on other sites

Okay, tried this... still rotation is incorrect...

 

       

local entity = Sector():getEntity(objectIndex)
local plan = entity:getPlan()
local block = plan:getBlock(blockIndex)
local material = block.material.name

local entityPos = entity.translationf
local drop = block.box.center * entity.look
drop = entityPos + drop

        Sector():dropMoney(drop, nil, nil, 1)

 

I can see how money drop position gets affected by block position but rotation is incorrect so it drops somewhere left or right, up or down of the entity.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...