Mode7 Screen Coordinate in Perspective

Hello,

I play with Mode7Driver sample, and I would like insert sprite on the road.

The documentation of playdate.graphics.image:drawSampled explain the transformation :
Draws the image as if it’s mapped onto a tilted plane, transforming the target coordinates to image coordinates using an affine transform:

x' = dxx * x + dyx * y + dx
y' = dxy * x + dyy * y + dy
[...]

For adding some trees along the road, I defined trees postions with 'world' coordinates, and calculate screen coordinates x' and y' with function:
x' = dxx * x + dyx * y + dx and y' = dxy * x + dyy * y + dy

I have this result :
Mode7Driver

Trees are on my screen ! Yeah !

But not on the side of the road: Perspective is no calculated, because z (maybe tiltAngle) are not use in this function ...

Anybody can help me to resolve my perspective problem with a magical function ?
Can I find, somewhere, drawSampled implementation to see how is work ?

Next, with good coordinates in perspective, I need also apply a scale function base on 'z' value.
But I will advance step by step

I will try to understand a very good documentation found on the site below, but mathematics is hard for me :laughing:

https://www.coranac.com/tonc/text/mode7.htm
https://www.coranac.com/tonc/text/mode7ex.htm

Thank you for reading me

Hm, the provided formula doesn't incorporate the z and tiltAngle arguments, so implementing it yourself won't give the desired result. I also don't see an argument for rotating about the Z axis, so I don't think drawSampled() alone is sufficient for this use case. Are you using it in conjunction with something like drawRotated()?

Thank for your reply.

Today I have add this code in Mode7Driver example :

for i, t in ipairs(trees) do
    local deltaX = t.wx - x
    local deltaY = t.wy - y

    local cp = math.cos(angle)
    local sp = math.sin(angle)
    local dxx = cp / fieldscale
    local dyx = sp / fieldscale
    local dxy = -sp / fieldscale
    local dyy = cp / fieldscale
    local dx = t.wx/trackwidth
    local dy = t.wy/trackheight

    local posX = dxx * deltaX + dyx * deltaY + dx
    local posY = dxy * deltaX + dyy * deltaY + dy

    posX += 100
    posY += 90
    t:moveTo(posX, posY)

end

Where t.wx and t.wy is 'world' position of a tree

I have try to change some values or add some modifications to initial function, but is not a scientific approch xD
I need to understand how is work :stuck_out_tongue:

drawsampled documentation is incomplet, missing perspective effect.

Well, I will try to implement my own mode7 renderer. :cry:

This is a good question, I've just been wondering the same. After all any "mode 7" game will want to draw sprites in positions on the world, not just on the center.